繁体   English   中英

按钮在Sprite-Kit中按下动画

[英]Button pressed animation in Sprite-Kit

在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]];
    }
}

在切换到另一个场景之前,这将给你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]]];
    }
}

此外,当用户将手指从按钮上擦掉时,我想让按钮动画回来。

这似乎毫无意义,因为当用户按下按钮时,您正在转换到另一个场景。

-(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
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM