cocos2d-x 3.0游戏开发xcode5帅印博客教学 006.[HoldTail]子弹的创建和子弹与敌人的碰撞

上篇帖子写了敌人的创建,让我们的游戏感觉活泼了很多,今天要做的就是子弹的建立,子弹的建立其实跟敌人的建立差不多,只是一个是从左往右,一个是从右往左。

效果图



直接上代码,如果上面一篇文章你理解了,那么这一篇文章你也一定会理解的

SWGameBullet.h  和 SWGameBullet.cpp

SWGameBullet.h

//
//  SWGameBullet.h
//  Holdtail
//
//  Created by 帅 印 on 13-12-31.
//
//

#ifndef __Holdtail__SWGameBullet__
#define __Holdtail__SWGameBullet__Holdtail

#include <iostream>
#include "cocos2d.h"

using namespace cocos2d;

class SWGameBullet : public cocos2d::Sprite{
public:
    static SWGameBullet *createBullet(const char *_fileName,float _speed,Point _postion);
    
private:
    //初始化函数
    void bulletInit(float _speed,Point _posion);
    void update(float time);
    float speed;
    
    virtual void onExit();
    
    //是否已经运动
    bool isActed;
};
#endif /* defined(__Holdtail__SWGameBullet__) */


SWGameBullet.cpp

//
//  SWGameBullet.cpp
//  Holdtail
//
//  Created by 帅 印 on 13-12-31.
//
//

#include "SWGameBullet.h"
#include "SWGameEnemyPIG.h"
#include "SWGameWorld.h"
#include "cocos2d.h"
#include "SimpleAudioEngine.h"

using namespace CocosDenshion;

SWGameBullet * SWGameBullet::createBullet(const char *_fileName, float _speed, cocos2d::Point _postion){
    SWGameBullet *bullet = new SWGameBullet();
    if(bullet && bullet->initWithFile(_fileName)){
        bullet->autorelease();
        bullet->bulletInit(_speed, _postion);
        return bullet;
    }
    
    CC_SAFE_DELETE(bullet);
    return NULL;
}

void SWGameBullet::bulletInit(float _speed, cocos2d::Point _posion){
    speed = _speed;
    this->setPosition(_posion);
    this->scheduleUpdate();
}

//生命周期
void SWGameBullet::onExit(){
    this->unscheduleUpdate();
    CCSprite::onExit();
}

//子弹逻辑
void SWGameBullet::update(float time){
        //删除
        if(this->getPositionX()>1500){
            //从父类删除当前子弹
            SWGameWorld::sharedWorld()->getArrayForBullet()->removeObject(this);
            this->getParent()->removeChild(this,true);
        }
        isActed = true;
        //子弹向又运动
        this->setPosition(Point(this->getPosition() + Point(speed, 0)));
        Array *array = SWGameWorld::sharedWorld()->getArrayForEnemy();
        for (int i = 0; i<array->count(); i++) {
            SWGameEnemyPIG *enemy = (SWGameEnemyPIG *)array->getObjectAtIndex(i);
            
            if(enemy->boundingBox().intersectsRect(this->boundingBox())){
                //爆炸粒子效果
                ParticleSystemQuad *particle = CCParticleSystemQuad::create("particle_boom.plist");
                particle->setPosition(enemy->getPosition());
                particle->setAutoRemoveOnFinish(true);
                SWGameWorld::sharedWorld()->addChild(particle);
                
                //增加分数
                //增加杀人数
                //从敌人的数组将被攻击的敌怪删除
                array->removeObject(enemy);
                
                //删除子弹与敌怪
                SWGameWorld::sharedWorld()->removeChild(enemy,true);
                
                SWGameWorld::sharedWorld()->getArrayForBullet()->removeObject(this);
                SWGameWorld::sharedWorld()->removeChild(this,true);
                
            }
        }
}


下面是下载源码的地址-->飞机直达


  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

shuaiyinoo

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值