0
votes

I'm making a sprite kit game and I'm trying to add SKActions to it, but I have followed every guide I have found and nothing is working. Here is the code that incorporates the actions, right now the actions are supposed to just play once when the game starts, it has a two second delay before anything happens, so I don't miss seeing the animation.

SKAction* jumpAction;
SKAction* kneelAction;
SKAction* runAction;

-(id)initWithSize:(CGSize)size{
    if (self = [super initWithSize:size]) {
        [self setup];

        [self performSelector:@selector(spawnCloud) withObject:nil afterDelay:2.0];
        //[self performSelector:@selector(setupCharacter) withObject:nil afterDelay:4.0];
        [self setupCharacter];
        [self setupActions];
        [self createDpad];
        [self spawnStartupClouds];
        //self.physicsWorld.gravity = CGVectorMake(0.2,-2);
        self.physicsWorld.gravity = CGVectorMake(0.2 ,-2);
        self.physicsWorld.contactDelegate = self;

    }
    return self;
}

-(void) setupActions {
    SKTextureAtlas* atlas = [SKTextureAtlas atlasNamed:@"jump"];
    SKTexture* jumpTex1 = [atlas textureNamed:@"jump1.png"];
    SKTexture* jumpTex2 = [atlas textureNamed:@"jump2.png"];
    SKTexture* jumpTex3 = [atlas textureNamed:@"jump3.png"];

    NSArray* jumpAtlas = @[jumpTex1, jumpTex2, jumpTex3];

    SKAction* jumpAtlasAnimation = [SKAction animateWithTextures:jumpAtlas timePerFrame:0.1];
    SKAction* wait = [SKAction waitForDuration:2.5];

    jumpAction = [SKAction sequence:@[wait, jumpAtlasAnimation,]];
    NSLog(@"setupActions");
}
3
If you need any other info just ask. ;)MichaelD33
What is happening and how is it different from what you expect? Where if at all are you telling any node to run jumpAction? Is this the exact code? There's a syntax error in your action sequence.Mick MacCallum
This is the copy and pasted code from Xcode but is not all of the code in the project. And the expectation is for the character to run through the texture atlas of jumping, the character is not supposed to move but it is supposed to change textures.MichaelD33

3 Answers

0
votes

You are trying to run a SKAction but are not specifying what to run it on. "An SKAction object is an action that is executed by a node in the scene".

First create your SKSpriteNode (call it myNode for example) then run your SKAction on it. Something like this:

SKSpriteNode *myNode = [SKSpriteNode spriteNodeWithImageNamed:@"idle.png"];
// add rest of specs...

Then you can use your code:

SKTextureAtlas* atlas = [SKTextureAtlas atlasNamed:@"jump"];
SKTexture* jumpTex1 = [atlas textureNamed:@"jump1.png"];
SKTexture* jumpTex2 = [atlas textureNamed:@"jump2.png"];
SKTexture* jumpTex3 = [atlas textureNamed:@"jump3.png"];

NSArray* jumpAtlas = @[jumpTex1, jumpTex2, jumpTex3];

SKAction* jumpAtlasAnimation = [SKAction animateWithTextures:jumpAtlas timePerFrame:0.1];
SKAction* wait = [SKAction waitForDuration:2.5];

SKAction *jumpAction = [SKAction sequence:@[wait, jumpAtlasAnimation]];

[myNode runAction:jumpAction]; // <- now you are running the action on a node

Read up on "Running Actions" in the SKNode Class Reference.

0
votes

Are you ever calling runAction for your scene.

This might be why they arent working.

[self runAction:jumpAction];
0
votes

If I got this correct, you are trying to make jump animation with few images? I solved this problem in my game by not using atlases. So try this:

-(void)makePlayerJump:(SKSpriteNode *)player
{
    SKTexture *jump1 = [SKTexture textureWithImageNamed:@"jump1.png"];
    SKTexture *jump2 = [SKTexture textureWithImageNamed:@"jump2.png"];
    SKTexture *jump3 = [SKTexture textureWithImageNamed:@"jump3.png"];

    SKAction *wait = [SKAction waitForDuration: 2.5];
    SKAction *jumping = [SKAction animateWithTextures:@[jump1, jump2, jump3] timePerFrame:0.1];

   SKAction *jumpAction = [SKAction sequence:@[wait, jumping]];

   [player runAction:jumpAction];
}

When you wish to activate jumping do this:

[self makePlayerJump:myNode];

or

[self makePlayerJump:self.myNode];

Just depends how you declared it. Hope this works.