《塔防类手游开发教程》 第七节 炮塔发射子弹

来源:互联网 发布:电子琴入门教程软件 编辑:程序博客网 时间:2024/04/29 21:26

在塔防游戏中,要建炮塔,让炮塔发射子弹攻击怪物,那么我们在编程中需要做个处理,每个塔都要发射子弹,攻击怪物。我们要做的事情就是让每个塔旋转并攻击敌人,在这过程中,要找到离你最近的敌人:①找最近的敌人;②在攻击范围内的敌人

在旋转的时候,塔有一个坐标,敌人有个坐标,两个坐标相减,获得一个向量,就是旋转的角度。另外,在cocos2d-x中规定顺时针方向为正,这显然与我们计算出的角度相反,所以转化的时候需要把角度α变为-α;speed表示炮塔旋转的速度,0.5/M_PI其实是1/2PI,塔表示1秒钟旋转几个圆;rolateDuration表示旋转特定的角度需要的时间,计算它用弧度乘以速度。

我们在塔里增加一个计划任务:每隔0.5秒旋转这个塔,发射子弹:

首先在Gamescene.h中定义一个静态向量static Vector<Bullet *> allBullet;//所有子弹

把怪物都保存到这个向量中static Vector<Enemy *> allEnemy;//所有怪物

之后创建一个子弹类:                                                

class Bullet:public Node

{

 public:

    int bx,by;

    int objx,objy;//目标点

    int type;

    CREATE_FUNC(Bullet);

    bool init();

    static Bullet * createBullet(int t,int ang,int x,int y,int dx,int dy);

    void killMe();

};

创建子弹:

Bullet * Bullet::createBullet(int t,int ang,int x,int y,int dx,int dy){

    Bullet * newb=Bullet::create();

    switch (t) {

        case 1:

          { Sprite* spbullet = Sprite::createWithSpriteFrameName("arrowBullet.png");

              newb->bx=x;

              newb->by=y;

           newb->setPosition(x+35,y+35);

            spbullet->setRotation(ang);

            newb->addChild(spbullet);

            newb->objx=dx;

            newb->objy=dy;

           }

            break;

            

        default:

            break;

    }

    float far=Vec2(x, y).getDistance(Vec2(dx,dy));

    float time=far/300;

    auto act1=MoveTo::create(time, Vec2(dx,dy));

    auto act2=CallFunc::create(CC_CALLBACK_0(Bullet::killMe,newb));

    newb->runAction(Sequence::create(act1,act2, NULL));

    

    return newb;

}

实现移动和攻击:

void TD::moveAndShot(float t)//移动和攻击

{

    if (GameScene::allEnemy.size()==0) {

        return;

    }

    // 1 找到最近的那个敌人坐标

    int index=0;

    int min=GameScene::allEnemy.at(0)->getPosition().getDistance(this->getPosition());

    for (int i=0; i<GameScene::allEnemy.size(); i++) {

        int far=GameScene::allEnemy.at(i)->getPosition().getDistance(this->getPosition());

        if (far<min) {

            index=i;

            min=far;

        }

    }

  

    dx= GameScene::allEnemy.at(index)->getPosition().x;

    dy= GameScene::allEnemy.at(index)->getPosition().y;

   

      // 2

        Vec2 rotateVector = GameScene::allEnemy.at(index)->getPosition() - this->getPosition();

        float rotateRadians = rotateVector.getAngle();

   

        float rotateDegrees = CC_RADIANS_TO_DEGREES(-1 * rotateRadians);

        ang= rotateDegrees;

        // 3

        float speed = 0.5 / M_PI;

        float rotateDuration = fabs(rotateRadians * speed);

        // 4

        this->getChildByTag(10)->getChildByTag(11)->runAction( Sequence::create(RotateTo::create(rotateDuration, rotateDegrees),

        CallFunc::create(CC_CALLBACK_0(TD::fire, this)),

                                                 NULL));

 

}

计划任务的定义:

// 计划任务 每隔0.5秒旋转这个塔 发射子弹

    td->schedule(schedule_selector(TD::moveAndShot),0.
0 0
原创粉丝点击