开发者

Cocoas2d Get Sprite by CGPoint

开发者 https://www.devze.com 2023-02-28 00:23 出处:网络
I use the following code to initialize an object CCSprite *gridp = [CCSprite spriteWithFile:@\"grid.png\"

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 functi开发者_运维知识库ons 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.


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


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.


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

[self getChildByTag:spriteName]


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号