您的位置:首页 > 移动开发 > Cocos引擎

cocos2d-x笔记(九)Lua开发飞机大战-3-背景图的滚动

2014-04-17 10:18 281 查看
在飞机大战中,飞机一直是向前飞,根据相当论来说只要背景向后滚动,那飞机就保持向前飞的。只需用两张背景图片一直滚动就可以实现视觉上飞机飞起来的感觉。



function create()    gameLayer = CCLayer:create()
local background1 = CCSprite:createWithSpriteFrameName("background.png")
background1:setAnchorPoint(ccp(0,0))
background1:setPosition(ccp(0,0))
gameLayer:addChild(background1)

local background2 = CCSprite:createWithSpriteFrameName("background.png")
background2:setAnchorPoint(ccp(0,0))
background2:setPosition(ccp(0,background1:getContentSize().height-2))
gameLayer:addChild(background2)


两个背景图片已经添加好,现在就让它们滚起来吧

---背景滚动
local function backgroundMove()
background1:setPositionY(background1:getPositionY()-2)
background2:setPositionY(background1:getPositionY() + background1:getContentSize().height - 2)
if background2:getPositionY() == 0 then
background1:setPositionY(0)
end
end
backgroundEntry = CCDirector:sharedDirector():getScheduler():scheduleScriptFunc(backgroundMove, 0.01,false)


 

backgroudMove是一个回调函数,将其注册到scheduler中,定时刷新滚动。backgroundEntry是一个标示,在移除这个定时器的会使用到这个参数。当游戏结束是会调用以下语句

CCDirector:sharedDirector():getScheduler():unscheduleScriptEntry(backgroundEntry)


 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  cocos2d-x Lua
相关文章推荐