在前面的文章中曾接触过炼金坊方面的代码。这次我要给炼金坊增加自动捡钱功能,那些研究研究经验刚好可以派上用场。
炼金炉喷出金子的函数是UINewWorkshop:produceGold(count)。
玩家捡取金币的代码,还是得从触摸事件找起:
-- 注册点击处理回调
functionUINewWorkshop:registerTouchCallBack()
……
-- 背景图点击回调
local bg = findChildByName(self.node,"bg");
bg:addTouchEventListener(function(sender,eventType)
if eventType ==ccui.TouchEventType.began then
local beganPos =sender:getTouchBeganPosition();
self.lastMovePos = sender:getTouchBeganPosition();
self:tryPickupGold(beganPos);
elseif eventType ==ccui.TouchEventType.moved then
local movePos =sender:getTouchMovePosition();
self.lastMovePos = sender:getTouchBeganPosition();
self:tryPickupGold(movePos);
end
end);
……
end
self:tryPickupGold(beganPos),就它了。找到之后呢,只要把它在UINewWorkshop:produceGold(count)中进行调用就好了:
-- 炼金炉喷出金子
functionUINewWorkshop:produceGold(count)
……
local goldNode = findChildByName(self.node,"bg/gold_node");
local goldParentNode =findChildByName(self.node, "bg");
for i = 1, count do
local newGold = goldNode:clone();
newGold:setVisible(true);
local gold =newGold:getChildByName("gold");
gold:setVisible(true);
local shadow =newGold:getChildByName("shadow");
local targetNode = self:getFreePos();
if targetNode == nil then
-- 没有空闲位置,就不产了
return;
end
local targetPos =cc.p(targetNode:getPosition());
targetNode.isUsed = true;
local jumpTo1 = cc.JumpTo:create(0.2,cc.pAdd(targetPos, targetNode.offset2), 50, 1);
local jumpTo2 = cc.JumpTo:create(0.1,cc.pAdd(targetPos, targetNode.offset1), 10, 1);
local jumpTo3 = cc.JumpTo:create(0.1,targetPos, 5, 1);
local callFunc =cc.CallFunc:create(function()
goldParentNode:removeChild(newGold);
newGold:setPosition(0, 0);
shadow:setVisible(true);
-- 到目标节点后,从底图删除,挂在目标节点上
targetNode:removeAllChildren();
targetNode:addChild(newGold);
targetNode:setVisible(true);
local function showGoldEffect()
local effect;
local callFunc1 =cc.CallFunc:create(function()
local gold =newGold:getChildByName("gold");
gold:setOpacity(10);
shadow:setOpacity(0);
-- 播放金币闪过的动画
_, effect =playEffect(newGold, 1161, 2, -13);
end);
local delay = cc.DelayTime:create(1);
local callFunc2 =cc.CallFunc:create(function()
newGold:removeChild(effect);
-- 动画播放完毕,金币图标出现
gold:setOpacity(255);
shadow:setOpacity(255);
performWithDelay(newGold,showGoldEffect, math.random(3, 10));
end);
newGold:runAction(cc.Sequence:create(callFunc1, delay, callFunc2));
end
showGoldEffect();
-- 第一块金币,引导玩家去捡
if self.waitForClick == nil then
self.waitForClick = true;
local hand =newGold:getChildByName("hand");
hand:setVisible(true);
hand:setLocalZOrder(1000);
playClickEffect(hand);
end
-- 一次喷两个金币,引导玩家拖动
if self.showDragEffect and i == 1then
self.showDragEffect = false;
self.waitForClick = true;
local hand =newGold:getChildByName("hand");
hand:removeAllChildren();
playDragEffect(hand);
end
self:tryPickupGold(targetPos);
end);
newGold:runAction(cc.Sequence:create(jumpTo1, jumpTo2, jumpTo3,callFunc));
-- 先在底图上飞
goldParentNode:addChild(newGold);
end
if self.lastCount == 1 and count == 2 then
self.showDragEffect = true;
end
self.lastCount = count;
end
这么改之后发现居然有bug,那就换一种方法。喷射金币是一个定时循环操作,我们只要在这个定时中不停地tryPickupGold就行。这个定时循环在UINewWorkshop:startProduce()中定义:
-- 炼金炉开始工作
functionUINewWorkshop:startProduce()
……
-- 金币位置列表
self.goldPosList = {};
for i = 1, 20 do
local posNode =findChildByName(self.node, "bg/pos_parent/pos"..i);
local jump2Node =posNode:getChildByName("jump2");
local jump1Node =posNode:getChildByName("jump1");
posNode.offset1 =cc.p(jump1Node:getPosition());
posNode.offset2 =cc.p(jump2Node:getPosition());
posNode:setLocalZOrder(math.floor(-posNode:getPositionY()));
table.insert(self.goldPosList,posNode);
end
……
-- 炼金
local function start()
if self.waitForClick orself:getFreePos() == nil or self.isHighlightWorkshopBubble
or (notAlchemyWorkshopM.isAllGuideFinished() and bubble:isVisible()) then
produceNode:removeAllChildren();
performWithDelay(self, start, 0.5);
return;
end
furnaceIcon:setVisible(false);
-- 喷金币
produceNode:removeAllChildren();
playEffect(produceNode, 1173, 88, -53);
-- performWithDelay(produceNode,function() AudioM.playFx("workshop_furnance"); end, 0.1);
-- performWithDelay(produceNode,function() AudioM.playFx("workshop_produce"); end, 2.3);
local function produce()
furnaceIcon:setVisible(true);
local produceCountEachTime =FormulaM.invoke("GET_GOLD_PRODUCE_AMOUNT");
self:produceGold(produceCountEachTime);
performWithDelay(self, start, 0.6);
end
performWithDelay(self, produce, 2);
for_, node in pairs(self.goldPosList) do
local targetPos =cc.p(node:getPosition());
self:tryPickupGold(targetPos);
end
end
start();
end
居然还是有问题,最左边的两个金币无法捡取,不过倒也可以接受了,就这样吧。
有需要的进群 (161355323) 下载补丁。