Sprite-Kit 中的按钮按下动画
Posted
技术标签:
【中文标题】Sprite-Kit 中的按钮按下动画【英文标题】:Button pressed animation in Sprite-Kit 【发布时间】:2014-04-23 12:51:48 【问题描述】:在 Sprite-Kit 游戏中,我想在按钮被按下时制作动画。现在代码直接做出反应,而不是等到动画执行。此外,当用户将手指从按钮上擦掉时,我希望按钮能够动画回来。
Here my code:
-(void)addStartButton:(CGSize)size
self.start = [SKSpriteNode spriteNodeWithImageNamed:@"startButton1"];
self.start.position = CGPointMake(size.width/2, size.height/2);
self.start.name = @"start";
[self addChild:self.start];
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInNode:self];
SKNode *node = [self nodeAtPoint:location];
if ([node.name isEqualToString:@"start"])
self.start.texture = [SKTexture textureWithImageNamed:@"startButton2"];
MyScene *myScene = [MyScene sceneWithSize:self.size];
[self.view presentScene:myScene transition:[SKTransition pushWithDirection:SKTransitionDirectionLeft duration:0.5]];
【问题讨论】:
【参考方案1】:这会让你在切换到另一个场景之前有 2 秒的延迟:
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInNode:self];
SKNode *node = [self nodeAtPoint:location];
if ([node.name isEqualToString:@"start"])
SKAction *changeTexture = [SKAction runBlock:^
self.start.texture = [SKTexture textureWithImageNamed:@"startButton2"];
];
SKAction *wait = [SKAction waitForDuration:2.f];
SKAction *presentAnotherScene = [SKAction runBlock:^
MyScene *myScene = [MyScene sceneWithSize:self.size];
[self.view presentScene:myScene transition:[SKTransition pushWithDirection:SKTransitionDirectionLeft duration:0.5]];
];
[self runAction:[SKAction sequence:@[changeTexture,wait,presentAnotherScene]]];
此外,我希望当用户将手指从按钮上擦掉时,按钮会重新生成动画。
这似乎毫无意义,因为当用户按下按钮时,您正在转换到另一个场景。
【讨论】:
【参考方案2】:-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInNode:self];
SKNode *node = [self nodeAtPoint:location];
if ([node.name isEqualToString:@"start"])
startButton.texture = [SKTexture textureWithImageNamed:@"startButton2"];
elsestartButton.texture = [SKTexture textureWithImageNamed:@"startButton1"];
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInNode:self];
SKNode *node = [self nodeAtPoint:location];
if ([node.name isEqualToString:@"start"])
startButton.texture = [SKTexture textureWithImageNamed:@"startButton2"];
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInNode:self];
SKNode *node = [self nodeAtPoint:location];
if ([node.name isEqualToString:@"start"])
//Start Button Actions
【讨论】:
以上是关于Sprite-Kit 中的按钮按下动画的主要内容,如果未能解决你的问题,请参考以下文章