00003 不思议迷宫.0009.2.3:自动换装:回改到真实环境

本文介绍了不思议迷宫游戏中自动换装功能的代码重构过程,将UI代码抽取到独立函数,简化构造函数,并展示了如何将修改后的代码回改到真实游戏环境中,包括创建UI元素、事件监听和交互逻辑。同时,文章提到了在真实设备上调试和日志记录的调整,以更好地跟踪错误和调试信息。
摘要由CSDN通过智能技术生成


00003 不思议迷宫.0009.2.3:自动换装:回改到真实环境

在将代码弄到真实环境之前,先将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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值