当前位置:   article > 正文

4、cocos2d-Lua的demo--游戏逻辑GameView_0 then return end

0 then return end
接上, PlayScene场景创建游戏逻辑视图GameView并调用start函数开始游戏:
-- GameView is a combination of view and controller
local GameView = class("GameView", cc.load("mvc").ViewBase)

local BugBase = import("..models.BugBase")
local BugAnt = import("..models.BugAnt")
local BugSpider = import("..models.BugSpider")

local BugSprite = import(".BugSprite")
local DeadBugSprite = import(".DeadBugSprite")

GameView.HOLE_POSITION = cc.p(display.cx - 30, display.cy - 75)
GameView.INIT_LIVES = 99999
GameView.ADD_BUG_INTERVAL_MIN = 1
GameView.ADD_BUG_INTERVAL_MAX = 3

GameView.IMAGE_FILENAMES = {}
GameView.IMAGE_FILENAMES[BugBase.BUG_TYPE_ANT] = "BugAnt.png"
GameView.IMAGE_FILENAMES[BugBase.BUG_TYPE_SPIDER] = "BugSpider.png"

GameView.BUG_ANIMATION_TIMES = {}
GameView.BUG_ANIMATION_TIMES[BugBase.BUG_TYPE_ANT] = 0.15
GameView.BUG_ANIMATION_TIMES[BugBase.BUG_TYPE_SPIDER] = 0.1

GameView.ZORDER_BUG = 100
GameView.ZORDER_DEAD_BUG = 50

GameView.events = {
PLAYER_DEAD_EVENT = "PLAYER_DEAD_EVENT",
}

function GameView:start()
self:scheduleUpdate(handler(self, self.step))
return self
end

function GameView:stop()
self:unscheduleUpdate()
return self
end

function GameView:step(dt)
if self.lives_ <= 0 then return end

self.addBugInterval_ = self.addBugInterval_ - dt
if self.addBugInterval_ <= 0 then
self.addBugInterval_ = math.random(GameView.ADD_BUG_INTERVAL_MIN, GameView.ADD_BUG_INTERVAL_MAX)
self:addBug()
end

for _, bug in pairs(self.bugs_) do
bug:step(dt)
if bug:getModel():getDist() <= 0 then
self:bugEnterHole(bug)
end
end

return self
end

function GameView:getLives()
return self.lives_
end

function GameView:getKills()
return self.kills_
end

function GameView:addBug()
local bugType = BugBase.BUG_TYPE_ANT
if math.random(1, 2) % 2 == 0 then
bugType = BugBase.BUG_TYPE_SPIDER
end

local bugModel
if bugType == BugBase.BUG_TYPE_ANT then
bugModel = BugAnt:create()
else
bugModel = BugSpider:create()
end

local bug = BugSprite:create(GameView.IMAGE_FILENAMES[bugType], bugModel)
:start(GameView.HOLE_POSITION)
:addTo(self.bugsNode_, GameView.ZORDER_BUG)

self.bugs_[bug] = bug
return self
end

function GameView:bugEnterHole(bug)
self.bugs_[bug] = nil

bug:fadeOut({time = 0.5, removeSelf = true})
:scaleTo({time = 0.5, scale = 0.3})
:rotateTo({time = 0.5, rotation = math.random(360, 720)})

self.lives_ = self.lives_ - 1
self.livesLabel_:setString(self.lives_)
audio.playSound("BugEnterHole.wav")

if self.lives_ <= 0 then
self:dispatchEvent({name = GameView.events.PLAYER_DEAD_EVENT})
end

return self
end

function GameView:bugDead(bug)
local imageFilename = GameView.IMAGE_FILENAMES[bug:getModel():getType()]
DeadBugSprite:create(imageFilename)
:fadeOut({time = 2.0, delay = 0.5, removeSelf = true})
:move(bug:getPosition())
:rotate(bug:getRotation() + 120)
:addTo(self.bugsNode_, GameView.ZORDER_DEAD_BUG)

self.bugs_[bug] = nil
bug:removeSelf()

self.kills_ = self.kills_ + 1
audio.playSound("BugDead.wav")

return self
end

function GameView:onCreate()
self.lives_ = GameView.INIT_LIVES
self.kills_ = 0
self.bugs_ = {}
self.addBugInterval_ = 0

-- add touch layer
display.newLayer()
:onTouch(handler(self, self.onTouch))
:addTo(self)

-- add background image
display.newSprite("PlaySceneBg.jpg")
:move(display.center)
:addTo(self)

-- add bugs node
self.bugsNode_ = display.newNode():addTo(self)

-- add lives icon and label
display.newSprite("Star.png")
:move(display.left + 50, display.top - 50)
:addTo(self)
self.livesLabel_ = cc.Label:createWithSystemFont(self.lives_, "Arial", 32)
:move(display.left + 90, display.top - 50)
:addTo(self)

-- create animation for bugs
for bugType, filename in pairs(GameView.IMAGE_FILENAMES) do
-- load image
local texture = display.loadImage(filename)
local frameWidth = texture:getPixelsWide() / 3
local frameHeight = texture:getPixelsHigh()

-- create sprite frame based on image
local frames = {}
for i = 0, 1 do
local frame = display.newSpriteFrame(texture, cc.rect(frameWidth * i, 0, frameWidth, frameHeight))
frames[#frames + 1] = frame
end

-- create animation
local animation = display.newAnimation(frames, GameView.BUG_ANIMATION_TIMES[bugType])
-- caching animation
display.setAnimationCache(filename, animation)
end

-- bind the "event" component
cc.bind(self, "event")
end

function GameView:onTouch(event)
if event.name ~= "began" then return end
local x, y = event.x, event.y
for _, bug in pairs(self.bugs_) do
if bug:getModel():checkTouch(x, y) then
self:bugDead(bug)
end
end
end

function GameView:onCleanup()
self:removeAllEventListeners()
end

return GameView
start调用scheduleUpdate开启一个定时器,绑定每帧刷新时的回调函数:step,回调函数的参数为逝去的时间,step每次随机一两秒的数字,然后每次减去帧的逝去时间,当为0时调用addBug添加虫子,其实意义就是每隔一两秒添加一个虫子。并判断所有虫子的坐标是否在中心洞中,如果是调用bugEnterHole,该函数播放虫子进洞效果动画和音效,减少血量值,当血量小于等于0时分发游戏结束事件。

关于虫子的操作稍后再看,继续看GameView,onCreate中初始化血量,杀死虫子数等数据,setAnimationCache初始化虫子的播放动画。创建点击回调onTouch,当点击的坐标命中到某虫子时表示杀死该虫子。

添加虫子:
function GameView:addBug()
local bugType = BugBase.BUG_TYPE_ANT
if math.random(1, 2) % 2 == 0 then
bugType = BugBase.BUG_TYPE_SPIDER
end

local bugModel
if bugType == BugBase.BUG_TYPE_ANT then
bugModel = BugAnt:create()
else
bugModel = BugSpider:create()
end

local bug = BugSprite:create(GameView.IMAGE_FILENAMES[bugType], bugModel)
:start(GameView.HOLE_POSITION)
:addTo(self.bugsNode_, GameView.ZORDER_BUG)

self.bugs_[bug] = bug
return self
end
蚂蚁和蜘蛛均派生自BugBase,随机产生蚂蚁和蜘蛛,创建虫子对象后再创建虫子精灵调用start开始移动虫子并播放虫子移动时候的动画,下一节主要讲解虫子对象和虫子精灵。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/羊村懒王/article/detail/652432
推荐阅读
相关标签
  

闽ICP备14008679号