cocos2d-x(十一)Lua开发飞机大战-6-添加子弹

来源:互联网 发布:安卓魔音通话变声软件 编辑:程序博客网 时间:2024/05/16 07:18


接下来我们为飞机添加子弹,首先创建一个BulletLayer:

module("BulletLayer",package.seeall) local bulletBatchNode = nillocal plane = nillocal bulletArray = {}local bulletLayer = nilfunction create()bulletLayer = CCLayer:create()bulletBatchNode = CCSpriteBatchNode:create("Images/shoot.png")bulletLayer:addChild(bulletBatchNode);return bulletLayerend

 

先解释一下定义的那几个变量吧。

bulletBatchNode 是用来管理子弹精灵的,对于CCSpriteBatchNode不太熟悉,请自己去查查资料。

plane是指我们的主角,在设置子弹位置的时候需要飞机的大小。

bulletArray是用来存放子弹的一个table,在做实体碰撞的时候会用上。

现在来创建一个子弹吧

function addBullet()local planeX = plane:getPositionX()local planeY = plane:getPositionY()local bullet = CCSprite:createWithSpriteFrameName("bullet1.png")local bulletPostion = ccp(planeX,planeY + plane:getContentSize().height / 2)bullet:setPosition(bulletPostion)bulletBatchNode:addChild(bullet)local length = visibleSize.height + bullet:getContentSize().height / 2 - bulletPostion.y;local velocity = 250 --飞行速度local moveTime = length/velocitylocal actionMove = CCMoveTo:create(moveTime,ccp(bulletPostion.x,visibleSize.height  + bullet:getContentSize().height / 2))local actionDone = CCCallFuncN:create(removeBullet)local sequence = CCSequence:createWithTwoActions(actionMove,actionDone)bullet:runAction(sequence)table.insert(bulletArray,bullet)end

 子弹有了,图层有了,如何会将子弹不断的添加到图层上呢?当然是用scheduler。

local addBulletEntry = nilfunction stratOneShoot()if addDoulbeBulletEntry ~= nil thenstopDoubleShoot()endaddBulletEntry = CCDirector:sharedDirector():getScheduler():scheduleScriptFunc(addBullet, 0.2,false)end


有了开始射击,自然应该有停止射击

function stopOneShoot()if addBulletEntry ~= nil thenCCDirector:sharedDirector():getScheduler():unscheduleScriptEntry(addBulletEntry)addBulletEntry = nilendend

 

好了,现在是不是看见子弹满天飞了。 


 

0 0
原创粉丝点击