我试图在主菜单中实现一个动画启动按钮。因为加载场景需要一段时间,所以我想用该按钮动画来缩短等待时间。不幸的是,动画无法开始。我的代码有什么问题?
-(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];
});
});
}
}
这是因为您是异步加载场景的,只有在这样做之后,您才能异步启动按钮动画:
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)都可以异步运行。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句