Cocos2d-x   发布时间:2022-05-03  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了4、cocos2d-Lua的demo--游戏逻辑GameView大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
接上, 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_ = @H_241_17@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 = @H_408_90@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")
:@H_74_31@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所遇到的程序开发问题。

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

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。