0

I have a crash in interesting form. I setUp SKAction like

-(id)initWithSize:(CGSize)size
{
    if (self = [super initWithSize:size])
    {
        self.backgroundColor = [SKColor colorWithWhite:255 alpha:1];

        [self createNinja];
        [self setUpJump];
    }
    return self;
}

- (void)setUpJump
{
    SKTextureAtlas *jumpAtlas = [SKTextureAtlas atlasNamed:@"Ninja_jump"];

    SKTexture *jump1 = [jumpAtlas textureNamed:@"Ninja_jump_1"];
    SKTexture *jump2 = [jumpAtlas textureNamed:@"Ninja_jump_2"];
    SKTexture *jump3 = [jumpAtlas textureNamed:@"Ninja_jump_3"];
    SKTexture *jump4 = [jumpAtlas textureNamed:@"Ninja_jump_4"];

    SKAction *jumpUpAnimation = [SKAction animateWithTextures:@[jump1, jump2, jump3, jump4]
                                             timePerFrame:0.07];

    SKAction *jumpDownAnimation = [SKAction animateWithTextures:@[jump3, jump2, jump1, [SKTexture textureWithImageNamed:@"Ninja"]]
                                               timePerFrame:0.07];

    SKAction *wait = [SKAction waitForDuration:0.3];

    self.jumpAction = [SKAction sequence:@[jumpUpAnimation, wait, jumpDownAnimation]];
}

But when I don't run this action on first SKScene and go to other SKScene, when I set up the same action, I have a crash

enter image description here

But if I run this action on first SKScene, everything is OK on next SKScene.

Is there problem in SpriteKit in SKTexture?

Run my action like that

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    for (UITouch *touch in touches)
    {
        CGPoint location = [touch locationInNode:self];

        SKSpriteNode *ninja = (SKSpriteNode *)[self childNodeWithName:@"ninja"];

        if (location.x > 230 &&
           (location.x < 419 && location.y > 500))
        {
            [ninja runAction:self.jumpAction];
        }
    }
}
Roman
  • 366
  • 5
  • 19

1 Answers1

0

Not 100% sure what you are saying but if I read you right, you are setting up an SKAction property jumpAction in the first scene and are referring to that action in the other scene's touchesBegan: method(?):

[ninja runAction:self.jumpAction];

If you are not setting up the self.jumpAction for your second scene, then you are essentially saying [ninja runAction:nil];

I personally find it a good idea to tie actions as close as possible to the node that is supposed to perform it (in this case the sprite). If you are reusing a lot of actions for different nodes creating an ActionFactory class could be another way to go about it...

T. Benjamin Larsen
  • 6,373
  • 4
  • 22
  • 32
  • Actually no:) I setting this property on first scene AND on the second, the same code. For example if I doing that not using junpAction I have a crash, but init jump action actually in initWithSize SKScene *gameOver = [[MyScene alloc] initWithSize:self.size]; SKTransition *transition = [SKTransition doorsCloseVerticalWithDuration:2]; [self.view presentScene:gameOver transition:transition]; – Roman Feb 26 '14 at 08:27
  • Could you add some more code to your original post, your init-method where you set up the `self.jumpAction` property for instance? – T. Benjamin Larsen Feb 26 '14 at 09:39
  • ...and you are obviously(?) setting up the `ninja` instance in 100% identical ways? Have you tried logging the instance after the `SKSpriteNode *ninja = (SKSpriteNode *)[self childNodeWithName:@"ninja"];` line in both classes to see that is is not somehow nil in either one? – T. Benjamin Larsen Feb 28 '14 at 06:57
  • yes 100%. ninjas are different and both not nil. And jumpAction not nil and different – Roman Feb 28 '14 at 16:06
  • just on screen on top. Not NSZombie nor Exception break point are saying something. I think problem that maybe spritekit is cashing SKTexture? – Roman Mar 01 '14 at 08:47