0

我试图在我的主菜单中实现一个动画开始按钮。因为我的场景需要一段时间才能加载,所以我想用那个按钮动画来缩短等待时间。不幸的是动画没有开始。我的代码有什么问题?

-(void)buttonAnimation{
    SKAction *HUDzoom = [SKAction scaleTo:3 duration:1];
    SKAction *HUDzoomOut = [SKAction scaleTo:1.0 duration:1];
    SKAction *HUDAnimation = [SKAction sequence:@[HUDzoom, HUDzoomOut]];

    [self.startButton runAction:[SKAction repeatActionForever:HUDAnimation]];
}

-(void)loadScene{
    SKScene *restart = [[Level_1 alloc] initWithSize:self.size];
    [self.view presentScene:restart];
}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

    UITouch *touch = [touches anyObject];
    CGPoint location = [touch locationInNode:self];

    SKNode *node = [self nodeAtPoint:location];

    if ([node.name isEqualToString:@"startLevel1"]){

        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
                [self loadScene];
            dispatch_async(dispatch_get_main_queue(), ^{
                [self buttonAnimation];
            });
        });

    }
}
4

1 回答 1

3

那是因为您异步加载场景,并且只有在完成之后您才异步启动按钮动画:

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
    // loading the scene
    [self loadScene];

    // when scene has finished loading, animate the button asynchronically
    // (this makes no sense)
    dispatch_async(dispatch_get_main_queue(), ^{
        [self buttonAnimation];
    });
});

相反,您应该启动动画,然后异步加载场景。

[self buttonAnimation];

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
        [self loadScene];
});

该按钮由 Sprite Kit 动作制作动画,虽然您可以异步启动动画,但它不会异步制作整个动画。相反,您只需要确保像 loadScene 这样的任何阻塞方法异步运行。

于 2014-09-12T10:08:13.010 回答