加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-14 21:43:08 所属栏目:百科 来源:网络整理
导读:接上, PlayScene场景创建游戏逻辑视图GameView并调用start函数开始游戏: -- GameView is a combination of view and controller local GameView = class ( "GameView" , cc . load ( "mvc" ). ViewBase ) BugBase = import ( "..models.BugBase" ) BugAnt
接上, 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开始移动虫子并播放虫子移动时候的动画,下一节主要讲解虫子对象和虫子精灵。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读