如何比较SKSpriteNode纹理 [英] How to compare SKSpriteNode textures

查看:282
本文介绍了如何比较SKSpriteNode纹理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Sprite Kit制作游戏。当发生碰撞时,我想检索我的射弹碰撞的SKSpriteNode的图像,根据怪物的图像分配不同的点值。我认为比较SKSpriteNode的纹理属性可能会起作用。我尝试了以下代码,但我的if语句从未被调用过。有什么建议吗?

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


推荐答案

是,有一种方法可以使用UIImage比较两个图像/纹理。

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

这篇关于如何比较SKSpriteNode纹理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆