Hi,
I have some codes to make some sprites (enemy) chase after a sprite (player) like the following:
void HelloWorld::enemyMoveFinished(Node* sender)
{
Sprite *enemy = (Sprite *)sender;
animateEnemy(enemy);
}
void HelloWorld::animateEnemy(Sprite* enemy)
{
auto actionMove = MoveBy::create(0.1, ccpMult(ccpNormalize(ccpSub(player->getPosition(),
enemy->getPosition())), 10));
auto actionMoveDone = CallFuncN::create(this, callfuncN_selector(Game::enemyMoveFinished));
auto actions = Sequence::create(actionMove, actionMoveDone, nullptr);
enemy->runAction(actions);
}
void HelloWorld::addEnemies(Point pos)
{
auto spriteNode = Node::create();
_enemySprite = Sprite::create("files/enemy.png");
_enemySprite->setPosition(pos);
spriteNode->addChild(_enemySprite);
this->addChild(spriteNode, 0);
this->animateEnemy(_enemySprite);
}
The basic idea is that animateEnemy()
check player position and a enemy position, calculate the different, then move that enemy sprite a short distance base on the calculation then repeat the position calculation and move the sprite again and again.
The code work ok. But i noticed that, if i have for example 50 enemies in game, after a while, some enemies or more than half of them just stop moving (player stand still or moving doesn’t matter).
Anyone have any ideas what could have cause the sprites to stop moving with these code? I could send you the complete of my code for further examinations.
Thanks.