3

I am making a game with Sprite Kit. When there is a collision I would like to retrieve the image of the SKSpriteNode that my projectile collided with to assign different point values depending on the image of the monster. I think comparing the texture property of the SKSpriteNode could work. I have tried the following code, but my if statement is never called. Any suggestions?

- (void)projectile:(SKSpriteNode *)projectile didCollideWithMonster:(SKSpriteNode *)monster {
        SKTexture *tex = [SKTexture textureWithImageNamed:@"img.png"];
        if ([[monster texture] isEqual:tex])
        {
            NSLog(@"it works");
        }
}
MSU_Bulldog
  • 3,501
  • 5
  • 37
  • 73
  • why not to use userData property or name property? – AndrewShmig Apr 21 '14 at 16:16
  • I specifically want the image of the SKSpriteNode named "monster" – MSU_Bulldog Apr 21 '14 at 16:20
  • why not to name different monsters with different names? I think its quite time-consuming to compare two images/textures. – AndrewShmig Apr 21 '14 at 16:23
  • I tried using monster.name = @"monster1" where I initialize the monster, but still no results when comparing a name in my if statement. – MSU_Bulldog Apr 21 '14 at 16:32
  • And? Please, tell us what you want exactly to achieve. Is your SKSpriteNode runs any animations or it has static texture which you want to check and perform some action? – AndrewShmig Apr 21 '14 at 16:34
  • You should compare strings with isEqualToString: method, not isEqual: – AndrewShmig Apr 21 '14 at 16:34
  • I am rapidly creating SKSpriteNodes that will move across the screen and are removed when the collision method is called. When I do a [monster description] on the SKSpriteNode, the only property that is consistent is the SKTexture, that is why I would like to compare the SKTexture of each monster after collision. – MSU_Bulldog Apr 21 '14 at 16:44
  • Why do you even call [monster description]?! There is an SKSpriteNode property called "name" and "userData", you should read more about them in Apple docs. – AndrewShmig Apr 21 '14 at 16:52
  • I know of the "name" and "userData" properties, but what I want to do is retrieve the image of the monster and compare it. Is this possible? – MSU_Bulldog Apr 21 '14 at 17:04
  • Yes, there is a way. You can use UIImage and compare them like here: http://stackoverflow.com/questions/11342897/how-to-compare-two-uiimage-objects – AndrewShmig Apr 21 '14 at 17:16
  • Thank you so much for you help. Assigning a name actually ended up being my answer. For some reason I needed to name every monster for it to work, not just one individual. – MSU_Bulldog Apr 21 '14 at 18:17
  • @AndrewShmig please post an answer to this question so it can be accepted formally. – ZeMoon Apr 22 '14 at 09:47

1 Answers1

3

Yes, there is a way to compare two images/textures using UIImage.

- (BOOL)image:(UIImage *)image1 isEqualTo:(UIImage *)image2
{
    NSData *data1 = UIImagePNGRepresentation(image1);
    NSData *data2 = UIImagePNGRepresentation(image2);

    return [data1 isEqual:data2];
}
AndrewShmig
  • 4,843
  • 6
  • 39
  • 68
  • 1
    Парень, это ты переводил книгу про CoreData на Хабре? Респект тебе огромный за твой труд! :) Как раз сейчас читаю – Andrey Gordeev Apr 22 '14 at 15:13
  • 1
    @AndreyGordeev, я :) не закончил правда, но планирую закончить, как только учебу завершу. – AndrewShmig Apr 22 '14 at 15:17