SKTexture获取图像名称

时间:2014-02-11 09:58:20

标签: ios sprite-kit skspritenode sktexture

是否有可能从SKSpriteNode获取SKTexture的当前图像名称?

我是SpriteKit的新手,我正在写一个小游戏。我需要检测忍者何时会击中敌人。像这样的东西

enter image description here enter image description here

我正在做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]];
}

但是只有图像Ninja_hit_2.png或Ninja_hit_3.png才会出现。

所以当我和敌人做crosssNode忍者时,我需要检测当前的纹理图像名称。

现在我正在做类似

的事情
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;
}

我知道这不正确,但我找不到如何获取当前纹理名称或正确执行它。你可以帮帮我吗?

3 个答案:

答案 0 :(得分:2)

嗯,我的策略是:

在你的忍者类上有一个纹理属性。把它们放在一边

@property (nonatomic, strong) NSArray * hitTextures;

然后存储命中纹理(注意这是一个延迟加载的getter方法)

-(NSArray *)hitTextures{
    if (_hitTextures == nil){
        SKTexture *hit1 = [SKTexture textureWithImageNamed:@"Ninja_hit_1"];
        SKTexture *hit2 = [SKTexture textureWithImageNamed:@"Ninja_hit_2"];
        SKTexture *hit3 = [SKTexture textureWithImageNamed:@"Ninja_hit_3"];
        SKTexture *hit4 = [SKTexture textureWithImageNamed:@"Ninja_hit_4"];

        _hitTextures = @[hit1, hit2, hit3, hit4];
    }
    return _hitTextures;
}

请注意,我们不需要明确地生成SKTextureAtlas对象:

  

加载纹理数据时,Sprite Kit会在应用程序包中搜索具有指定文件名的图像文件。如果找不到匹配的图像文件,Sprite Kit将在应用程序包中存储的任何纹理图集中搜索纹理。如果指定的图像不存在于包中的任何位置,Sprite Kit将创建占位符纹理图像。

使用此纹理数组填写SKAction

    SKAction *hitAnimation = [SKAction animateWithTextures:self.hitTextures
                                              timePerFrame:0.1];

这允许您更改-isHit方法,如下所示:

- (BOOL)isHit:(SKTexture *)texture
{
    NSUInteger index = [self.hitTextures indexOfObject:texture];

    if (index == 1 ||
        index == 2)
    {
        return YES;
    }

    return NO;
}

这样您就不会依赖可能会发生变化的-description方法的实现细节。

答案 1 :(得分:2)

从SKAtlas动画中检测当前名称,这不是最好的方法,但它对我有用。

var tempstr = yoursprite.texture!.description

        if(tempstr.rangeOfString("currentFrameName", options: NSStringCompareOptions.LiteralSearch, range: nil, locale: nil) != nil){
// do something
}

答案 2 :(得分:0)

查看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)")
    }
}

一个示例将在调试时打印出来

精灵节点图片名称“ Clear10x10”(10 x 10)

然后,您可以将RegX用作图像名称的第三个字段,在此示例中为“ Clear10x10”。请注意,Apple可能会更改此描述格式。