用Lua又写了遍2048.这回发现比之前写的更快了。在上下左右移动块的上边,例如像左移动:那么需要遍历的则
是x轴方向。并且是跟移动方向相反的向右进行遍历。此时定义一个对象,记录向右遍历的第i个的值,记录后将此值
赋给它左边的那个值(这里说的通俗些,坑了勿喷!)最后代码呈上。有需要的可留言,可以找我要代码一起学习。
function GameScene:leftCombineNumber() if (isPlay == false) then return true end local card local nextCard for j = 0,3 do for i = 0,3 do card = cardList[i..":"..j] if(card:getData() ~= 0) then local k = i+1 while(k < 4) do nextCard = cardList[k..":"..j] if (nextCard:getData() ~=0) then if(card:getData() == nextCard:getData()) then card:setNum(card:getData()*2) nextCard:setNum(0) totalscore = totalscore+card:getData() end k = 4 break end k = k + 1 end end end end for j = 0,3 do card = cardList[i..":"..j] if(card:getData() == 0) then local k = i+1 while(k < 4) do nextCard = cardList[k..":"..j] if (nextCard:getData() ~=0) then card:setNum(nextCard:getData()) nextCard:setNum(0) k = 4 end k = k + 1 end end end end self:updateNumber() end