如何通过重复触摸不中断动画完成? (精灵套件Atlas)

时间:2013-11-15 19:57:48

标签: animation xcode5 sprite-kit

我正在使用IOS精灵工具包来创建一个由touch驱动的动画(在.atlas文件中)。如何通过重复触摸不中断动画完成? 我知道我忽略了一些非常简单的事情。

-(void) setUpActions {

SKTextureAtlas *atlas = [SKTextureAtlas atlasNamed:@"Wheel"];

SKTexture *Wheel1 = [atlas textureNamed:@"Wheel1.png"];
SKTexture *Wheel2 = [atlas textureNamed:@"Wheel2.png"];
SKTexture *Wheel3 = [atlas textureNamed:@"Wheel3.png"];
SKTexture *Wheel4 = [atlas textureNamed:@"Wheel4.png"];
SKTexture *Wheel5 = [atlas textureNamed:@"Wheel5.png"];
SKTexture *Wheel6 = [atlas textureNamed:@"Wheel6.png"];
SKTexture *Wheel7 = [atlas textureNamed:@"Wheel7.png"];
SKTexture *Wheel8 = [atlas textureNamed:@"Wheel8.png"];
SKTexture *Wheel9 = [atlas textureNamed:@"Wheel9.png"];

NSArray *atlasTexture = @[Wheel1, Wheel2, Wheel3, Wheel4, Wheel5, Wheel6, Wheel7, Wheel8, Wheel9];

SKAction* atlasAnimation = [SKAction animateWithTextures:atlasTexture timePerFrame:0.1];

   SKAction *resetTexture = [SKAction setTexture:[SKTexture textureWithImageNamed:@"Wheel1.png"] ];

runAnimation = [SKAction sequence:@[atlasAnimation,resetTexture]];

}

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {

    SKSpriteNode* Wheel = (SKSpriteNode*)[self childNodeWithName:@"Wheel"];
    [Wheel runAction:runAnimation];

   if (_tapCount < 1) {

      //How do I cancel the touches until the animation is complete?
       NSLog(@"STOP touches???");
   }
}

2 个答案:

答案 0 :(得分:1)

首先,有一种更简单的方法来添加纹理:

    NSMutableArray *animations = [[NSMutableArray alloc] init];

    for (int i = 0; i < [atlas.textureNames count]; i++) {
    NSString *temp = [NSString stringWithFormat:@"Wheel%d.png", i + 1]; // name your animations from zero to avoid adding 1 here
    SKTexture *texture = [atlas textureNamed:temp];
    [animations addObject:texture];
}

关于你的问题:

将动画方法更改为:

SKAction* atlasAnimation = [SKAction animateWithTextures:atlasTexture timePerFrame:0.1 withKey:"wheelAnimation"];

您可能应该从其他方法调用动画启动。然后在触摸方法中,您可以检查

 if ([Wheel actionForKey:@"wheelAnimation"]) {
    return;
    }

这将退出touches方法而不在return语句后执行代码。

或者在您的代码中:

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {

     SKSpriteNode* Wheel = (SKSpriteNode*)[self childNodeWithName:@"Wheel"];

    if (![Wheel actionForKey:@"wheelAnimation"]) { // everything in this block will not trigger if it finds animation
        [Wheel runAction:runAnimation];

       if (_tapCount < 1) {

          //How do I cancel the touches until the animation is complete?
           NSLog(@"STOP touches???");
       }
     }
   }

另请注意,触摸实际上是从touchesBegin方法开始的,因此您最好将代码放在那里。

答案 1 :(得分:0)

您可以通过在序列中再添加两个操作来完成此操作:

-(void) setUpActions {

    SKTextureAtlas *atlas = [SKTextureAtlas atlasNamed:@"Wheel"];

    // build atlasTexture array dynamically

    NSMutableArray *atlasTexture = [NSMutableArray array];

    int totalWheelImages = atlas.textureNames.count;

    for (int i=1; i <=totalWheelImages; i++) {
        NSString *textureName = [NSString stringWithFormat:@"Wheel%d", i];

        SKTexture *temp = [atlas textureNamed:textureName];

        [atlasTexture addObject:temp];

    }

    // SKAction to stop user interaction

    SKAction *start = [SKAction runBlock:^{

        [self setUserInteractionEnabled:FALSE];

        }];

    SKAction* atlasAnimation = [SKAction animateWithTextures:atlasTexture timePerFrame:0.1];

    SKAction *resetTexture = [SKAction setTexture:[SKTexture textureWithImageNamed:@"Wheel1.png"] ];

    // SKAction to restart user interaction

    SKAction *end = [SKAction runBlock:^{

        [self setUserInteractionEnabled:TRUE];

        }];

    runAnimation = [SKAction sequence:@[start, atlasAnimation, resetTexture, end]];

}