1

我编写了一个在模拟器上完美运行的演示应用程序,但是当我将它放在实际设备上时,骰子会跳来跳去。这是一个视频作为示例。重新启动应用程序后,动画很好。错误发生在重复按下滚动按钮大约 1 分钟的时间。

http://youtu.be/N1k1QPa1brg

代码全部位于:

https://github.com/rnystrom/MartionDemo 我如何在骰子对象中制作动画:

CCAnimation *anim = [CCAnimation animationWithFrames:frames delay:delay];
if(self.sprite){
    // Animate the sprite
    [self.sprite runAction:[CCAnimate actionWithAnimation:anim restoreOriginalFrame:NO]];
}

滚动功能:

-(void)roll
{
    // Array that contains the new positions of dice
    // Predetermine the position, check if that will be on top of other dice
    NSMutableArray* checkPos = [NSMutableArray array];

    for(Dice* d in rollDiceArray){
        [d resetPosition];    

        // Select a random position within bounds
        NSInteger x = arc4random() % 600 + 50;
        NSInteger y = arc4random() % 600 + 150;
        CGPoint location = CGPointMake(x, y);

        // Check if die will touch other dice
        while (! [self checkPositionWithPoint:location array:checkPos]) {
            // If position overlaps another die, get a new position
            // PROBLEM: This is O(infinity)!
            x = arc4random() % 600 + 50;
            y = arc4random() % 600 + 150;
            location = CGPointMake(x, y);
        }

        // If position does not overlap, add it to array of positions to be checked
        [checkPos addObject:[NSArray arrayWithObjects:[NSNumber numberWithInteger:x], [NSNumber numberWithInteger:y], nil]];

        // Animate the dice to a position
        // Addition in the switch is for some randomness and correcting the animation's timing offset
        NSInteger numberFrames;
        float frameRate;
        float mod = (float)(arc4random() % 60) / 100;
        switch (d.fileNum) {
            case 0:
                numberFrames = kRayFrames;
                frameRate = numberFrames/24 + mod;
                break;
            case 1:
                numberFrames = kRayFrames;
                frameRate = numberFrames/24 + mod - 0.4;
                break;
            case 2:
                numberFrames = kTankFrames;
                frameRate = numberFrames/24 + mod + 0.2;
                break;
            case 3:
                numberFrames = kChickenFrames;
                frameRate = numberFrames/24 + mod;
                break;
            case 4:
                numberFrames = kCowFrames;
                frameRate = numberFrames/24 + mod + 0.2;
                break;
            case 5:
                numberFrames = kHumanFrames;
                frameRate = numberFrames/24;
                break;
            default:
                break;
        }

        id action = [CCMoveTo actionWithDuration:frameRate position:location];
        id ease = [CCEaseOut actionWithAction:action rate:4.0];
        [d.sprite runAction:ease];
    }
}
4

1 回答 1

0

原来这是一个 performSelector:afterDelay: 问题。我通过在延迟中添加一些填充时间来解决它,这样事情就不会同时发生。

我还放置了一个块,以便只有在动画完成后才能进行动作。中断 CCMoveTo 式动画时似乎存在某种问题。

于 2012-06-02T15:16:54.383 回答