Animating my character in Sprite Kit?

How can I animate something that is moving across the screen in sprite kit? This is what I have at the moment:

-(void)createMan {

CGPoint startPoint = CGPointMake(120, 298);// those coordinates are the bottom left corner

SKSpriteNode *man = [SKSpriteNode spriteNodeWithImageNamed:@"man1"];
man.position = CGPointMake(startPoint.x, startPoint.y);
man.zPosition = 6;
[man setScale:0.5f];
[self addChild:man];

// we now want to call this method again repeatedly at a random interval :)

float randomNum = arc4random_uniform(3)+3;
[self performSelector:@selector(createMan) withObject:nil afterDelay:randomNum];

//man moves right
SKAction *moveNodeUp = [SKAction moveByX:400.0 y:0 duration:5];
[man runAction: moveNodeUp];
}

So how can I change the picture after a set time is basically what im asking. Thanks in advance.

Answers


So how can I change the picture after a set time is basically what im asking.

Changing a Sprite's Texture in the Sprite Kit Programming Guide demonstrates how to use -animateWithTextures:timePerFrame: to loop over a sequence of textures over time, which is what you probably want in order to animate your sprite as it moves.

Following the example in that section, you should create an action that uses a sequence of textures. Let's say that you have three textures, texture1, texture2, and texture3. Then you'd put the textures in an array and create an animation action like this:

NSArray *walkingUpTextures = @[texture1, texture2, texture3];
SKAction *animateAction = [SKAction animateWithTextures:walkingUpTextures timePerFrame:0.1];

Note: you can create the texture array any way you want. I used Objective-C's object literal syntax above, but you can call +[NSArray arrayWithObjects:] or any other method that works for you.

To make sure that the animation happens with the movement, and only with the movement, combine that action with your movement action:

SKAction *animateAction = [SKAction animateWithTextures:manMovementTextures timePerFrame:0.1];
SKAction *moveNodeUpAction = [SKAction moveByX:400.0 y:0 duration:5];
SKAction *groupAction = [SKAction group:@[animateAction, moveNodeUpAction]];
[man runAction:groupAction];

Need Your Help

XSLT add attribute value based on another value

xml xslt xpath xslt-2.0

How can i add values based on another value?

How to make UITabbarcontroller show over all other view controllers?

iphone objective-c uiviewcontroller uitabbarcontroller

I want the tabs at the bottom to always show, even when I present a modal view contoller. So when I call