Hi,
I dont know if im doing it correct, but to remove objects from my game (dead players, objects out of screen, …) im using:
1 - I have a MutableArray for each object types, one for in-game objects and other of the same type for out-game objects:
static cocos2d::CCMutableArray<Coin*> *coins;
static cocos2d::CCMutableArray<Coin>*coinsToDelete;
static cocos2d::CCMutableArray<Enemy> *enemies;
static cocos2d::CCMutableArray<Enemy>**enemiesToDelete;
the asterisk make the text bold
2 - When create an object (an enemy for example) i do:
Enemy *enemy = new Enemy();
>
enemy~~>getSprite~~>setPosition);
>
layerPlayers~~>addChild);
GameObjects::enemies~~>addObject(enemy); // this is the mutable array from #1
3 - When i need update the objects (enemy for example):
CCMutableArray<Enemy*>::CCMutableArrayIterator it2;
>
for (it2 = GameObjects::enemies~~>begin; it2 != GameObjects::enemies~~>end(); it2++)
{
Enemy **enemy =**it2;
enemy~~>update;
}
4~~ When i need remove it(this code is the last operation in update method, after all updates i check if i have an object to remove):
for (it2 = GameObjects::enemiesToDelete~~>begin; it2 != GameObjects::enemiesToDelete~~>end(); it2++)
{
Enemy **enemy =**it2;
>
if (enemy~~>isCanDelete)
{
GameObjects::enemies~~>removeObject(enemy);
enemy~~>getSpriteSheet~~>removeFromParentAndCleanup;
}
}
5~~ Each entity when check that it can be excluded by collision or out of screen, they set:
GameObjects::enemiesToDelete~~>addObject(this);
And after update all objects, it will be deleted, as you see in #4
Is this the best or correct way?