cocos2d-x 3.2 |飞机大战:敌机与爆炸

前情提要:飞机大战第三篇 实现敌机与爆炸
如下: 新建类----->Enemy + Boom
第一步:敌机类
Enemy.h
#include <stdio.h>
#include "cocos2d.h"
USING_NS_CC;
class Enemy:public Node
{
public:
    CREATE_FUNC(Enemy);
    int type;
    int hp;
    int ex,ey;
    int toolID;//道具 0 不含 1 加血 2 炸雷 3 激光 4 散弹
    Sprite * eSprite;
    bool init();
    static Enemy * createEnemy(int type,int x,int y);
    void moveTo(int x,int y);
};
Enemy.cpp
#include "Enemy.h"


Enemy * Enemy::createEnemy(int type,int x,int y)
{
    Enemy * newE=Enemy::create();
    
    //随机百分比产生道具
    int num=random()%10;
    if (num<10) {
        newE->toolID=random()%5;
    }
    //产生敌机
    switch (type)
    {
            
        case 1:
            newE->eSprite=Sprite::create("plane_1.png");
            newE->hp=1;
            break;
        case 2:
            newE->eSprite=Sprite::create("plane_2.png");
            newE->hp=2;
            break;

            
    }
    newE->type=type;
    newE->addChild(newE->eSprite);
    newE->moveTo(x, y);
    return newE;
}
bool Enemy::init()
{
    if (!Node::init())
    {
        return false;
    }
    return true;
}
void Enemy::moveTo(int x,int y)
{
    this->eSprite->setPosition(x,y);
    this->ex=x;
    this->ey=y;
}

第二步:爆炸类(爆炸效果来自粒子编辑器,如需请自行下载设计后导出.plist)
Boom.h
#include <stdio.h>
#include "cocos2d.h"
USING_NS_CC;
class Boom:public Node
{
public:
    CREATE_FUNC(Boom);
    bool init();
    static Boom * createBoom(int type, int x,int y);
    void Killme();
};
Boom.cpp
#include "Boom.h"
Boom * Boom::createBoom(int type, int x,int y)
{
    Boom * newB=Boom::create();
    Sprite * sp=nullptr;
    //添加纹理动画
    switch (type) {
        case 1://第1种爆炸效果
        {
            auto particle=ParticleSystemQuad::create("Boom_1.plist");
            newB->addChild(particle);
            particle->setPosition(x,y);
            //action 序列
            auto act=Sequence::create(DelayTime::create(1),CallFunc::create(CC_CALLBACK_0(Boom::Killme, newB)), NULL);
            newB->runAction(act);
        }
            break;
        case 2://第2种爆炸效果
        {
            auto particle=ParticleSystemQuad::create("Boom_2.plist");
            newB->addChild(particle);
            particle->setPosition(x,y);
            //action 序列
            auto act=Sequence::create(DelayTime::create(1),CallFunc::create(CC_CALLBACK_0(Boom::Killme, newB)), NULL);
            newB->runAction(act);
        }
            break;
    }
    return newB;
}
bool Boom::init()
{
    if (!Node::init())
    {
        return false;
    }
    return true;
}
void Boom::Killme()
{
    this->removeFromParentAndCleanup(true);
}


总结:以上实现了敌机类+爆炸类

你可能感兴趣的:(编辑器,效果,粒子,爆炸,敌机)