您的位置:首页 > 移动开发 > Cocos引擎

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

2014-09-09 00:18 543 查看
敌机的产生与子弹的产生类似,只是出现在随机的位置,而且向下运动怎么产生随机的坐标就要将坐标设置为随机函数
</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--;
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐