先上主要代码:
local function onMyButton_AutoPickUpAllItems(sender, eventType)
if eventType ~= ccui.TouchEventType.ended then return; end
local function pickAllItems()
for i=1,#self.grids do
local g = self.grids[i];
if tiewen_canPickUpItem(g) and g.pickItemClick ~= nil then
g.pickItemClick(nil, ccui.TouchEventType.began);
end
end
end
local o = {};
o.timer = cc.Director:getInstance():getScheduler():scheduleScriptFunc(function ()
if self.cacheOpenGrids and #self.cacheOpenGrids > 0 then return; end
local g = tiewen_findGridCanBeOpenedButNotMonster(self.grids);
if g == nil then
cc.Director:getInstance():getScheduler():unscheduleScriptEntry(o.timer);
pickAllItems();
else
if g.openGridClick ~= nil then
g.openGridClick(nil, ccui.TouchEventType.began);
end
end
end, 0.1, false);
end
local apai = TieWen_UI.createButton(self, "翻板捡物", onMyButton_AutoPickUpAllItems);
在前面我挑过原版的刺,再看看我自己的代码,只能说,我很惭愧,我的代码一样垃圾、一样的挫。哎,说似乎比做容易。
代码中有一个地方需要说一下,就是那个红色的o。为什么要定义这么一个table而不直接使用timer呢,就如下面这样:
local timer = cc.Director:getInstance():getScheduler():scheduleScriptFunc(function ()
……
cc.Director:getInstance():getScheduler():unscheduleScriptEntry(timer);
……
end, 0.1, false);
我看有的文章上就是这么写的。这可能和cocos2dx的版本(或者说是lua的版本)有关。在具体的这个游戏版本中,这么写是有问题的,传入scheduleScriptFunc的匿名函数被预编译,此时timer的值是nil。
function tiewen_isGridCanBeOpenedButNotMonster(grid)
if DungeonM.canOpenGrid(grid.index) ~= GRID_OPEN_OK then return false; end
if grid.gridData:isMonster() then return grid.gridData.monster:isDead(); end
return true;
end
function tiewen_findGridCanBeOpenedButNotMonster(grids)
for i=1,#grids do
if tiewen_isGridCanBeOpenedButNotMonster(grids[i]) then return grids[i]; end
end
return nil
end
function tiewen_canPickUpItem(grid)
if not DungeonM.checkDungeonAction() then
-- 经检测,不允许执行地牢行为
return false;
end
grid = grid.gridData;
local bonus = grid.bonus;
-- 不是可以拾取的类型
if not grid.has_key
and grid.type ~= GRID_TYPE_BEEHIVE
and grid.type ~= GRID_TYPE_TRAP
and grid.type ~= GRID_TYPE_MONSTER
and grid.type ~= GRID_TYPE_BOSS
and grid.type ~= GRID_TYPE_ITEM
and grid.type ~= GRID_TYPE_BOX
and grid.type ~= GRID_TYPE_SLIME
and grid.type ~= GRID_TYPE_APPRENTICE
and grid.type ~= GRID_TYPE_MAP
and grid.type ~= GRID_TYPE_BOSS_BOX
and grid.type ~= GRID_TYPE_ACTIVITY_BOSS_BOX
and grid.type ~= GRID_TYPE_SUMMON
and grid.type ~= GRID_TYPE_DIAMON_STATUE
and grid.type ~= GRID_TYPE_GOLD_CAN then
return false;
end
-- 冈布奥和学徒
if (grid.type == GRID_TYPE_SLIME or grid.type == GRID_TYPE_APPRENTICE) and not DungeonM.allMonsterIsDead() then
return false;
end
-- 如果是在元素领主副本,提示击杀领主才能领取
if UnrealDungeonM.isInAltarRuins() and not DungeonM.allMonsterIsDead() then
return false;
end
-- 一些提前显示出来的奖励
if grid:isPublicGrid() and not grid.has_key and not DungeonM.allMonsterIsDead() and grid.type ~= GRID_TYPE_TRAP and ( not grid:isMonster() or grid.monster.dbase:query("flee") == 1) then
return false;
end
-- 如果是钥匙
if grid.has_key then
-- 如果怪物没死就不能拾取钥匙
if grid:isMonster() then
if not grid.monster:isDead() then
return false;
end
end
end
-- 没有奖励就不能拾取
if not bonus then
return false;
end
-- 如果还未打开
if not grid:isOpened() then
return false;
end
-- 如果是怪物
if grid:isMonster() then
if not grid.monster:isDead() then
return false;
end
end
return true;
end
如有需要,可进群161355323下载补丁或安装程序。