2

I am using cocos2d-iphone to place Sprites onto a Layer to setup a game playfield. At certain points in the game, certain Sprites need to be removed based upon game conditions. What I would like to do is setup an array of Sprite pointers, but I have two questions:

What's the best way to place Sprite pointers in an array?

How does one remove the Sprite in cocos2d with only a pointer to the Sprite? I know how to do it from its parent layer, but that is too runtime intensive for the main game loop.

Thanks in advance!

user21293
  • 6,439
  • 11
  • 44
  • 57
  • Please feel free to accept your own answer instead of mine. By the sound of things, you were able to get it working using your method, and I won't be offended! :) – e.James Oct 13 '09 at 18:45

3 Answers3

3

The Sprite class inherits from CocosNode, so you should be able to call spritePointer.parent.remove(spritePointer)

e.James
  • 116,942
  • 41
  • 177
  • 214
2

There's also [mySprite removeFromParentAndCleanup:YES].

buildsucceeded
  • 4,203
  • 4
  • 34
  • 72
2

I figured it out. If anyone else is interested, the way to do it is to declare an array of Sprite pointers, such as:

Sprite * mySprites[10][10]; // assuming a 10x10 playfield where obstacles get placed

Then, when setting up your Sprites:

mySprites[0][0] = [Sprite spriteWithFile: @"obstacle.png"];   
[myLayer add:mySprites[0][0]];  

To remove the Sprite:

[myLayer remove:mySprites[0][0]];
Nitish
  • 13,845
  • 28
  • 135
  • 263
user21293
  • 6,439
  • 11
  • 44
  • 57