Search code examples
objective-csprite-kitskspritenodesknode

Sprite kit (objective c) check texture of sprite node


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


Solution

  • 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!!!!");
    }