• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

ios - Sprite kit - 快速处理重叠动画

[复制链接]
菜鸟教程小白 发表于 2022-12-13 00:55:43 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我正在构建一个应用程序,当按下 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)touchesBeganNSSet *)touches withEventUIEvent *)event 
{ 
    UITouch *touch = [touches anyObject]; 
    CGPoint positionInScene = [touch locationInNode:self];         
    [self selectNodeForTouch:positionInScene];
} 

- (void)selectNodeForTouchCGPoint)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];}
}



Best Answer-推荐答案


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

- (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)touchesBeganNSSet *)touches withEventUIEvent *)event {
    UITouch *touch = [touches anyObject];
    CGPoint positionInScene = [touch locationInNode:self];
    [self selectNodeForTouch:positionInScene];
}


- (void)selectNodeForTouchCGPoint)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/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap