环境
操作系统: windows 10 21H1 x64
Visual Studio 2019
Visual Studio Code (插件:QuickXDev)
下载 Quick-Cocos2dx-Community 最新版,并解压到一个路径。
下载后在运行目录运行 setup_win.bat 桌面会生成:
运行创建cocos游戏工程:
打开后点击新建项目
创建游戏后在 vscode中安装QuickXDev插件,安装后用vscode打开文件夹打开创建的工程项目,按F6就可以看到创建的工程
游戏设计
有一个主场景,点击开始后进入游戏场景,开始时水果会从屏幕上方向下掉落。
掉落后点击水果,如果点击的水果相邻处有同类型的水果,那么就高亮显示这些水果,反之就不高亮。再次点击高亮的水果,就消除它们并获得分数
随后水果继续由上至下掉落。
编写游戏代码
1.分辨率适配
当前游戏设计是竖屏游戏,游戏图片以 640*1136 为基准,所以需要修正设计的分辨率
首先打开工程目录下
src/config.lua
-- design resolution
CONFIG_SCREEN_WIDTH = 640
CONFIG_SCREEN_HEIGHT = 960
-- auto scale mode
CONFIG_SCREEN_AUTOSCALE = "FIXED_WIDTH"
随后设置内容缩放比例
src\app\MyApp.lua
function MyApp:run()
cc.FileUtils:getInstance():addSearchPath("res/")
cc.Director:getInstance():setContentScaleFactor(640 / CONFIG_SCREEN_WIDTH)
self:enterScene("MainScene")
end
2.设置游戏主场景
把图片复制到工程目录 res 下
打开 src\app\scenes\MainScene.lua 进行代码编写
这里我已经将输出log代码去除,改为加入场景代码
local MenuScene = class("MenuScene", function()
return display.newScene("MenuScene")
end)
--[=[
1.加载 TexturePacker 打包精灵帧到内存。
2.背景图片是一个精灵,只是图片大一些,
把它坐标设置为屏幕中心点,有利于多
分辨率适配的自动裁剪。
3.开始按钮的图片是从精灵帧缓冲中获取得到,
注意 ccui.Button:create 的第四个参数为1
按钮的回调函数简单输出log信息,待游戏场景
加入后,再把它替换为场景代码.
--]=]
function MenuScene:ctor()
-- 1.加载精灵帧
display.addSpriteFrames("fruit.plist", "fruit.png")
-- 2.背景图片
display.newSprite("mainBG.png")
:pos(display.cx,display.cy)
:addTo(self)
-- 3.开始按钮
local btn = ccui.Button:create("startBtn_N.png", "startBtn_S.png", "", 1)
:align(display.CENTER, display.cx, display.cy - 80)
:addTo(self)
btn:addTouchEventListener(function(ref, eventType)
if cc.EventCode.ENDED == eventType then -- 2
print("TODO: switch to PlayScene!")
-- 导入一个 Lua 文件模块,并返回这个模块的类名,后调用new()方法得到类的实例
local playScene = import("app.scenes.PlayScene"):new()
-- 转场,去 PlayScene ,特效:当前场景分成多个块,逐渐替换为新场景,间隔 0.5 秒
display.replaceScene(playScene, "turnOffTiles", 0.5)
end
end)
end
function MenuScene:onEnter()
end
function MenuScene:onExit()
end
return MenuScene
当前部分运行效果如下:
3.设置游戏水果类
创建游戏场景需要的水果类,我们对它有一下要求:
1.修改水果皮肤(因为高亮显示水果)
2.判断水果相邻
3.矩阵坐标
首先 src\app\scenes\MainScene.lua 复制一份重命名为
FruitItem.lua,然后代码实现:
--[=[
定义 FruitItem 类,类的实例是通过匿名函数来构造,
匿名函数接受三个参数:
1. x,水果在矩阵上的 x 坐标,x为大于等于1的整数
2. y, 水果在矩阵上的 y 坐标,y为大于等于1的整数
3. fruitIndex,水果的图片。事先把水果图片都按照编号命名,
以便代码获取图片。如果没有传入 fruitIndex 那么取
1~8 随机数作为 fruitIndex(准备了8张不同水果)
--]=]
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)
function FruitItem:ctor()
end
function FruitItem:onEnter()
end
function FruitItem:onExit()
end
function FruitItem:setActive(active)
self.isActive = active
-- 设置正常图片和高亮图片间的切换
local frame
if (active) then
frame = display.newSpriteFrame("fruit" .. self.fruitIndex .. '_2.png')
else
frame = display.newSpriteFrame("fruit" .. self.fruitIndex .. '_1.png')
end
self:setSpriteFrame(frame)
-- 在从正常图片切换至高亮图片时,播放一组动画
if (active) then
self:stopAllActions()
local scaleTo1 = cc.ScaleTo:create(0.1, 1.1)
local scaleTo2 = cc.ScaleTo:create(0.05, 1.0)
self:runAction(cc.Sequence:create(scaleTo1, scaleTo2))
end
end
--[=[
FruitItem.getWidth 是一个类方法,不是成员方法。它获取水果的宽度
这里把所有水果设计为正方形,并且大小相等。这样有利于水果矩阵布局
所以这个是静态方法,并且只有第一次调用的时候会创建精灵来计算,之后
就直接返回全局变量的值。
--]=]
function FruitItem.getWidth()
g_fruitWidth = 0
if (0 == g_fruitWidth) then
local sprite = display.newSprite("#fruit1_1.png")
g_fruitWidth = sprite:getContentSize().width
end
return g_fruitWidth
end
return FruitItem
3.设置游戏场景
3.1 游戏的UI界面
添加游戏图片、声音、字体等资源
function PlayScene:initUI()
-- 背景图片
display.newSprite("playBG.png")
:pos(display.cx, display.cy)
:addTo(self)
-- high sorce
display.newSprite("#high_score.png")
:align(display.LEFT_CENTER, display.left + 15, display.top - 30)
:addTo(self)
display.newSprite("#highscore_part.png")
:align(display.LEFT_CENTER, display.cx + 10, display.top - 26)
:addTo(self)
self.highSorceLabel = display.newBMFontLabel({
text = tostring(self.highSorce),
font = "font/earth38.fnt",
})
:align(display.CENTER, display.cx + 105, display.top - 24)
:addTo(self)
-- 声音
display.newSprite("#sound.png")
:align(display.CENTER, display.right - 60, display.top - 30)
:addTo(self)
-- stage
display.newSprite("#stage.png")
:align(display.LEFT_CENTER, display.left + 15, display.top - 80)
:addTo(self)
display.newSprite("#stage_part.png")
:align(display.LEFT_CENTER, display.left + 170, display.top - 80)
:addTo(self)
self.highStageLabel = display.newBMFontLabel({
text = tostring(self.stage),
font = "font/earth32.fnt",
})
:align(display.CENTER, display.left + 214, display.top - 78)
:addTo(self)
-- target
display.newSprite("#tarcet.png")
:align(display.LEFT_CENTER, display.cx - 50, display.top - 80)
:addTo(self)
display.newSprite("#tarcet_part.png")
:align(display.LEFT_CENTER, display.cx + 130, display.top - 78)
:addTo(self)
self.highTargetLabel = display.newBMFontLabel({
text = tostring(self.target),
font = "font/earth32.fnt",
})
:align(display.CENTER, display.cx + 195, display.top - 76)
:addTo(self)
-- current sorce
display.newSprite("#score_now.png")
:align(display.CENTER, display.cx, display.top - 150)
:addTo(self)
self.curSorceLabel = display.newBMFontLabel({
text = tostring(self.curSorce),
font = "font/earth48.fnt",
})
:align(display.CENTER, display.cx, display.top - 150)
:addTo(self)
-- 选中水果分数
self.activeScoreLabel = display.newTTFLabel({text = "", size = 30})
:pos(display.width / 2, 120)
:addTo(self)
self.activeScoreLabel:setColor(display.COLOR_WHITE)
end
3.2 水果矩阵算法
function PlayScene:ctor()
-- 初始化随机数 这里是因为 Fruitltem 需要使用随机数
math.newrandomseed()
-- 计算水果矩阵左下角的x、y坐标:以矩阵中点对齐屏幕中点来计算,然后再做Y轴修正。
self.matrixLBX = (display.width - FruitItem.getWidth() * self.xCount - (self.yCount - 1) * self.fruitGap) / 2
self.matrixLBY = (display.height - FruitItem.getWidth() * self.yCount - (self.xCount - 1) * self.fruitGap) / 2 - 30
--
--[=[
addNodeEventListener -> 添加一个节点事件 Node
enter: Node 加入了正在运行的场景
exit: Node 退出了正在运行的场景
enterTransitionFinish: 进入一个新场景时的特效结束
exitTransitionStart: 退出一个现有场景时的特效开始
cleanup: Node 被完全清理并从内存删除时
--]=]
-- 等待转场特效结束后再加载矩阵
self:addNodeEventListener(cc.NODE_EVENT, function(event)
-- 添加节点事件监听
if event.name == "enterTransitionFinish" then
self:initMartix()
end
end)
创建空水果矩阵
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
单个水果的创建
function PlayScene:createAndDropFruit(x, y, fruitIndex)
-- newFruit 是一个新的精灵类
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)
end
3.3 选中水果
-- 绑定触摸事件
newFruit:addNodeEventListener(cc.NODE_TOUCH_EVENT, function(event)
if event.name == "ended" then
if newFruit.isActive then
-- 消除高亮水果,加分,并继续补充水果(由上至下掉落)
self:removeActivedFruits()
self:dropFruits()
else
self:inactive()
self:activeNeighbor(newFruit)
self:showActivesScore()
end
end
if event.name == "began" then
return true
end
end)
newFruit:setTouchEnabled(true)
3.4 高亮选中水果
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右边的水果
if (fruit.x + 1) <= self.xCount then
local rightNeighbor = self.matrix[(fruit.y - 1) * self.xCount + fruit.x + 1]
if (rightNeighbor.isActive == false) and (rightNeighbor.fruitIndex == fruit.fruitIndex) then
rightNeighbor:setActive(true)
table.insert(self.actives, rightNeighbor)
self:activeNeighbor(rightNeighbor)
end
end
-- 检查fruit上边的水果
if (fruit.y + 1) <= self.yCount then
local upNeighbor = self.matrix[fruit.y * self.xCount + fruit.x]
if (upNeighbor.isActive == false) and (upNeighbor.fruitIndex == fruit.fruitIndex) then
upNeighbor:setActive(true)
table.insert(self.actives, upNeighbor)
self:activeNeighbor(upNeighbor)
end
end
-- 检查fruit下边的水果
if (fruit.y - 1) >= 1 then
local downNeighbor = self.matrix[(fruit.y - 2) * self.xCount + fruit.x]
if (downNeighbor.isActive == false) and (downNeighbor.fruitIndex == fruit.fruitIndex) then
downNeighbor:setActive(true)
table.insert(self.actives, downNeighbor)
self:activeNeighbor(downNeighbor)
end
end
end
3.5 水果掉落与补全
function PlayScene:dropFruits()
local emptyInfo = {}
-- 1. 掉落已存在的水果
-- 一列一列的处理
for x = 1, self.xCount do
local removedFruits = 0
local newY = 0
-- 从下往上处理
for y = 1, self.yCount do
local temp = self.matrix[(y - 1) * self.xCount + x]
if temp == nil then
-- 水果已被移除
removedFruits = removedFruits + 1
else
-- 如果水果下有空缺,向下移动空缺个位置
if removedFruits > 0 then
newY = y - removedFruits
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] = removedFruits
end
-- 2. 掉落新水果补齐空缺
for x = 1, self.xCount do
for y = self.yCount - emptyInfo[x] + 1, self.yCount do
self:createAndDropFruit(x, y)
end
end
end
3.6 分数算法
self.curSorce = 0 -- 当前分数
self.scoreStep = 10 -- 加成分数
self.activeScore = 0 -- 当前高亮的水果得分
-- 更新当前得分
self.curSorce = self.curSorce + self.activeScore
self.curSorceLabel:setString(tostring(self.curSorce))
-- 水果分数依次为5、15、25、35... ,求它们的和
self.activeScore = (self.scoreStart * 2 + self.scoreStep * (#self.actives - 1)) * #self.actives / 2
self.activeScoreLabel:setString(string.format("%d 连消,得分 %d", #self.actives, self.activeScore))
最后
local FruitItem = import(".FruitItem")
local PlayScene = class("PlayScene", function()
return display.newScene("PlayScene")
end)
function PlayScene:ctor()
-- init value
self.highSorce = 0 -- 最高分数
self.stage = 1 -- 当前关卡
self.target = 123 -- 通关分数
self.curSorce = 0 -- 当前分数
self.xCount = 8 -- 水平方向水果数
self.yCount = 8 -- 垂直方向水果数
self.fruitGap = 0 -- 水果间距
self.scoreStart = 5 -- 水果基分
self.scoreStep = 10 -- 加成分数
self.activeScore = 0 -- 当前高亮的水果得分
self:initUI()
-- 初始化随机数 这里是因为 Fruitltem 需要使用随机数
math.newrandomseed()
-- 计算水果矩阵左下角的x、y坐标:以矩阵中点对齐屏幕中点来计算,然后再做Y轴修正。
self.matrixLBX = (display.width - FruitItem.getWidth() * self.xCount - (self.yCount - 1) * self.fruitGap) / 2
self.matrixLBY = (display.height - FruitItem.getWidth() * self.yCount - (self.xCount - 1) * self.fruitGap) / 2 - 30
--
--[=[
addNodeEventListener -> 添加一个节点事件 Node
enter: Node 加入了正在运行的场景
exit: Node 退出了正在运行的场景
enterTransitionFinish: 进入一个新场景时的特效结束
exitTransitionStart: 退出一个现有场景时的特效开始
cleanup: Node 被完全清理并从内存删除时
--]=]
-- 等待转场特效结束后再加载矩阵
self:addNodeEventListener(cc.NODE_EVENT, function(event)
-- 添加节点事件监听
if event.name == "enterTransitionFinish" then
self:initMartix()
end
end)
end
function PlayScene:initUI()
-- 背景图片
display.newSprite("playBG.png")
:pos(display.cx, display.cy)
:addTo(self)
-- high sorce
display.newSprite("#high_score.png")
:align(display.LEFT_CENTER, display.left + 15, display.top - 30)
:addTo(self)
display.newSprite("#highscore_part.png")
:align(display.LEFT_CENTER, display.cx + 10, display.top - 26)
:addTo(self)
self.highSorceLabel = display.newBMFontLabel({
text = tostring(self.highSorce),
font = "font/earth38.fnt",
})
:align(display.CENTER, display.cx + 105, display.top - 24)
:addTo(self)
-- 声音
display.newSprite("#sound.png")
:align(display.CENTER, display.right - 60, display.top - 30)
:addTo(self)
-- stage
display.newSprite("#stage.png")
:align(display.LEFT_CENTER, display.left + 15, display.top - 80)
:addTo(self)
display.newSprite("#stage_part.png")
:align(display.LEFT_CENTER, display.left + 170, display.top - 80)
:addTo(self)
self.highStageLabel = display.newBMFontLabel({
text = tostring(self.stage),
font = "font/earth32.fnt",
})
:align(display.CENTER, display.left + 214, display.top - 78)
:addTo(self)
-- target
display.newSprite("#tarcet.png")
:align(display.LEFT_CENTER, display.cx - 50, display.top - 80)
:addTo(self)
display.newSprite("#tarcet_part.png")
:align(display.LEFT_CENTER, display.cx + 130, display.top - 78)
:addTo(self)
self.highTargetLabel = display.newBMFontLabel({
text = tostring(self.target),
font = "font/earth32.fnt",
})
:align(display.CENTER, display.cx + 195, display.top - 76)
:addTo(self)
-- current sorce
display.newSprite("#score_now.png")
:align(display.CENTER, display.cx, display.top - 150)
:addTo(self)
self.curSorceLabel = display.newBMFontLabel({
text = tostring(self.curSorce),
font = "font/earth48.fnt",
})
:align(display.CENTER, display.cx, display.top - 150)
:addTo(self)
-- 选中水果分数
self.activeScoreLabel = display.newTTFLabel({text = "", size = 30})
:pos(display.width / 2, 120)
:addTo(self)
self.activeScoreLabel:setColor(display.COLOR_WHITE)
end
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
function PlayScene:createAndDropFruit(x, y, fruitIndex)
-- newFruit 是一个新的精灵类
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)
if event.name == "ended" then
if newFruit.isActive then
self:removeActivedFruits()
self:dropFruits()
else
self:inactive()
self:activeNeighbor(newFruit)
self:showActivesScore()
end
end
if event.name == "began" then
return true
end
end)
newFruit:setTouchEnabled(true)
end
function PlayScene:removeActivedFruits()
local fruitScore = self.scoreStart
for _, fruit in pairs(self.actives) do
if (fruit) then
-- 从矩阵中移除
self.matrix[(fruit.y - 1) * self.xCount + fruit.x] = nil
-- 分数特效
self:scorePopupEffect(fruitScore, fruit:getPosition())
fruitScore = fruitScore + self.scoreStep
fruit:removeFromParent()
end
end
-- 清空高亮数组
self.actives = {}
-- 更新当前得分
self.curSorce = self.curSorce + self.activeScore
self.curSorceLabel:setString(tostring(self.curSorce))
-- 清空高亮水果分数统计
self.activeScoreLabel:setString("")
self.activeScore = 0
end
function PlayScene:scorePopupEffect(score, px, py)
local labelScore = display.newBMFontLabel({text = tostring(score), font = "font/earth32.fnt"})
:pos(px, py)
:addTo(self)
-- cc.MoveBy 通过修改Node对象位置属性来移动该 x,y像素,
-- 参数1: 持续时间0.8秒,参数位置 0~80
-- 参数2: 移动的x坐标和y坐标,
local move = cc.MoveBy:create(0.8,cc.p(0, 80)) --cc.p(0, 80)
-- 在 0.8 秒内淡出精灵
local fadeOut = cc.FadeOut:create(0.8)
-- 按照我们想要运行它们的顺序创建动作序列
local action = cc.Sequence:create(
-- 创建运行动画动作
cc.Spawn:create(move, fadeOut),
-- 执行完第一个参数后,执行 动画结束移除 Label
cc.CallFunc:create(function() labelScore:removeFromParent() end)
)
-- 运行动作序列
labelScore:runAction(action)
end
function PlayScene:dropFruits()
local emptyInfo = {}
-- 1. 掉落已存在的水果
-- 一列一列的处理
for x = 1, self.xCount do
local removedFruits = 0
local newY = 0
-- 从下往上处理
for y = 1, self.yCount do
local temp = self.matrix[(y - 1) * self.xCount + x]
if temp == nil then
-- 水果已被移除
removedFruits = removedFruits + 1
else
-- 如果水果下有空缺,向下移动空缺个位置
if removedFruits > 0 then
newY = y - removedFruits
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] = removedFruits
end
-- 2. 掉落新水果补齐空缺
for x = 1, self.xCount do
for y = self.yCount - emptyInfo[x] + 1, self.yCount do
self:createAndDropFruit(x, y)
end
end
end
function PlayScene:positionOfFruit(x, y)
local px = self.matrixLBX + (FruitItem.getWidth() + self.fruitGap) * (x - 1) + FruitItem.getWidth() / 2
local py = self.matrixLBY + (FruitItem.getWidth() + self.fruitGap) * (y - 1) + FruitItem.getWidth() / 2
return cc.p(px, py)
end
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右边的水果
if (fruit.x + 1) <= self.xCount then
local rightNeighbor = self.matrix[(fruit.y - 1) * self.xCount + fruit.x + 1]
if (rightNeighbor.isActive == false) and (rightNeighbor.fruitIndex == fruit.fruitIndex) then
rightNeighbor:setActive(true)
table.insert(self.actives, rightNeighbor)
self:activeNeighbor(rightNeighbor)
end
end
-- 检查fruit上边的水果
if (fruit.y + 1) <= self.yCount then
local upNeighbor = self.matrix[fruit.y * self.xCount + fruit.x]
if (upNeighbor.isActive == false) and (upNeighbor.fruitIndex == fruit.fruitIndex) then
upNeighbor:setActive(true)
table.insert(self.actives, upNeighbor)
self:activeNeighbor(upNeighbor)
end
end
-- 检查fruit下边的水果
if (fruit.y - 1) >= 1 then
local downNeighbor = self.matrix[(fruit.y - 2) * self.xCount + fruit.x]
if (downNeighbor.isActive == false) and (downNeighbor.fruitIndex == fruit.fruitIndex) then
downNeighbor:setActive(true)
table.insert(self.actives, downNeighbor)
self:activeNeighbor(downNeighbor)
end
end
end
function PlayScene:inactive()
for _, fruit in pairs(self.actives) do
if (fruit) then
fruit:setActive(false)
end
end
self.actives = {}
end
function PlayScene:showActivesScore()
-- 只有一个高亮,取消高亮并返回
if 1 == #self.actives then
self:inactive()
self.activeScoreLabel:setString("")
self.activeScore = 0
return
end
-- 水果分数依次为5、15、25、35... ,求它们的和
self.activeScore = (self.scoreStart * 2 + self.scoreStep * (#self.actives - 1)) * #self.actives / 2
self.activeScoreLabel:setString(string.format("%d 连消,得分 %d", #self.actives, self.activeScore))
end
function PlayScene:onEnter()
end
function PlayScene:onExit()
end
return PlayScene
当前项目源码与资源下载:
https://download.csdn.net/download/Tiantangbujimo7/85413473