How to compare SKSpriteNode textures

寵の児 提交于 2019-12-20 02:42:28

问题


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

回答1:


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];
}


来源:https://stackoverflow.com/questions/23201190/how-to-compare-skspritenode-textures

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!