Cocos2d iphone cocos2d调度功能导致EXC\u访问错误
我计划每2秒执行一个名为Cocos2d iphone cocos2d调度功能导致EXC\u访问错误,cocos2d-iphone,Cocos2d Iphone,我计划每2秒执行一个名为Enemyshoot()的函数。在函数中,我访问一个“敌人”向量中的随机对象,该向量存储所有敌人对象。同时,敌人在两秒钟内被摧毁。我发现,当我计划enemyshoot()函数时,它可能会访问已经被摧毁的敌人对象,这将报告EXC\u BAD\u访问。我该怎么办 std::vector<Enemy*> enemies; this->schedule(schedule_selector(GameScene::enemyShoot), ENEMY_
Enemyshoot()
的函数。在函数中,我访问一个“敌人
”向量中的随机对象,该向量存储所有敌人对象。同时,敌人在两秒钟内被摧毁。我发现,当我计划enemyshoot()
函数时,它可能会访问已经被摧毁的敌人对象,这将报告EXC\u BAD\u访问
。我该怎么办
std::vector<Enemy*> enemies;
this->schedule(schedule_selector(GameScene::enemyShoot), ENEMY_SHOOT_FREQUENCY);
void GameScene::enemyShoot(float dt) {
auto random = int(CCRANDOM_0_1()*(enemies.size()-1));
Enemy *tempEnemy = enemies.at(random);
new EnemyMissle(this,tempEnemy->enemy->getPosition().x,tempEnemy->enemy->getPosition().y);
}
//this is where I delete the enemy object
if((ENEMY_COLLISION_BITMASK==a>getCollisionBitmask()&&PLAYER_MISSLE_COLLISION_BITMASK==b->getCollisionBitmask())||(ENEMY_COLLISION_BITMASK==b->getCollisionBitmask()&&PLAYER_MISSLE_COLLISION_BITMASK==a->getCollisionBitmask())){
//add the score and refresh the score label
score+=10;
__String *tempScore = __String::createWithFormat("score: %i",score);
//traversing all the enemy objects to find the collided enemy
scoreLabel->setString(tempScore->getCString());
for(int i=0;i<enemies.size();i++){
if((enemies.at(i)->enemy->getPhysicsBody()==b))
enemies.erase(enemies.begin()+i);
}
remainingEnemies--;
this->removeChild(b->getOwner());
this->removeChild(a->getOwner());
//if it is the last enemy spaceship, go to the game over scene.
if (enemies.size()==0)GameScene::GoToGameOverScene(score);
}
std::向量敌人;
这->时间表(时间表选择器(游戏场景::敌人射击),敌人射击频率);
虚空游戏场景::敌人射击(浮动dt){
auto random=int(CCRANDOM_0_1()*(friends.size()-1));
敌方*TEMPFOURY=敌方.at(随机);
新的敌人导弹(这个,临时敌人->敌人->获取位置().x,临时敌人->敌人->获取位置().y);
}
//这是我删除敌人对象的地方
如果((敌方冲突比特掩码==a>getCollisionBitmask()&&PLAYER冲突比特掩码==b->getCollisionBitmask())|(敌方冲突比特掩码==b->getCollisionBitmask()&&PLAYER冲突比特掩码==a->getCollisionBitmask()){
//添加分数并刷新分数标签
分数+=10分;
__String*tempScore=\uu String::createWithFormat(“分数:%i”,分数);
//穿越所有敌人的物体,找到相撞的敌人
scoreLabel->setString(tempScore->getCString());
对于(int i=0;ienemy->getPhysicsBody()=b))
敌人。擦除(敌人。开始()+i);
}
剩下的敌人——;
这->删除child(b->getOwner());
这->删除child(a->getOwner());
//如果它是最后一艘敌方宇宙飞船,请进入游戏场景。
如果(敌人.size()==0)游戏场景::GoToGameOverScene(分数);
}
你应该创建一个要删除的敌人数组,而不是在你的循环中删除他们,然后解析这个数组来真正删除他们