gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:21:23 25 4
gpt4 key购买 nike

我正在构建一个应用程序,当按下 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com