精灵套件 - 以快速顺序处理重叠动画

时间:2014-08-20 17:12:50

标签: ios objective-c sprite-kit

我正在构建一个应用程序,其中当按下精灵时,它执行一个非常简单的动画,其中初始图像被新图像替换,然后在短暂停顿后再次应用初始图像。有2个这样的精灵。

除非按极快的顺序按下按钮,否则一切正常。 让我们举例说,用户按下按钮1然后非常快地按下按钮2(在此之前有时间恢复到初始图像):在这种情况下,按钮1仍然卡在新图像上!

即使我禁用与sprite的交互(例如在动画正在进行时重命名或插入不同的标志),仍然会注册touchesBegan事件,我相信这足以让sprite 1卡在新的图像,而不是恢复到最初的一个!

我已经没想完了......有什么建议吗?欢呼声。

- (void)SwitchButtonImage
{    
    touchedNode.texture = [SKTexture textureWithImageNamed:ImageNew]; 

    //After a pause change sprite image to the initial image
    SKAction *wait = [SKAction waitForDuration: 0.3]; 

    [touchedNode runAction: wait completion:^
     {
         touchedNode.texture = [SKTexture textureWithImageNamed:ImageInitial];
     }];
}

 -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    UITouch *touch = [touches anyObject]; 
    CGPoint positionInScene = [touch locationInNode:self];         
    [self selectNodeForTouch:positionInScene];
} 

- (void)selectNodeForTouch:(CGPoint)touchLocation 
{
    touchedNode = (SKSpriteNode *)[self nodeAtPoint:touchLocation]; 

    _selectedNode = touchedNode; 

    if([[touchedNode name] isEqualToString:kButtonNodeName_01]) 
    {[self SwitchButtonImage];} 

    else if([[touchedNode name] isEqualToString:kButtonNodeName_02]) 
    {[self SwitchButtonImage];}
}

2 个答案:

答案 0 :(得分:1)

编辑:我编辑了我的答案,处理了多个按钮并使用原始代码。

- (void)SwitchButtonImage
{
    touchedNode.texture = [SKTexture textureWithImageNamed:@"NewImage"];

    //After a pause change sprite image to the initial image
    SKAction *wait = [SKAction waitForDuration: 0.3];
    SKTexture *texture = [SKTexture textureWithImageNamed:@"InitialImage"];
    SKAction *resetImage = [SKAction setTexture:texture];

    // Add action with a unique identifier
    [touchedNode runAction:[SKAction sequence:@[wait, resetImage]] withKey:@"ResetImage"];
}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [touches anyObject];
    CGPoint positionInScene = [touch locationInNode:self];
    [self selectNodeForTouch:positionInScene];
}


- (void)selectNodeForTouch:(CGPoint)touchLocation {
    touchedNode = (SKSpriteNode *)[self nodeAtPoint:touchLocation];
    _selectedNode = touchedNode;
    if([[touchedNode name] isEqualToString:kButtonNodeName_01]) {
        // Change image only if previous action is done
        if (![touchedNode actionForKey:@"ResetImage"]) {
            [self SwitchButtonImage];
        }
    }
    else if([[touchedNode name] isEqualToString:kButtonNodeName_02]) {
        // Change image only if previous action is done
        if (![touchedNode actionForKey:@"ResetImage"]) {
            [self SwitchButtonImage];
        }
    }
}

编辑2:您可以向具有完成块的SKAction添加密钥,但是您可以在SKAction序列的末尾添加runBlock来执行相同操作。这是一个例子:

    SKAction *action1 = [SKAction rotateByAngle:M_PI duration:1];
    SKAction *action2 = [SKAction rotateByAngle:-M_PI duration:1];
    SKAction *completed = [SKAction runBlock:^{
        NSLog(@"I am done");
    }];
    SKAction *action = [SKAction sequence:@[action1, action2, completed]];

    [sprite runAction:action withKey:@"actionKey"];

答案 1 :(得分:0)

您可以使用块和BOOL ivar来完成此操作。

创建一个ivar:

BOOL readyForNextAnimation;

设置这样的块:

SKAction *block0 = [SKAction runBlock:^{
                // your animation code here
            }];

SKAction *wait0 = [SKAction waitForDuration:0.3]; // your animation time length

SKAction *block1 = [SKAction runBlock:^{
                readyForNextAnimation = true;
            }];

[self runAction:[SKAction sequence:@[block0, wait0, block1]]];

以上代码将使用如下代码:

// some trigger just occurred
if(readyForNextAnimation == true)
{
    readyForNextAnimation = false;

    // create blocks 0,1,2 and run them as above
}

如果您需要做其他事情,只需创建更多块并将它们添加到运行序列中。