0

我有一个有趣的形式崩溃。我设置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时,当我设置相同的动作时,我会崩溃

在此处输入图像描述

但是,如果我首先运行此操作SKScene,接下来一切正常SKScene

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

1 回答 1

0

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

[ninja runAction:self.jumpAction];

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

我个人认为将动作尽可能靠近应该执行它的节点(在本例中为精灵)是一个好主意。如果您为不同的节点重用大量操作,则创建 ActionFactory 类可能是另一种解决方法......

于 2014-02-24T08:32:34.393 回答