您的位置:首页 > 移动开发 > Cocos引擎

【quick-cocos2d-lua】 水果消除 FruitFest

2018-06-21 10:25 85 查看

 

开始场景:start按钮

游戏场景:最高得分,当前等级,闯关分数,当前得分标签,音乐,水果掉落消除区,进度条显示。

玩法:连着2个及2个以上水果相同时,可进行消除,点击一下,水果变高亮,点击第二下进行消除。

 

代码实现:

1.加载UI控件,最高分和当前关卡通过取出保存的数据获得

2.切换到游戏场景结束时,加载水果矩阵。

 

水果矩阵的实现:

 

 

x,y方向各8个,self.xCount = 8,self.yCount = 8。

先水果矩阵左下角水果的坐标,其他水果可在此基础上计算。左下角水果坐标为:

[code]self.matrixLBX = (display.width - FruitItem.getWidth()*self.xCount -
self.fruitGap*(self.yCount - 1))/2
self.matrixLBY = (display.height - FruitItem.getWidth()*self.yCount -
self.fruitGap*(self.xCount - 1))/2 - 30

self.matrix = {},用来放水果矩阵,为了确保有可消除的水果,使左下角第一个和第二个产生的水果一样。

[code]function PlayScene:initMartix()
--创建空矩阵
self.matrix = {}
self.actives = {}
for y = 1,self.yCount do
for x = 1,self.xCount do
if 1 == y and 2 == x then
--确保有可消除的水果
self:createAndDropFruit(x,y,self.matrix[1].fruitIndex)
else
self:createAndDropFruit(x,y)
end
end
end
end

如何创建掉落水果:

随机产生一种水果,通过左下角坐标和x,y的值算出掉落结束位置,设置一个开始位置,创建一个动作从开始位置移动到结束位置,将水果添加到场景,并设置水果可触摸,并给水果注册一个触摸监听。

[code]function PlayScene:createAndDropFruit(x,y,fruitIndex)
local newFruit = FruitItem.new(x,y,fruitIndex)
local endPosition = self:positionOfFruit(x,y)
local startPosition = cc.p(endPosition.x,endPosition.y + display.height/2)
newFruit:setPosition(startPosition)
local speed = startPosition.y/(2 * display.height)
newFruit:runAction(cc.MoveTo:create(speed,endPosition))
self.matrix[(y-1) * self.xCount + x] = newFruit
self:addChild(newFruit)

newFruit:addNodeEventListener(cc.NODE_TOUCH_EVENT,function(event)
--触摸结束时判断水果是否高亮,高亮则消除,掉落水果等操作;不高亮则进行判断是够有相同的水果,有相同水果进行高亮
end)
end

--FruitItem部分代码实现
local FruitItem = class("FruitItem",   function (x,y,fruitIndex)
fruitIndex = fruitIndex or math.round(math.random()*1000) % 8 + 1
local sprite = display.newSprite("#fruit"..fruitIndex..'_1.png')
sprite.fruitIndex = fruitIndex
sprite.x = x
sprite.y = y
sprite.isActive = false
return sprite
end)

--通过左下角水果坐标和x,y算出掉落结束位置
function PlayScene:positionOfFruit(x,y)
local px = self.matrixLBX + FruitItem.getWidth() / 2 + (FruitItem.getWidth() +
self.fruitGap) * (x - 1)
local py = self.matrixLBY + FruitItem.getWidth() / 2 + (FruitItem.getWidth() +
self.fruitGap) * (y - 1)
return  cc.p(px,py)
end

触摸监听内容:

触摸结束时,判断水果是否处于高亮状态。

如果不是高亮状态,则先清除以前处于高亮状态的水果数据(self.actives),然后检查该水果周围的水果,如果有相同的水果,则高亮显示,并添加到高亮数据表里。

[code]function PlayScene:activeNeighbor(fruit)
--高亮水果fruit
if false == fruit.isActive then
fruit:setActive(true)
table.insert(self.actives,fruit)
end

--检查fruit左边的水果
if (fruit.x - 1) >= 1 then
local leftNeighbor = self.matrix[(fruit.y-1)*self.xCount + fruit.x - 1]
if (leftNeighbor.isActive == false) and (leftNeighbor.fruitIndex ==         fruit.fruitIndex) then
leftNeighbor:setActive(true)
table.insert(self.actives,leftNeighbor)
self:activeNeighbor(leftNeighbor)
end
end

--检查fruit右边的水果
--检查fruit上边的水果
--检查fruit下边的水果

end

如果处于高亮状态,则进行水果消除,并掉落水果,检查是否通关,更新分数等。

水果消除,即从水果矩阵数据中移除,把所有高亮的水果fruit从(self.actives)遍历出来,然后从(self.matrix)移除掉,

self.matrix[(fruit.y-1)*self.xCount + fruit.x] = nil

移除的时候,可增加一些动作动画效果,然后清空高亮组,更新得分、进度条等。

 

掉落水果:

1.先掉落还存在的水果,一列列处理,从下往上处理,如果下面有空缺则往下移动。并记录本列最终空缺数emptyInfo = {}

2.掉落新水果补齐空缺

[code]function PlayScene:dropFruits()
local emptyInfo = {}

--掉落已存在的水果
--一列列处理
for x = 1,self.xCount do
local romovedFruits = 0
local newY = 0
--从下往上处理
for y = 1,self.yCount do
local temp = self.matrix[(y-1)*self.xCount + x]
if temp == nil then
--水果已经被移除
romovedFruits = romovedFruits + 1
else
--如果水果下面有空缺则往下移动
if romovedFruits > 0 then
newY = y - romovedFruits
self.matrix[(newY-1)*self.xCount + x] = temp
temp.y = newY
self.matrix[(y-1)*self.xCount + x] = nil

local endPosition = self:positionOfFruit(x,newY)
local speed = (temp:getPositionY() - endPosition.y)/display.height
temp:stopAllActions()
temp:runAction(cc.MoveTo:create(speed,endPosition))
end
end
end

--记录本列最终空缺数
emptyInfo[x] = romovedFruits
end

--掉落新水果补齐空缺
for x = 1,self.xCount do
for y = self.yCount - emptyInfo[x] + 1,self.yCount do
self:createAndDropFruit(x,y)
end
end

end

 

检查是否通关,更新数据,保存数据。

[code]cc.UserDefault:getInstance():setIntegerForKey("HighSorce",self.highSorce)
cc.UserDefault:getInstance():setIntegerForKey("Stage",self.stage)

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

阅读更多
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: