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

前端之家收集整理的这篇文章主要介绍了4、cocos2d-Lua的demo--游戏逻辑GameView前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
接上,@H_502_1@ PlayScene场景创建游戏逻辑视图GameView并调用start函数开始游戏:@H_502_1@
-- GameView is a combination of view and controller
@H_502_1@local @H_502_1@GameView @H_502_1@= class@H_502_1@("GameView"@H_502_1@,cc@H_502_1@.load@H_502_1@("mvc"@H_502_1@).ViewBase@H_502_1@)

BugBase @H_502_1@= import@H_502_1@("..models.BugBase"@H_502_1@)
BugAnt @H_502_1@= "..models.BugAnt"@H_502_1@)
BugSpider @H_502_1@= "..models.BugSpider"@H_502_1@)

BugSprite @H_502_1@= ".BugSprite"@H_502_1@)
DeadBugSprite @H_502_1@= ".DeadBugSprite"@H_502_1@)

GameView@H_502_1@.HOLE_POSITION @H_502_1@= p@H_502_1@(display@H_502_1@.cx @H_502_1@- 30@H_502_1@,43)">cy @H_502_1@- 75@H_502_1@)
INIT_LIVES @H_502_1@= 99999
@H_502_1@ADD_BUG_INTERVAL_MIN @H_502_1@= 1
@H_502_1@ADD_BUG_INTERVAL_MAX @H_502_1@= 3
@H_502_1@
@H_502_1@IMAGE_FILENAMES @H_502_1@= {}
IMAGE_FILENAMES@H_502_1@[BugBase@H_502_1@.BUG_TYPE_ANT@H_502_1@] = "BugAnt.png"
@H_502_1@BUG_TYPE_SPIDER@H_502_1@] = "BugSpider.png"
@H_502_1@
@H_502_1@BUG_ANIMATION_TIMES @H_502_1@= {}
BUG_ANIMATION_TIMES@H_502_1@[0.15
@H_502_1@0.1
@H_502_1@ZORDER_BUG @H_502_1@= 100
@H_502_1@ZORDER_DEAD_BUG @H_502_1@= 50
@H_502_1@events @H_502_1@= {
PLAYER_DEAD_EVENT @H_502_1@= "PLAYER_DEAD_EVENT"@H_502_1@,
}

function @H_502_1@GameView@H_502_1@:start@H_502_1@()
self@H_502_1@:scheduleUpdate@H_502_1@(handler@H_502_1@(self@H_502_1@,self@H_502_1@.step@H_502_1@))
return @H_502_1@self
@H_502_1@end
@H_502_1@
@H_502_1@stop@H_502_1@()
unscheduleUpdate@H_502_1@()
step@H_502_1@(dt@H_502_1@)
if @H_502_1@lives_ @H_502_1@<= 0 @H_502_1@then return end
@H_502_1@@H_502_1@addBugInterval_ @H_502_1@= addBugInterval_ @H_502_1@- dt
@H_502_1@@H_502_1@addBugInterval_ @H_502_1@<= then
@H_502_1@addBugInterval_ @H_502_1@= math@H_502_1@.random@H_502_1@(GameView@H_502_1@.ADD_BUG_INTERVAL_MIN@H_502_1@,GameView@H_502_1@.ADD_BUG_INTERVAL_MAX@H_502_1@)
addBug@H_502_1@()
for @H_502_1@_@H_502_1@,bug @H_502_1@in @H_502_1@pairs@H_502_1@(bugs_@H_502_1@) do
@H_502_1@@H_502_1@bug@H_502_1@:if @H_502_1@getModel@H_502_1@():getDist@H_502_1@() <= bugEnterHole@H_502_1@(bug@H_502_1@)
end
@H_502_1@ return @H_502_1@getLives@H_502_1@()
lives_
@H_502_1@getKills@H_502_1@()
kills_
@H_502_1@bugType @H_502_1@= BugBase@H_502_1@.BUG_TYPE_ANT
@H_502_1@@H_502_1@if @H_502_1@random@H_502_1@(1@H_502_1@,255)">2@H_502_1@) % 2 @H_502_1@== BUG_TYPE_SPIDER
@H_502_1@ local @H_502_1@bugModel
@H_502_1@@H_502_1@bugType @H_502_1@== BugBase@H_502_1@.BUG_TYPE_ANT @H_502_1@bugModel @H_502_1@= BugAnt@H_502_1@:create@H_502_1@()
else
@H_502_1@bugModel @H_502_1@= BugSpider@H_502_1@:bug @H_502_1@= BugSprite@H_502_1@:create@H_502_1@(GameView@H_502_1@.bugType@H_502_1@],153)">bugModel@H_502_1@)
:start@H_502_1@(GameView@H_502_1@.HOLE_POSITION@H_502_1@)
:addTo@H_502_1@(bugsNode_@H_502_1@,43)">ZORDER_BUG@H_502_1@)

bugs_@H_502_1@[bug@H_502_1@] = bug
@H_502_1@bugEnterHole@H_502_1@(bug@H_502_1@)
bugs_@H_502_1@[bug@H_502_1@] = nil
@H_502_1@
@H_502_1@@H_502_1@bug@H_502_1@:fadeOut@H_502_1@({time @H_502_1@= 0.5@H_502_1@,removeSelf @H_502_1@= true@H_502_1@})
:scaleTo@H_502_1@({scale @H_502_1@= 0.3@H_502_1@})
:rotateTo@H_502_1@({rotation @H_502_1@= 360@H_502_1@,255)">720@H_502_1@)})

lives_ @H_502_1@= lives_ @H_502_1@- @H_502_1@livesLabel_@H_502_1@:setString@H_502_1@(lives_@H_502_1@)
audio@H_502_1@.playSound@H_502_1@("BugEnterHole.wav"@H_502_1@)

dispatchEvent@H_502_1@({name @H_502_1@= GameView@H_502_1@.events@H_502_1@.PLAYER_DEAD_EVENT@H_502_1@})
bugDead@H_502_1@(bug@H_502_1@)
imageFilename @H_502_1@= GameView@H_502_1@.IMAGE_FILENAMES@H_502_1@[getType@H_502_1@()]
DeadBugSprite@H_502_1@:create@H_502_1@(imageFilename@H_502_1@)
:2.0@H_502_1@,43)">delay @H_502_1@= move@H_502_1@(getPosition@H_502_1@())
:rotate@H_502_1@(getRotation@H_502_1@() + 120@H_502_1@)
:ZORDER_DEAD_BUG@H_502_1@)

removeSelf@H_502_1@()

kills_ @H_502_1@= kills_ @H_502_1@+ @H_502_1@"BugDead.wav"@H_502_1@)

onCreate@H_502_1@()
lives_ @H_502_1@= GameView@H_502_1@.INIT_LIVES
@H_502_1@@H_502_1@kills_ @H_502_1@= 0
@H_502_1@bugs_ @H_502_1@= {}
addBugInterval_ @H_502_1@= -- add touch layer
@H_502_1@@H_502_1@newLayer@H_502_1@()
:onTouch@H_502_1@(onTouch@H_502_1@))
:self@H_502_1@)

-- add background image
@H_502_1@newSprite@H_502_1@("PlaySceneBg.jpg"@H_502_1@)
:move@H_502_1@(center@H_502_1@)
:-- add bugs node
@H_502_1@@H_502_1@bugsNode_ @H_502_1@= newNode@H_502_1@():-- add lives icon and label
@H_502_1@"Star.png"@H_502_1@)
:left @H_502_1@+ 50@H_502_1@,43)">top @H_502_1@- 50@H_502_1@)
:self@H_502_1@)
livesLabel_ @H_502_1@= Label@H_502_1@:createWithSystemFont@H_502_1@(lives_@H_502_1@,0)">"Arial"@H_502_1@,255)">32@H_502_1@)
:90@H_502_1@,128)">-- create animation for bugs
@H_502_1@@H_502_1@for @H_502_1@bugType@H_502_1@,153)">filename @H_502_1@pairs@H_502_1@(GameView@H_502_1@.IMAGE_FILENAMES@H_502_1@) @H_502_1@-- load image
@H_502_1@texture @H_502_1@= loadImage@H_502_1@(filename@H_502_1@)
frameWidth @H_502_1@= texture@H_502_1@:getPixelsWide@H_502_1@() / frameHeight @H_502_1@= getPixelsHigh@H_502_1@()

-- create sprite frame based on image
@H_502_1@frames @H_502_1@= {}
i @H_502_1@= 0@H_502_1@,255)">1 @H_502_1@frame @H_502_1@= newSpriteFrame@H_502_1@(texture@H_502_1@,43)">rect@H_502_1@(frameWidth @H_502_1@* i@H_502_1@,153)">frameWidth@H_502_1@,153)">frameHeight@H_502_1@))
frames@H_502_1@[#frames @H_502_1@+ 1@H_502_1@] = frame
@H_502_1@-- create animation
@H_502_1@animation @H_502_1@= newAnimation@H_502_1@(frames@H_502_1@,153)">bugType@H_502_1@])
-- caching animation
@H_502_1@setAnimationCache@H_502_1@(filename@H_502_1@,153)">animation@H_502_1@)
-- bind the "event" component
@H_502_1@bind@H_502_1@("event"@H_502_1@)
onTouch@H_502_1@(event@H_502_1@)
event@H_502_1@.name @H_502_1@~= "began" @H_502_1@x@H_502_1@,153)">y @H_502_1@= x@H_502_1@,43)">y
@H_502_1@ if @H_502_1@checkTouch@H_502_1@(y@H_502_1@) bugDead@H_502_1@(onCleanup@H_502_1@()
removeAllEventListeners@H_502_1@()
return @H_502_1@GameView
@H_502_1@
start调用scheduleUpdate开启一个定时器,绑定每帧刷新时的回调函数:step,回调函数的参数为逝去的时间,step每次随机一两秒的数字,然后每次减去帧的逝去时间,当为0时调用addBug添加虫子,其实意义就是每隔一两秒添加一个虫子。并判断所有虫子的坐标是否在中心洞中,如果是调用bugEnterHole,该函数播放虫子进洞效果动画和音效,减少血量值,当血量小于等于0时分发游戏结束事件。

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

添加虫子:
end@H_502_1@
蚂蚁和蜘蛛均派生自BugBase,随机产生蚂蚁和蜘蛛,创建虫子对象后再创建虫子精灵调用start开始移动虫子并播放虫子移动时候的动画,下一节主要讲解虫子对象和虫子精灵。
原文链接:https://www.f2er.com/cocos2dx/342311.html

猜你在找的Cocos2d-x相关文章