Help identify the problem

Help identify the problem
0.0 0

#1

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.


#2

Why not using Follow action?:

http://www.cocos2d-x.org/reference/native-cpp/V3.0alpha0/d8/dab/classcocos2d_1_1_follow.html

Also, I can see some old stuff like ccpMult, you should switch to 3.x standard.

You can also don’t even use action and move enemies in update().


#3

Hi piotrros,
Thanks for the suggestion. My code in animateEnemy() is to move a sprite to a position which i can expand it into moving to player position (following) or just moving to a certain position near the player position (surrounding) which i doubt the follow action can do with a Node (not sure yet, i though it only work with camera before, now you mention it so i am looking into it again.).
But this Follow action is really useful, thanks again.