是否可以从SKTexture中从SKSpriteNode获取当前图像名称?
我是一个新的SpriteKit,我正在写一个小游戏。我需要探测忍者何时会攻击敌人。就像这样


我在做SKAction
- (void)setUpHit
{
SKTextureAtlas *hitAtlas = [SKTextureAtlas atlasNamed:@"Ninja_hit"];
SKTexture *hit1 = [hitAtlas textureNamed:@"Ninja_hit_1"];
SKTexture *hit2 = [hitAtlas textureNamed:@"Ninja_hit_2"];
SKTexture *hit3 = [hitAtlas textureNamed:@"Ninja_hit_3"];
SKTexture *hit4 = [hitAtlas textureNamed:@"Ninja_hit_4"];
SKAction *hitAnimation = [SKAction animateWithTextures:@[hit1, hit2, hit3, hit4]
timePerFrame:0.1];
SKAction *wait = [SKAction waitForDuration:0.3];
SKAction *goBack = [SKAction animateWithTextures:@[hit1, [SKTexture textureWithImageNamed:@"Ninja"]]
timePerFrame:0.1];
self.hitAction = [SKAction sequence:@[hitAnimation, wait, goBack]];
}但是,hit只在图片Ninja_hit_2.png或Ninja_hit_3.png上进行。
因此,我需要检测当前纹理图像名称,当我做intersectsNode忍者与敌人。
现在我正在做这样的事情
if ([ninja intersectsNode:node] && !self.isKilled)
{
SKTexture *currentTexture = [ninja texture];
if ([self isHit:currentTexture])
{
//kill enemy here
}
}哪里
- (BOOL)isHit:(SKTexture *)texture
{
NSString *description = [texture description];
NSRange range = [description rangeOfString:@"'"];
NSString *textureName = [description substringFromIndex:NSMaxRange(range)];
range = [textureName rangeOfString:@"'"];
textureName = [textureName substringToIndex:NSMaxRange(range) - 1];
if ([textureName isEqualToString:@"Ninja_hit_2.png"] ||
[textureName isEqualToString:@"Ninja_hit_3.png"])
{
return YES;
}
return NO;
}我知道这是不正确的,但我找不到如何使用当前的纹理名称或正确的做法。你能帮我一下吗?
发布于 2020-01-01 19:50:05
查看SKSpriteNode的纹理描述,您应该能够看到图像的名称。
for node in self.children {
if node is SKSpriteNode && node.physicsBody != nil {
let spriteNode:SKSpriteNode = node as! SKSpriteNode
print("sprite node image name \(spriteNode.texture!.description)")
}
}这方面的一个示例将在调试时打印出来。
sprite节点图像名称'Clear10x10‘(10×10)
然后,您可以在本例中为图像名RegX第三个字段“Clear10x10”。请注意,Apple可能会更改此描述格式。
https://stackoverflow.com/questions/21698512
复制相似问题