本文介绍了修改正在运行的 SKAction 的速度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有这个代码:
@implementation MyScene {
SKAction *delayAction;
}
Inside a method:
delayAction = [SKAction waitForDuration:3.0];
[self runAction:[SKAction repeatActionForever: [SKAction sequence:
@[delayAction, [SKAction ...]]]]]
withKey:@"myKey"];
然后我想减少加班时间.(更新时调用此方法:)所以我尝试了:
Then i want to decrease duration overtime. (This method is called on update:)So i tried:
- (void)updateVelocity
{
NSLog(@"duration:%f",delayAction.duration);
delayAction.duration = delayAction.duration - 0.001;
}
我得到:
2014-04-04 11:45:05.781 FlyFish[5409:60b] duration:1.300000
2014-04-04 11:45:05.785 FlyFish[5409:60b] duration:1.299000
2014-04-04 11:45:05.800 FlyFish[5409:60b] duration:1.298000
2014-04-04 11:45:05.816 FlyFish[5409:60b] duration:1.297000
这看起来不错,但我的 [SKAction ...] 仍然在 3 秒后继续重复.
Which seems good, but my [SKAction ...] still continues repeating after 3 seconds.
推荐答案
我会以不同的方式来做这件事.像这样的...
I'd do this a different way. Something like this...
- (void)recursiveActionMethod
{
if (some end condition is met) {
return;
// this allows you to stop the repeating action.
}
self.duration -= 0.01;
// store duration in a property
SKAction *waitAction = [SKAction waitForDuration:self.duration];
SKAction *theAction = [SKAction doWhatYouWantHere];
SKAction *recursiveAcion = [SKAction performSelector:@selector(recursiveActionMethod) onTarget:self];
SKAction *sequence = [SKAction sequence:@[waitAction, theAction, recursiveAction]];
[self runAction:sequence];
}
这将执行您的操作,然后返回到此函数,以不同的等待时间再次运行,一次又一次,...
This will perform your action and then come back to this function to be run again with a different wait time and again, and again, ...
您甚至可以通过设置一些结束条件来停止序列,该条件会跳转到 if 块中并停止循环.
You can even stop the sequence by having some end condition that would jump inside the if block and stop the loop.
这篇关于修改正在运行的 SKAction 的速度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!