user636915
user636915

Reputation: 1

Cocos2d sprite file finding

I am looking for a way to find the filename of the CGSprite variable, then use it in an IF statement. Like this:

if(target.spritefilename?? == @"Car1.png")
{
    target = [CCSprite spriteWithFile:@"Car1_dead.png" rect:CGRectMake(0, 0, 37, 76)];
}

Upvotes: 0

Views: 1262

Answers (7)

Abhinav
Abhinav

Reputation: 191

If the purpose of this to just identifying the sprite you can simply do it by using tag property of sprite.

Like set a taf for sprite while initialing or creating the sprite as

CCSprite *spr1=[CCSprite spriteWithFile:@"1.png"];
spr1.tag=1;

CCSprite *spr2=[CCSprite spriteWithFile:@"2.png"];
spr2.tag=2;

and while getting the sprite on particular event use to get tag value as

if([(CCSprite*)tagetSprite tag]==1)
{
}
else if([(CCSprite*)tagetSprite tag]==2)
{
}

Upvotes: 0

greenny
greenny

Reputation: 1

if([(NSString*)sprite.userData hasPrefix@"Car1"])
  ...

Upvotes: 0

gixdev
gixdev

Reputation: 570

If([sprite.userData hasPrefix@"Car1"])
...

Upvotes: 0

Tayyab
Tayyab

Reputation: 10631

I am not sure why you want to do this. But as far as I am guessing your purpose I would like to suggest an alternative and proper way to identify your different sprites.

There is a "userData" property in the sprite (inherited from parent) which you can use to store your custom data. So when you create your sprites you should also assign the identified to the userData property.

Like in your case when you first create your "target" sprite, you can then set, "target.userData = "

Later you can check if target.userData is equal to your required file name.

This is the proper way to store custom data in your sprites.

I hope it helps.

Upvotes: 3

Chetan Bhalara
Chetan Bhalara

Reputation: 10344

You can use isEqualtoString for string comparison.

if([target.spritefilename isEqualtoString:@"Car1.png"])

For NSString.

Upvotes: 1

Anish
Anish

Reputation: 2917

store the sprite names in an array and check the current sprite name matches the name in an array.

if([image_array containsObject:@"car.png"])
{
NSLog(@"Image Found");
} 

Hope this helps!!!

Upvotes: 0

Praveen S
Praveen S

Reputation: 10395

if([target.spritefilename isEqualtoString:@"Car1.png"]) 

is what i guess you are looking for.

Upvotes: 2

Related Questions