4、cocos2d-Lua的demo--游戏逻辑GameView

4、cocos2d-Lua的demo--游戏逻辑GameView,第1张

概述接上, PlayScene场景创建游戏逻辑视图GameView并调用start函数开始游戏: -- GameView is a combination of view and controller local GameView = class("GameView", cc.load("mvc").ViewBase) local BugBase = import("..models.BugBas 接上, PlayScene场景创建游戏逻辑视图GameVIEw并调用start函数开始游戏:
-- GameVIEw is a combination of vIEw and controller
local GameVIEw = class("GameVIEw",cc.load("mvc").VIEwBase)

BUGBase = import("..models.BUGBase")
BUGAnt = "..models.BUGAnt")
BUGSpIDer = "..models.BUGSpIDer")

BUGSprite = ".BUGSprite")
DeadBUGSprite = ".DeadBUGSprite")

GameVIEw.HolE_position = p(display.cx - 30,43)">cy - 75)
INIT_liVES = 99999
ADD_BUG_INTERVAL_MIN = 1
ADD_BUG_INTERVAL_MAX = 3

IMAGE_filenameS = {}
IMAGE_filenameS[BUGBase.BUG_TYPE_ANT] = "BUGAnt.png"
BUG_TYPE_SPIDER] = "BUGSpIDer.png"

BUG_ANIMATION_TIMES = {}
BUG_ANIMATION_TIMES[0.15
0.1
ZORDER_BUG = 100
ZORDER_DEAD_BUG = 50
events = {
PLAYER_DEAD_EVENT = "PLAYER_DEAD_EVENT",
}

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

stop()
unscheduleUpdate()
step(dt)
if lives_ <= 0 then return end
addBUGInterval_ = addBUGInterval_ - dt
addBUGInterval_ <= then
addBUGInterval_ = math.random(GameVIEw.ADD_BUG_INTERVAL_MIN,GameVIEw.ADD_BUG_INTERVAL_MAX)
addBUG()
for _,BUG in pairs(BUGs_) do
BUG:if getModel():getdist() <= BUGEnterHole(BUG)
end
return getlives()
lives_
getKills()
kills_
BUGType = BUGBase.BUG_TYPE_ANT
if random(1,255)">2) % 2 == BUG_TYPE_SPIDER
local BUGModel
BUGType == BUGBase.BUG_TYPE_ANT BUGModel = BUGAnt:create()
else
BUGModel = BUGSpIDer:BUG = BUGSprite:create(GameVIEw.BUGType],153)">BUGModel)
:start(GameVIEw.HolE_position)
:addTo(BUGsNode_,43)">ZORDER_BUG)

BUGs_[BUG] = BUG
BUGEnterHole(BUG)
BUGs_[BUG] = nil

BUG:fadeOut({time = 0.5,removeSelf = true})
:scaleto({scale = 0.3})
:rotateto({rotation = 360,255)">720)})

lives_ = lives_ - livesLabel_:setString(lives_)
audio.playSound("BUGEnterHole.wav")

dispatchEvent({name = GameVIEw.events.PLAYER_DEAD_EVENT})
BUGDead(BUG)
imagefilename = GameVIEw.IMAGE_filenameS[getType()]
DeadBUGSprite:create(imagefilename)
:2.0,43)">delay = move(getposition())
:rotate(getRotation() + 120)
:ZORDER_DEAD_BUG)

removeSelf()

kills_ = kills_ + "BUGDead.wav")

onCreate()
lives_ = GameVIEw.INIT_liVES
kills_ = 0
BUGs_ = {}
addBUGInterval_ = -- add touch layer
newLayer()
:ontouch(ontouch))
:self)

-- add background image
newSprite("PlaySceneBg.jpg")
:move(center)
:-- add BUGs node
BUGsNode_ = newNode():-- add lives icon and label
"Star.png")
:left + 50,43)">top - 50)
:self)
livesLabel_ = Label:createWithSystemFont(lives_,0)">"Arial",255)">32)
:90,128)">-- create animation for BUGs
for BUGType,153)">filename pairs(GameVIEw.IMAGE_filenameS) -- load image
texture = loadImage(filename)
frameWIDth = texture:getPixelsWIDe() / frameHeight = getPixelsHigh()

-- create sprite frame based on image
frames = {}
i = 0,255)">1 frame = newSpriteFrame(texture,43)">rect(frameWIDth * i,153)">frameWIDth,153)">frameHeight))
frames[#frames + 1] = frame
-- create animation
animation = newAnimation(frames,153)">BUGType])
-- caching animation
setAnimationCache(filename,153)">animation)
-- bind the "event" component
bind("event")
ontouch(event)
event.name ~= "began" x,153)">y = x,43)">y
if checktouch(y) BUGDead(onCleanup()
removeAllEventListeners()
return GameVIEw
start调用scheduleUpdate开启一个定时器,绑定每帧刷新时的回调函数:step,回调函数的参数为逝去的时间,step每次随机一两秒的数字,然后每次减去帧的逝去时间,当为0时调用addBUG添加虫子,其实意义就是每隔一两秒添加一个虫子。并判断所有虫子的坐标是否在中心洞中,如果是调用BUGEnterHole,该函数播放虫子进洞效果动画和音效,减少血量值,当血量小于等于0时分发游戏结束事件。
关于虫子的 *** 作稍后再看,继续看GameVIEw,onCreate中初始化血量,杀死虫子数等数据,setAnimationCache初始化虫子的播放动画。创建点击回调ontouch,当点击的坐标命中到某虫子时表示杀死该虫子。
添加虫子:
end
蚂蚁和蜘蛛均派生自BUGBase,随机产生蚂蚁和蜘蛛,创建虫子对象后再创建虫子精灵调用start开始移动虫子并播放虫子移动时候的动画,下一节主要讲解虫子对象和虫子精灵。 总结

以上是内存溢出为你收集整理的4、cocos2d-Lua的demo--游戏逻辑GameView全部内容,希望文章能够帮你解决4、cocos2d-Lua的demo--游戏逻辑GameView所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1063876.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-26
下一篇 2022-05-26

发表评论

登录后才能评论

评论列表(0条)

保存