1

i need to check if my sprite node texture is equal to name @"GoldDot". i try the code

if ([red.texture isEqual:@"GoldDot"]) {

     NSLog(@"gold!!!!");
    }else{



}

please help

sangony
  • 11,636
  • 4
  • 39
  • 55
100tomer
  • 137
  • 1
  • 10

2 Answers2

3

For this you would be better testing to see if your texture is equal to another texture, not a string. The test you might want to try is,

if([red.texture isEqual:[SKTexture textureWithImageNamed:@"GoldDot.png"]]){
     NSLog(@"gold!!!");
else{
}

Then simply give the name of the texture you are looking for.

swelsh
  • 154
  • 3
  • This probably wouldnt work. A separate texture instance, even though derived fromt the same image will not return equal using that method. – ZeMoon Mar 23 '15 at 13:21
3

To compare SKTextures, have a look at this answer.

A cleaner alternative would be to set the name of the SKSpriteNode as the image you are setting.

NSString *textureName = @"GoldDot";
SKSpriteNode *node =[SKSpriteNode spriteNodeWithImageNamed: textureName];
node.name = textureName;

Afterwards, just compare the name

if ([red.name isEqual:@"GoldDot"]) {
   NSLog(@"gold!!!!");
}
Community
  • 1
  • 1
ZeMoon
  • 20,054
  • 5
  • 57
  • 98