Removing elements in an array in C++

Removing elements in an array in C++
0.0 0


Well, this is more of a C++ question rather than a cocos2d-x question. So anyway, I’ve been following tutorials from this site and from
In both tutorials (in the shooter game sample) they make “enemies” and then puts them in an array (a CCMutableArray but the one I have does not have CCMutableArray so I use CCArray instead)

void createMonster( ) {
   // codes to position
   CCSprite * monster = CCSprite::create( "monster.png" );
   m_monstersArray.addObject( monster );

But when the monster is hit by the bullet, it’s not totally destroyed but is just set to invisible. The object still remains in the array, just hiding from the player. Something like this:

void destroyMonster( CCSprite * monster ) {
   // Where 'monster' is the monster in the array that needs to be destroyed.
   monster.setVisible( false );

I guess this works since the monster is a CCSprite object. But what if I have a sprite in a custom class and that custom class is the one inside the array, not the sprite inside the custom class?

class Fruit {
   CCSprite * m_fruitSprite;

   void createFruitSprite( ); // makes the sprite.
   CCSprite * getfruitSprite( ); // returns m_fruitSprite.

Then on my main layer:

// m_FruitsVector is a std::vector()
void makeFruits( ) {
   Fruit myFruit;
   m_FruitsVector.push_back( myFruit );

void ccTouchBegan( ARGS ) {
   // loop through each element in m_FruitsVector
   // check if touch coordinates is inside fruit rect. i ).setTouched( true );
   // PROBLEM: Remove the Fruit object from the array so that the next time the player touches the screen, ccTouchBegan would not need to loop through an already touched fruit.

Thanks a lot.