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

ボタンはスプライト キット アクションによってアニメーション化されますが、アニメーションを非同期で開始することはできますが、アニメーション全体が非同期になるわけではありません。代わりに、loadScene などのブロッキング メソッドが非同期で実行されるようにする必要があります。

于 2014-09-12T10:08:13.010 に答える