是否有一种聪明的方法来设置一系列SKActions的总持续时间?

时间:2018-03-23 20:15:04

标签: objective-c sprite-kit sequence skaction

我认为答案是否定的,但这就是我正在做的事情,然后我将展示我希望能做什么。总的来说,我要求的是一系列阻尼旋转,总共24秒。

#define START_ANGLE         M_PI/4.0
#define SPRING_TENSION      0.9
#define START_DURATION      1.3     // Hack!

NSMutableArray *animationA = [NSMutableArray array];
double a = START_ANGLE;
double d = START_DURATION;
while (a * a > 0.01) {
    // Bounce to the other side.
    SKAction *a1 = [SKAction rotateToAngle:a duration:d];
    a1.timingMode = SKActionTimingEaseIn;
    [animationA addObject:a1];

    // Return to rest
    SKAction *a2 = [SKAction rotateToAngle:0 duration:d];
    a2.timingMode = SKActionTimingEaseOut;
    [animationA addObject:a2];

    // Reduce the height of the bounce by the spring's tension
    a *= -SPRING_TENSION;
    d *= SPRING_TENSION;
}
SKAction *seqA = [SKAction sequence:animationA];
[mySprite runAction:seqA];

我想要的是总序列持续时间为24秒,并且从中推断出干预动作。由于API不允许这样做,为了达到这个解决方案,我必须预先计算循环的迭代次数,并跟踪角度的确认,以便到达START_DURATION。

这很糟糕且不灵活 - 我已经不得不展开循环以找出每个组件的持续时间,但如果我想稍后更改SPRING_TENSION但保持总持续时间怎么办?我必须再次展开循环并重新计算START_DURATION。

我希望我能做出更灵活的事情:

#define START_ANGLE         M_PI/4.0
#define SPRING_TENSION      0.9
#define TOTAL_DURATION      24.0

NSMutableArray *animationA = [NSMutableArray array];
double a = START_ANGLE;
while (a * a > 0.01) {
    // Bounce to the other side.
    SKAction *a1 = [SKAction rotateToAngle:a];  // Duration will be computed from total duration
    a1.timingMode = SKActionTimingEaseIn;
    [animationA addObject:a1];

    // Return to rest
    SKAction *a2 = [SKAction rotateToAngle:0];  // Duration will be computed from total duration
    a2.timingMode = SKActionTimingEaseOut;
    [animationA addObject:a2];

    // Reduce the height of the bounce by the spring's tension
    a *= -SPRING_TENSION;
    d *= SPRING_TENSION;
}
SKAction *seqA = [SKAction sequence:animationA duration:TOTAL_DURATION];      // This computes all undefined component durations
[mySprite runAction:seqA];

这样,我得到了我想要的东西:我得到一个选定持续时间的动画序列,让框架找出介入的旋转。这类似于CAKeyframeAnimation如何推断关键帧。有没有一种聪明的方法来使用我忽略的SpriteKit做到这一点?

0 个答案:

没有答案