在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]];
}
}
答案 0 :(得分:3)
在切换到另一个场景之前,这将给你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]]];
}
}
此外,当用户将手指从按钮上擦掉时,我想让按钮动画回来。
这似乎毫无意义,因为当用户按下按钮时,您正在转换到另一个场景。
答案 1 :(得分:0)
-(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"];
}else{startButton.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
}