我正在构建一个应用程序,当按下 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-推荐答案 strong>
编辑:我编辑了我的答案以处理多个按钮并使用您的原始代码。
- (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/
|