SKTexture get image name

Is it possible to get the current image name from SKTexture from SKSpriteNode?

I am new to SpriteKit and I am writing a little game. I need to determine when the ninja will hit the enemy. Something like that

enter image description hereenter image description here

I do SKAction as

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

But the blow goes only to the images of Ninja_hit_2.png or Ninja_hit_3.png.

So I need to determine the current name of the texture image when I do the intersectionsNode ninja with the enemy.

Now i'm doing something like

if ([ninja intersectsNode:node] &&  !self.isKilled)
{
    SKTexture *currentTexture = [ninja texture];

    if ([self isHit:currentTexture])
    {
        //kill enemy here
    }  
}

Where

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

I know this is wrong, but I cannot find how to use the current texture name or do it right. Could you help me?

+3
source share
2 answers

Hmm my strategy would be this:

Your ninja class has a property for your textures. Keep them around

@property (nonatomic, strong) NSArray * hitTextures;

- ( , )

-(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, .

+2

SKAtlas, , .

var tempstr = yoursprite.texture!.description

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

All Articles