在飞机大战中,飞机一直是向前飞,根据相当论来说只要背景向后滚动,那飞机就保持向前飞的。只需用两张背景图片一直滚动就可以实现视觉上飞机飞起来的感觉。
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)