SpriteKit在其他SKScene上运行动作时崩溃

时间:2014-02-20 09:44:13

标签: ios sprite-kit skaction sktexture sktextureatlas

我有一种有趣的崩溃形式。我设置SKAction喜欢

-(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]];
}

但是当我没有在第一个SKScene上运行此操作并转到其他SKScene时,当我设置相同的操作时,我发生了崩溃

enter image description here

但如果我在第一个SKScene上运行此操作,则下一个SKScene的所有内容都可以。{/ p>

SpriteKit中的SKTexture是否存在问题?

像那样运行我的行动

-(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];
        }
    }
}

1 个答案:

答案 0 :(得分:0)

不是100%肯定你在说什么,但如果我读了你,你在第一个场景中设置一个SKAction属性jumpAction,并在另一个场景的touchesBegan:方法(?)中引用该动作:

[ninja runAction:self.jumpAction];

如果您没有为第二个场景设置self.jumpAction,那么您基本上是在说[ninja runAction:nil];

我个人认为将动作尽可能接近应该执行它的节点(在本例中为精灵)是个好主意。如果你为不同的节点重复使用很多动作,那么创建一个ActionFactory类可能是另一种方法......