ios - Sprite kit - 处理快速序列中的重叠动画

标签 ios objective-c sprite-kit

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

除非以极快的顺序按下按钮,否则一切正常。 例如,假设用户按下按钮 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];}
}

最佳答案

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

- (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: 您可以将键添加到具有完成 block 的 SKAction,但您可以将 runBlock 添加到 SKAction 序列的末尾以执行相同的操作。这是一个例子:

    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"];

关于ios - Sprite kit - 处理快速序列中的重叠动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25410672/

相关文章:

ios - 通过iOS应用程序控制低电量模式

ios - 如何在 iPad 应用程序中创建类似于 Web 浏览器选项卡的选项卡

iphone - facebook 类型的左滑动条布局应该出现在所有 View Controller 中

ios - SKLabelNode 删除前导和尾随空格 - 我怎样才能阻止它?

ios - 如何通过拖动手指在两个 View 之间执行翻转动画?

iphone - 每次应用启动时仅调用一次方法

ios - 模拟 UIScrollView 减速

ios - 根据 Sprite 的当前位置让 Sprite 移动到某个位置?

ios - SKShapeNode 框架包含点 (0, 0) 即使 CGPath 不

ios - 数据未传递到 Storyboard 中的详细 View Controller