SpriteKit动画问题

时间:2014-04-12 19:59:47

标签: ios objective-c animation sprite sprite-kit

我的游戏中的精灵动画存在问题。

在下面发布的当前代码中,精灵将显示在屏幕上,但不会执行步行动画;它只是步行周期中其中一个图像的静态帧。

init函数:

-(id)initWithSize:(CGSize)size {
    if (self = [super initWithSize: size]) {
        self.backgroundColor = [SKColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:1.0];

        // Create the game nodes
        // Background
        _backgroundNode = [self createBackgroundNode];
        [self addChild: _backgroundNode];

        // Foreground
        _foregroundNode = [SKNode node];
        [self addChild: _foregroundNode];

        // Add the Player
        _thePlayer = [self createPlayer];
        [_foregroundNode addChild:_thePlayer];
        [self walkingQueen];

    }
    return self;
}

创建播放器/填充动画数组的功能:

-(SKSpriteNode *)createPlayer
{
    SKSpriteNode *playerNode = [SKSpriteNode node];
    //SKSpriteNode *_player = [SKSpriteNode node];

    NSMutableArray *walkFrames = [NSMutableArray array];
    SKTextureAtlas *queenAnimatedAtlas = [SKTextureAtlas atlasNamed: @"QueenSprites"];

    SKTexture *walk1 = [queenAnimatedAtlas textureNamed:@"queen7"];
    SKTexture *walk2 = [queenAnimatedAtlas textureNamed:@"queen8"];
    SKTexture *walk3 = [queenAnimatedAtlas textureNamed:@"queen9"];

    walkFrames[0] = walk1;
    walkFrames[1] = walk2;
    walkFrames[2] = walk3;

    _queenWalkingFrames = walkFrames;

    /*
    int numImages = 9;
    for (int i = 7; i <= numImages; i++) {
        NSString *textureName = [NSString stringWithFormat:@"queen%d", i];
        SKTexture *temp = [queenAnimatedAtlas textureNamed: textureName];
        [walkFrames addObject: temp];
    }
    */
    //_queenWalkingFrames = walkFrames;

    SKTexture *temp = _queenWalkingFrames[0];
    SKSpriteNode *_player = [SKSpriteNode spriteNodeWithTexture:temp];
    _player.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame));
    //[self addChild:_player];
    //[self walkingQueen];

    [playerNode addChild:_player];
    [self walkingQueen];

    return playerNode;
}

启动步行动画的功能:

-(void)walkingQueen
{
    for (int i = 0; i < _queenWalkingFrames.count; i++) {
        if (_queenWalkingFrames[i] == NULL) {
            NSLog(@"ERROR!");
        }
    }

    [_thePlayer runAction:[SKAction repeatActionForever:
                        [SKAction animateWithTextures:_queenWalkingFrames
                                         timePerFrame:0.1f
                                               resize:NO
                                              restore:YES]] withKey:@"walkingInPlaceQueen"];

    //return;
}

我玩了一些代码并且动画可以工作如果我忽略了图层的概念(背景/前景层)并且将玩家的创建坚持到init函数中(基本上把它放在了createPlayer()并将其粘贴在initWithSize()中。

我确实需要为我的游戏设置图层,一切看起来都应该有效;不知道将几行代码移动到自己的功能块中会产生这样的问题。 walkQueen()函数内部的检查没有返回任何错误,因此我假设我的数组已经填充了精灵图像。

2 个答案:

答案 0 :(得分:1)

    _thePlayer = [self createPlayer];

这意味着_thePlayer在createPlayer方法返回之前是nil。因此,在walkingQueen方法_thePlayer是nil并且动作不运行。

替换它:

SKSpriteNode *playerNode = [SKSpriteNode node];

_thePlayer = [SKSpriteNode node];

并使用_thePlayer而不是playerNode。您也可以跳过返回并指定它,毕竟它是一个ivar。

答案 1 :(得分:0)

重写了createPlayer()函数。正确的实现更简单,如下:

-(SKSpriteNode *)createPlayer
{
    NSMutableArray *walkFrames = [NSMutableArray array];
    SKTextureAtlas *queenWalkingAtlas = [SKTextureAtlas atlasNamed:@"QueenSprites"];

    SKTexture *walk1 = [queenWalkingAtlas textureNamed:@"queen7"];
    SKTexture *walk2 = [queenWalkingAtlas textureNamed:@"queen8"];
    SKTexture *walk3 = [queenWalkingAtlas textureNamed:@"queen9"];

    walkFrames[0] = walk1;
    walkFrames[1] = walk2;
    walkFrames[2] = walk3;

    _queenWalkingFrames = walkFrames;

    SKTexture *temp = _queenWalkingFrames[0];
    _thePlayer = [SKSpriteNode spriteNodeWithTexture:temp];
    _thePlayer.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame));

    return _thePlayer;
}

基本上按照上面提到的帖子做,摆脱ivars,使用函数ONLY用适当的图像填充数组。然后,在init函数中,调用createPlayer(),将其添加到前景层,然后调用walkingQueen()。