[cocos2d-x][游戏开发]通过cocos2d-x实现简易飞机大战 07.游戏界面 敌机的产生与移动

来源:互联网 发布:游戏源码怎么用 编辑:程序博客网 时间:2024/05/08 09:12
敌机的产生与子弹的产生类似,只是出现在随机的位置,而且向下运动怎么产生随机的坐标就要将坐标设置为随机函数
</pre><pre name="code" class="cpp">void Game::newEnemy(float t){    int type=random()%10;    if (type<3) {        type=3;    }else if(type>8)    {  type=1;    }else    {        type=2;    }    int ex=random()%(int)Director::getInstance()->getWinSize().width;    Enemy *newe=Enemy::createEnemy(type, ex, Director::getInstance()->getWinSize().height);    allEnemy.pushBack(newe);//将新产生的敌机添加到集合    this->addChild(newe);}

敌机产生后设置计划移动

void Game::moveEnemy(float t){    for (int i=0; i<allEnemy.size(); i++) {        //花去i家灰机        Enemy * nowE=allEnemy.at(i);        nowE->moveTo(nowE->ex-random()%10,nowE->ey-30);        if (nowE->ey<-nowE->eSprite->getContentSize().height) {            allEnemy.erase(i);            this->removeChild(nowE);            i--;        }            }


0 0
原创粉丝点击