项目飞机大战01:主要实现背景的移动,飞机的动画和触控,子弹的生成和发射

#include "Game.h"

USING_NS_CC;

using namespace cocos2d;

CCScene *GameScene::scene(){

    

   CCScene *scene=CCScene::create();

    GameScene *layer=GameScene::create();

    scene->addChild(layer);

    

   return scene;

    

}

boolGameScene::init(){

   if (!CCLayer::init()) {

        return false;

    }

    this->setTouchEnabled(true);

    

    // 添加背景

   CCSprite *spritebeijing=CCSprite::create("background4.png");

   this->addChild(spritebeijing);

    spritebeijing->setTag(101);

    spritebeijing->setAnchorPoint(ccp(0,0));

    

   CCSprite *spritebeijing02=CCSprite::create("background4.png");

   this->addChild(spritebeijing02);

    spritebeijing02->setTag(102);

    spritebeijing02->setAnchorPoint(ccp(0,0));

    spritebeijing02->setPositionY(spritebeijing->getContentSize().height);

    

    //添加飞机

   CCSprite *planeSprite=CCSprite::create("player.png");

    planeSprite->setPosition(ccp(160,240));

   this->addChild(planeSprite);

    planeSprite->setTag(103);

    

    //定义纹理对象

    CCTexture2D *texture=CCTextureCache::sharedTextureCache()->addImage("player.png");

    

    //获取飞机尺寸

   float spriteW=planeSprite->getContentSize().width/4;

   float spriteH=planeSprite->getContentSize().height;

    

    //创建飞机动画

    CCAnimation *animation=CCAnimation::create();

    animation->setDelayPerUnit(0.08f);//设置动画的间隔时间

    

   //获取

   for (int i=0; i<4; i++) {

        

        animation->addSpriteFrameWithTexture(texture,CCRectMake(i*spriteW, 0, spriteW, spriteH));

    }

   

    //飞机动画

    planeSprite->runAction(CCRepeatForever::create(CCAnimate::create(animation)));

    

    

    //添加子弹

    allBullet=CCArray::create();//创建子弹容器

   allBullet->retain();

    

    //背景图移动定时器

    this->schedule(schedule_selector(GameScene::moveBeijing),0.03);

    //产生子弹的定时器

    this->schedule(schedule_selector(GameScene::newBullet),0.1);

    //移动子弹的定时器

    this->schedule(schedule_selector(GameScene::moveBullet),0.001 );

    return true;

    

}

voidGameScene::newBullet(float t){

    //产生子弹

   CCPoint Bpoint;

    //得到飞机的位置

   CCSprite *spriteP=(CCSprite *)this->getChildByTag(103);

    Bpoint.x=spriteP->getPositionX();

    Bpoint.y=spriteP->getPositionY();

   CCSprite *spriteBullet=CCSprite::create("bullet2.png");

    spriteBullet->setPosition(Bpoint);

   this->addChild(spriteBullet);

    //添加到集合

   this->allBullet->addObject(spriteBullet);

    

}

voidGameScene::moveBullet(float t){

   

   for (int i=0; i<allBullet->count(); i++) {

        //获取第i颗子弹

       CCSprite *nowBullet=(CCSprite *)allBullet->objectAtIndex(i);

        nowBullet->setPositionY(nowBullet->getPositionY()+6);

       if (nowBullet->getPositionY()>480) {

           //从当前图层移除

           this->removeChild(nowBullet);

           allBullet->removeObject(nowBullet);

            i--;

        }

        

    }

}

 voidGameScene:: registerWithTouchDispatcher(void){

     CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(this,1, false);

}

 boolGameScene::ccTouchBegan(CCTouch *pTouch,CCEvent *pEvent){

    

     return true;

}

 voidGameScene::ccTouchMoved(CCTouch *pTouch,CCEvent *pEvent){

     

    CCPoint p=pTouch->getLocation();

    CCSprite *planSprite=(CCSprite *)this->getChildByTag(103);

     planSprite->setPosition(p);

     

}

 voidGameScene::ccTouchEnded(CCTouch *pTouch,CCEvent *pEvent){

    

}

//移动背景

voidGameScene::moveBeijing(float t)

{

   CCSprite *spriteBJ=(CCSprite *)this->getChildByTag(101);

    spriteBJ->setPositionY(spriteBJ->getPositionY()-1);

    


   CCSprite *spriteBJ02=(CCSprite *)this->getChildByTag(102);

    spriteBJ02->setPositionY(spriteBJ02->getPositionY()-1);

   if (spriteBJ->getPositionY()<-(spriteBJ->getContentSize().height))

    {

        spriteBJ->setPositionY(0);

        spriteBJ02->setPositionY(spriteBJ->getContentSize().height);

    }

    

}


你可能感兴趣的:(cocos2d-x)