Hmm my strategy would be this:
On your ninja class have a property for your textures. Keep them around
@property (nonatomic, strong) NSArray * hitTextures;
Then store the hit textures (note this is a lazy loading getter method)
-(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;
}
Note that we don't need to make the SKTextureAtlas
object explicitly:
When loading the texture data, Sprite Kit searches the app bundle for an image file with the specified filename. If a matching image file cannot be found, Sprite Kit searches for the texture in any texture atlases stored in the app bundle. If the specified image does not exist anywhere in the bundle, Sprite Kit creates a placeholder texture image.
Use this texture array to fill in your SKAction
SKAction *hitAnimation = [SKAction animateWithTextures:self.hitTextures
timePerFrame:0.1];
This allows you do change your -isHit
method like so:
- (BOOL)isHit:(SKTexture *)texture
{
NSUInteger index = [self.hitTextures indexOfObject:texture];
if (index == 1 ||
index == 2)
{
return YES;
}
return NO;
}
This way you don't rely on an implementation detail of the -description
method that is subject to change.