在将代码弄到真实环境之前,先将UIAutoEquipingConfig中的类似代码重构一下。
local label_goingFloor =cc.Label:createWithTTF("进入下一层",FONT, FONT_SIZE);
local icon_goingFloor =cc.Sprite:create(grayedEquipment);
local button_goingFloor =ccui.Button:create(BUTTON_IMAGE_NORMAL, BUTTON_IMAGE_SELECTED);
button_goingFloor:setTitleText(BUTTON_TEXT);
button_goingFloor:setTitleFontName(FONT);
button_goingFloor:setTitleFontSize(FONT_SIZE);
local function onButton_goingFloor(sender,eventType)
print("onButton_goingFloor");
end
AddTouchEventListener(button_goingFloor,onButton_goingFloor);
self:addChild(label_goingFloor);
self:addChild(icon_goingFloor);
self:addChild(button_goingFloor);
上面的这个代码,抽取到一个函数中:
functionUIAutoEquipingConfig:appendItem(equipType, labelText, buttonCallback)
local label = cc.Label:createWithTTF(labelText,FONT, FONT_SIZE);
local icon =cc.Sprite:create(grayedEquipments[equipType]);
local button =ccui.Button:create(BUTTON_IMAGE_NORMAL, BUTTON_IMAGE_SELECTED);
button:setTitleText(BUTTON_TEXT);
button:setTitleFontName(FONT);
button:setTitleFontSize(FONT_SIZE);
AddTouchEventListener(button,buttonCallback);
self:addChild(label);
self:addChild(icon);
self:addChild(button);
return {label, icon, button,};
end
原来的构造函数改成这样:
functionUIAutoEquipingConfig:ctor(equipType)
self:setName("UIAutoEquipingConfig");
local background =cc.Sprite:create("images/ui/button/btn_large_disable.png");
self:addChild(background);
UIPositioning.fitToScreen(background);
local button_close =ccui.Button:create("images/ui/equip/222.png");
local function onButton_close(sender,eventType)
self:getParent():removeChild(self)
end
AddTouchEventListener(button_close,onButton_close);
self:addChild(button_close);
UIPositioning.rightTop(button_close);
UIPositioning.hvCenter({
self:appendItem(equipType,"当前装备 ",
function (sender,eventType)
print("onButton_current");
end),
self:appendItem(equipType,"进入下一层",
function (sender,eventType)
print("onButton_nextFloor");
end),
self:appendItem(equipType,"进到下一层",
function (sender,eventType)
print("onButton_arrivingFloor");
end),
self:appendItem(equipType,"胜利者石柱",
function (sender,eventType)
print("onButton_clickingColumn");
end),
}, HSPACING, VSPACING);
return self
end
是不是变得简短很多?当然,随着修改的进行,可能还会把按钮回调函数抽取出去:
functionUIAutoEquipingConfig:ctor(equipType)
……
local function onButton_current(sender,eventType)
print("onButton_current");
end
local functiononButton_nextFloor(sender, eventType)
print("onButton_nextFloor");
end
local functiononButton_arrivingFloor(sender, eventType)
print("onButton_arrivingFloor");
end
local functiononButton_clickingColumn(sender, eventType)
print("onButton_clickingColumn");
end
UIPositioning.hvCenter({
self:appendItem(equipType,"当前装备 ", onButton_current),
self:appendItem(equipType,"进入下一层",onButton_nextFloor),
self:appendItem(equipType,"进到下一层",onButton_arrivingFloor),
self:appendItem(equipType,"胜利者石柱",onButton_clickingColumn),
}, HSPACING, VSPACING);
return self
end
是不是更加好看了一点?
下面进行改回到真实环境的工作。
首先在src目录下创建my文件夹,然后将UIAutoEquipingConfig.luac和UIPositioning.luac拷贝过去,并且将它们改为游戏所用的格式。
这两天有事耽搁了,文章没有详细写,下面仅给出代码,实现了:交互:自动换装配置界面中点击“装备图标”,弹出装备信息界面(含“卸下”按钮)、交互:自动换装配置界面中点击“选择按钮”,弹出装备装备选择界面(不能穿戴的装备灰掉)、交互:自动换装配置界面中点击“重置按钮”,取消配置。增加了重置按钮。
UIEquip.luac:
EQUIP_OPEN_TYPE_MY_AUTO_EQUIPING = 99;
-- 将英雄已经装备上的装备显示在界面上
function UIEquips:showUsedEquips(equipType)
……
ifself.openType == EQUIP_OPEN_TYPE_BLACKSMITH then
……
elseifself.openType == EQUIP_OPEN_TYPE_MY_AUTO_EQUIPING then
mask:setVisible(self.equipTypes[i]~= self.extraPara.equipType);
else
……
end
-- 重绘
function UIEquips:redraw(refreshNewItem)
……
elseifself.openType == EQUIP_OPEN_TYPE_MY_AUTO_EQUIPING then
ifEquipM.isEquipment(classId) and EquipM.query(classId, "type") ==self.extraPara.equipType then
mask:setVisible(false);
else
mask:setVisible(true);
end
else
……
end
UIAutoEquipingConfig.luac:
……
UIAutoEquipingConfig.configs = {};
functionUIAutoEquipingConfig.getEventConfig(equipType, event)
ifevent == "current" then
returnEquipM.getEquip(equipType, 1);
end
localconfig = UIAutoEquipingConfig.configs[equipType];
ifconfig == nil then return nil; end
returnconfig[event];
end
functionUIAutoEquipingConfig.setEventConfig(equipType, event, classId)
localconfig = UIAutoEquipingConfig.configs[equipType];
ifconfig == nil then
config= {};
UIAutoEquipingConfig.configs[equipType]= config;
end
config[event]= classId;
end
……
function showAutoEquipingConfigUI(equipType)
print("showAutoEquipingConfigUI1");
closeEquipUI();
localautoEquipingConfig = UIAutoEquipingConfig.create(equipType);
UIMgr.getCurrentScene():addForm(autoEquipingConfig);
print("showAutoEquipingConfigUI2");
end
function closeAutoEquipingConfigUI()
UIMgr.getCurrentScene():removeFormByName("UIAutoEquipingConfig");
end
function UIAutoEquipingConfig:ctor(equipType)
self:setName("UIAutoEquipingConfig");
self.equipType= equipType;
self.equiping= {};
self.selecting= "";
self:setLocalZOrder(UIMgr.TOP_MENU_ZORDER+ 1);
localbackground =cc.Sprite:create("images/ui/button/btn_large_disable.png");
self:addChild(background);
UIPositioning.fitToScreen(background);
localbutton_close = ccui.Button:create("images/ui/equip/222.png");
localfunction onButton_close(sender, eventType)
closeAutoEquipingConfigUI();
end
AddTouchEventListener(button_close,onButton_close);
self:addChild(button_close);
UIPositioning.rightTop(button_close);
self:registerEventCallback();
--common----------
localfunction onIcon(sender, eventType, event)
ifeventType ~= ccui.TouchEventType.ended then return; end
AudioM.playFx("button_click");
localclassId = UIAutoEquipingConfig.getEventConfig(self.equipType, event);
ifclassId then
--打开装备操作界面
ifUIMgr.getCurrentScene():isOpen("UIEquipsOperation") then
UIMgr.getCurrentScene():removeFormByName("UIEquipsOperation");
end
localeot = EQUIP_OPERATE_UNEQUIP;
localequiped = true;
ifevent ~= "current" andUIAutoEquipingConfig.getEventConfig(self.equipType, event) ~=UIAutoEquipingConfig.getEventConfig(self.equipType, "current") then
eot= EQUIP_OPERATE_EQUIP;
equiped= false;
end
localuiForm = UIEquipsOperation.create(classId, eot, equiped, {});
UIMgr.getCurrentScene():addForm(uiForm);
end
end
localfunction onSelect(sender, eventType, event)
ifeventType ~= ccui.TouchEventType.ended then return; end
AudioM.playFx("button_click");
self.selecting= event;
localuiEquip = UIEquips.create(EQUIP_OPEN_TYPE_MY_AUTO_EQUIPING,{["equipType"] = self.equipType});
UIMgr.getCurrentScene():addForm(uiEquip);
end
localfunction onUnselect(sender, eventType, event)
ifeventType ~= ccui.TouchEventType.ended then return; end
AudioM.playFx("button_click");
UIAutoEquipingConfig.setEventConfig(self.equipType,event, 0);
self:setupEquiping(self.equipType,event);
end
--current----------
localfunction onIcon_current(sender, eventType)
onIcon(sender,eventType, "current");
end
--nextFloor----------
localfunction onIcon_nextFloor(sender, eventType)
onIcon(sender,eventType, "nextFloor");
end
localfunction onSelectButton_nextFloor(sender, eventType)
onSelect(sender,eventType, "nextFloor");
end
localfunction onUnselectButton_nextFloor(sender, eventType)
onUnselect(sender,eventType, "nextFloor");
end
--arrivingFloor----------
localfunction onIcon_arrivingFloor(sender, eventType)
onIcon(sender,eventType, "arrivingFloor");
end
localfunction onSelectButton_arrivingFloor(sender, eventType)
onSelect(sender,eventType, "arrivingFloor");
end
localfunction onUnselectButton_arrivingFloor(sender, eventType)
onUnselect(sender,eventType, "arrivingFloor");
end
--clickingColumn----------
localfunction onIcon_clickingColumn(sender, eventType)
onIcon(sender,eventType, "clickingColumn");
end
localfunction onSelectButton_clickingColumn(sender, eventType)
onSelect(sender,eventType, "clickingColumn");
end
localfunction onUnselectButton_clickingColumn(sender, eventType)
onUnselect(sender,eventType, "clickingColumn");
end
self.events=
{
{"current","当前装备 ", onIcon_current, nil, nil,},
{"nextFloor","进入下一层", onIcon_nextFloor, onSelectButton_nextFloor,onUnselectButton_nextFloor, },
{"arrivingFloor","进到下一层", onIcon_arrivingFloor, onSelectButton_arrivingFloor,onUnselectButton_arrivingFloor, },
{"clickingColumn","胜利者石柱", onIcon_clickingColumn, onSelectButton_clickingColumn,onUnselectButton_clickingColumn, },
};
localitems = {};
fori=1, #self.events do
items[i]= self:appendItem(equipType, self.events[i][2], self.events[i][4],self.events[i][5]);
end
self.positions= UIPositioning.hvCenter(items, HSPACING, VSPACING);
fori=1, #self.events do
self:setupEquiping(equipType,self.events[i][1], self.events[i][3]);
end
returnself
end
functionUIAutoEquipingConfig:appendItem(equipType, labelText, selectButtonCallback,unselectButtonCallback)
locallabel = cc.Label:createWithTTF(labelText, FONT, FONT_SIZE);
localicon = cc.Sprite:create(grayedEquipments[equipType]);
self:addChild(label);
self:addChild(icon);
localselectButton = ccui.Button:create(BUTTON_IMAGE_NORMAL, BUTTON_IMAGE_SELECTED);
selectButton:setTitleText(BUTTON_TEXT_SELECT);
selectButton:setTitleFontName(FONT);
selectButton:setTitleFontSize(FONT_SIZE);
ifselectButtonCallback ~= nil then
AddTouchEventListener(selectButton,selectButtonCallback);
else
selectButton:setEnabled(false);
end
self:addChild(selectButton);
localunselectButton = ccui.Button:create(BUTTON_IMAGE_NORMAL,BUTTON_IMAGE_SELECTED);
unselectButton:setTitleText(BUTTON_TEXT_UNSELECT);
unselectButton:setTitleFontName(FONT);
unselectButton:setTitleFontSize(FONT_SIZE);
ifunselectButtonCallback ~= nil then
AddTouchEventListener(unselectButton,unselectButtonCallback);
else
unselectButton:setEnabled(false);
end
self:addChild(unselectButton);
return{label, icon, selectButton, unselectButton, };
end
functionUIAutoEquipingConfig:indexOfEvent(event)
fori=1, #self.events do
ifself.events[i][1] == event then return i; end
end
return0;
end
functionUIAutoEquipingConfig:setupEquiping(equipType, event, iconCallback)
localclassId = UIAutoEquipingConfig.getEventConfig(equipType, event);
localbtn = self.equiping[event]
ifclassId then
localicon = getItemIconPath(ItemM.query(classId, "icon"));
ifbtn == nil then
btn= ccui.Button:create(icon, icon);
localposition = self.positions[self:indexOfEvent(event)][2];
btn:setPosition(position.x,position.y);
AddTouchEventListener(btn,iconCallback);
self:addChild(btn);
self.equiping[event]= btn;
else
btn:loadTexture(icon);
end
btn:setVisible(true);
btn:setTouchEnabled(true);
else
ifbtn ~= nil then
btn:setVisible(false);
btn:setTouchEnabled(false);
end
end
end
-- 注册事件处理回调函数
functionUIAutoEquipingConfig:registerEventCallback()
EventMgr.removeAll("UIAutoEquipingConfig");
--关注穿戴装备的事件
EventMgr.register("UIAutoEquipingConfig",event.EQUIP, function(classId)
self:setupEquiping(self.equipType,"current");
end);
--关注卸下装备的事件
EventMgr.register("UIAutoEquipingConfig",event.UNEQUIP, function(params)
self:setupEquiping(self.equipType,"current");
end);
--关注工坊放入装备的事件
EventMgr.register("UIAutoEquipingConfig",event.BLACKSMITH_INPUT_EQUIP, function(args)
self:whenInputEquip(args);
end);
--界面析构后,需要清理下
self:registerScriptHandler(function(ev)
ifev == "exit" then
EventMgr.removeAll("UIAutoEquipingConfig");
end
end);
end
-- 工坊放入装备的回调
functionUIAutoEquipingConfig:whenInputEquip(args)
UIAutoEquipingConfig.setEventConfig(self.equipType,self.selecting, args.classId);
self:setupEquiping(self.equipType,self.selecting);
end
构造中的那些类似函数让人很不爽,应当使用模版解决。setupEquiping(equipType, event,iconCallback)包含了两个功能:创建控件、刷新,应当重构。appendItem函数中也有类似的代码,也需要重构。
最后,希望在真机上看看调试信息或者错误堆栈。我不知道修改Config.luac中的DEBUG_MODE是否能够生效。原版中还将这个标记用在了测试(单元测试和试验)上,因此我就跳过了这个标记,直接进到/src/core/base/Log.luac中,修改:
-- 是否允许输出 trace
function is_trace_enable()
returntrue;
end
-- 重定义print
function print(msg)
sys_print = release_print;
……
end
结果发现,发生错误时才会将错误堆栈保存到/Library/Preferences/com.leiting.gumballs.plist中,而且仅保存一次。于是又改了一下:
require("core/base/File.luac");
-- 重定义print
function print(msg)
local msg = string.format("%s %s\r\n", os.date(),tostring(msg));
file.updateFile(msg, "my.log")
end
现在所有的信息都会保存到/Documents/my.log中。