Precursor
Precursor

Reputation: 642

Cocoas2d Get Sprite by CGPoint

I use the following code to initialize an object

CCSprite *gridp = [CCSprite spriteWithFile:@"grid.png" 
                                               rect:CGRectMake(0, 0, 60, 60)];
gridp.position = ccp(x, y);

My question is, after I add the object and after the functions return, how can I delete the sprite? I was guessing there would be a get by CGPoint, but I haven't found anything like that. I cannot store references to all of the objects since the game generates a few hundred of these at runtime.

Upvotes: 0

Views: 190

Answers (4)

tallen11
tallen11

Reputation: 1377

To remove a child:

[self removeChild:yourSprite cleanup:YES];

To remove by tag:

First you need to specify a tag for your sprite:

[self addChild:yourSprite z:0 tag:1];

Then, to remove it:

[self removeChildByTag:1 cleanup:YES];

Like the others said, you would need to write your own function in order to remove a sprite based on it's position.

Hope this helped,

~~Tate

Upvotes: 1

Adam Ashwal
Adam Ashwal

Reputation: 1472

And to remove a sprite you use [self removeChild:sprite cleanup: YES];

Upvotes: 0

droid kid
droid kid

Reputation: 7609

Also if you assign Tag to the Sprite, then it can be removed using

[self getChildByTag:spriteName]

Upvotes: 0

Bastian
Bastian

Reputation: 10433

There is no way to get a sprite just by the position.. unless you write a function that does that, and therefor you need references to your sprites...

You have to add the sprites to a node to display them... and a node always has a reference to all of it's children. You could walk through the children array to remove nodes you want to delete.

Upvotes: 0

Related Questions