如何比较 SKSpriteNode 纹理 [英] How to compare SKSpriteNode textures
本文介绍了如何比较 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屋!
查看全文