Reputation: 5428
I have this Class method to create a hero object.
+(id)hero
{
NSArray *heroWalkingFrames;
//Setup the array to hold the walking frames
NSMutableArray *walkFrames = [NSMutableArray array];
//Load the TextureAtlas for the hero
SKTextureAtlas *heroAnimatedAtlas = [SKTextureAtlas atlasNamed:@"HeroImages"];
//Load the animation frames from the TextureAtlas
int numImages = (int)heroAnimatedAtlas.textureNames.count;
for (int i=1; i <= numImages/2; i++) {
NSString *textureName = [NSString stringWithFormat:@"hero%d", i];
SKTexture *temp = [heroAnimatedAtlas textureNamed:textureName];
[walkFrames addObject:temp];
}
heroWalkingFrames = walkFrames;
//Create hero sprite, setup position in middle of the screen, and add to Scene
SKTexture *temp = heroWalkingFrames[0];
Hero *hero = [Hero spriteNodeWithTexture:temp];
hero.name =@"Hero";
hero.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:hero.size];
hero.physicsBody.categoryBitMask = heroCategory;
hero.physicsBody.categoryBitMask = obstacleCategory | groundCategory | homeCategory;
return hero;
}
and I have another instance method to perform run animation for my hero.
-(void)Start
{
SKAction *incrementRight = [SKAction moveByX:10 y:0 duration:.05];
SKAction *moveRight = [SKAction repeatActionForever:incrementRight];
[self runAction:moveRight];
}
now heroWalkingFrames
variable in Start
method so I can perform animation, I want to add this line in Start
method
[SKAction repeatActionForever:[SKAction animateWithTextures:heroWalkingFrames timePerFrame:0.1f resize:NO restore:YES]];
Is there any way I can use this variable for both ?
Upvotes: 0
Views: 42
Reputation: 2401
Sure, in Hero.h
add:
@property (nonatomic, retain) NSArray *walkingFrames;
Then, in your +(id)hero
method, instead of declaring a new array NSArray *heroWalkingFrames
, use:
+(id)hero
{
//Setup the array to hold the walking frames
NSMutableArray *walkFrames = [NSMutableArray array];
//Load the TextureAtlas for the hero
SKTextureAtlas *heroAnimatedAtlas = [SKTextureAtlas atlasNamed:@"HeroImages"];
//Load the animation frames from the TextureAtlas
int numImages = (int)heroAnimatedAtlas.textureNames.count;
for (int i=1; i <= numImages/2; i++) {
NSString *textureName = [NSString stringWithFormat:@"hero%d", i];
SKTexture *temp = [heroAnimatedAtlas textureNamed:textureName];
[walkFrames addObject:temp];
}
//We set hero texture to the first animation texture:
Hero *hero = [Hero spriteNodeWithTexture:walkFrames[0]];
// Set the hero property "walkingFrames"
hero.walkingFrames = walkFrames;
hero.name =@"Hero";
hero.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:hero.size];
hero.physicsBody.categoryBitMask = heroCategory;
hero.physicsBody.categoryBitMask = obstacleCategory | groundCategory | homeCategory;
return hero;
}
Upvotes: 2