cocos2d-x的初步学习二十之坦克大战七

到目前为止,我们的坦克已经可以移动,并且已经做了碰撞检测。现在,我们要发射子弹了哦。。。。首先,我们在我们的控制层添加一个虚拟按钮,来发射子弹,看代码:

ControlLayer.h

 //按钮
    SneakyButton* fireButton;

    //添加发射按钮
    void addFireButton(void);

ControlLayer.cpp

void ControlLayer::addFireButton()
{

    float buttonRadius = 160;

    fireButton=new SneakyButton();
    fireButton->initWithRect(CCRectZero);
    fireButton->autorelease();
    fireButton->setIsToggleable(false);
    fireButton->setIsHoldable(true);
    

    SneakyButtonSkinnedBase *skinFireButton=new SneakyButtonSkinnedBase();
    skinFireButton->autorelease();
    skinFireButton->init();

    CCSprite *normalSprite=CCSprite::create("fire_button_default.png");
    normalSprite->setOpacity(100);

    CCSprite *pressedSprite=CCSprite::create("fire_button_press.png");
    pressedSprite->setOpacity(100);

    skinFireButton->setPosition(ccp(wSize.width-buttonRadius/2, buttonRadius/2));
    skinFireButton->setDefaultSprite(normalSprite);
    skinFireButton->setPressSprite(pressedSprite);
    //skinFireButton->getButton()->setStatus(true);
    //注意,一定要加激活状态的,不然会报错
    skinFireButton->setActivatedSprite(CCSprite::create("fire_button_press.png"));
    skinFireButton->setButton(fireButton);
    this->addChild(skinFireButton);
    
    
   
}

在update函数中加入

if (fireButton->getIsActive()) {
        
        tank->onFire();
    }

这样,我们就在控制层加入了一个虚拟按钮,注意,跟2d版的不一样,需要添加激活状态,并且active这个属性是只读的,不可修改。OK,然后我们在TankSprite类中,加入开火的函数,看代码:

TankSprite.h

 void onFire(void);
    
    void fire(CCSprite *buttle, TankAction buttleOrientation);
    
    MapLayer *_mapLayer;
    
    cocos2d::CCTMXTiledMap *gameMap;

bool _isCanFire;

    //子弹方向
    int _buttleOrientation;

    void makeCanFire(void);

TankSprite.cpp

//开火
void TankSprite::onFire()
{

    if (_life==0) {
        
        return;
    }
    
    
    //当tank只能发射一颗子弹的时候
    if (kind==kBorn || kind==kPlusStarOne) {
        
        //能否继续开火
        if (_isCanFire==false) {
            
            return;
        }
        //子弹方向即坦克方向
        _buttleOrientation=kaction;
        
        CCSprite *buttle=CCSprite::createWithSpriteFrameName("bullet.png");
        
        gameMap->addChild(buttle, -1);
        
        //隐藏
        buttle->setVisible(false);
        
        _isCanFire=false;
        
        this->fire(buttle, (TankAction)_buttleOrientation);
        
        
    }

}


//根据传入的子弹,子弹方向来做判断
void TankSprite::fire(CCSprite *buttle, TankAction buttleOrientation)
{

    //发射子弹的声音
    SimpleAudioEngine::sharedEngine()->playEffect("bullet.aif");

    CCPoint ptn;
    
    switch (buttleOrientation) {
        case kUp:
            
            CCLOG("%f",this->getPosition().x);
            
            buttle->setRotation(0);
            
            buttle->setPosition(ccp(this->getPosition().x, this->getPosition().y+this->boundingBox().size.height/2));
           
            //终点
            ptn=ccp(this->getPosition().x, this->getPosition().y+_mapSize.width);
            
            break;
        case kDown:
            
            buttle->setRotation(180);
            
            buttle->setPosition(ccp(this->getPosition().x, this->getPosition().y-this->boundingBox().size.height/2));
            
            
            ptn=ccp(this->getPosition().x, this->getPosition().y-_mapSize.width);
            
            break;
        case kRight:
            
            buttle->setRotation(90);
            
            buttle->setPosition(ccp(this->getPosition().x+this->boundingBox().size.width/2, this->getPosition().y));
            
            
            ptn=ccp(this->getPosition().x+_mapSize.width, this->getPosition().y);
            
            break;
        case kLeft:
            
            buttle->setRotation(-90);
            
            buttle->setPosition(ccp(this->getPosition().x-this->boundingBox().size.width/2, this->getPosition().y));
            
            
            ptn=ccp(this->getPosition().x-_mapSize.width, this->getPosition().y);
            
            break;
            
            
        default:
            break;
    }
    

    const CCPoint realyPosition = ptn;

    //创建子弹移动动画
    CCShow *ac1=CCShow::create();
    CCMoveTo *ac2=CCMoveTo::create(2.0, realyPosition);

    CCFiniteTimeAction *seq=CCSequence::create(ac1,ac2,CCCallFunc::create(this, callfunc_selector(TankSprite::makeCanFire)),NULL);

    buttle->runAction(seq);
    
    
    
    
    
}

void TankSprite::makeCanFire()
{
    CCLOG("lll");
    
    _isCanFire=true;


}

OK,这样,就简单的加入了发射子弹的功能,

cocos2d-x的初步学习二十之坦克大战七_第1张图片

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

子弹虽然加入了,但我们发现它其实是可以穿透墙的,我们并没有对其做碰撞检测。



你可能感兴趣的:(cocos2dx学习)