2015-04-21 62 views
0

我目前正在从事某种自上而下的赛车手,如果你愿意的话。这个想法是有两个按钮,每个都可以将汽车左转或右转。这一切都有效,但只能一触即发。我想让玩家只需按住按钮即可转动汽车。我现在有Spritekit旋转节点,同时触摸

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { 
    /* Called when a touch begins */ 

    for (UITouch *touch in touches) { 
     touched = YES; 
     CGPoint location = [touch locationInNode:self]; 
     SKNode *node = [self nodeAtPoint:location]; 

     if ([node.name isEqualToString:@"buttonLeft"]) { 
      while (touched == YES) { 
       playerRotate = player.zRotation; 
       playerRotate += playerAngle; 
      } 
     } 
    } 
} 

-(void)update:(CFTimeInterval)currentTime { 
    /* Called before each frame is rendered */ 
    player.zRotation = playerRotate; 
    float dx = playerSpeed * cos(player.zRotation); 
    float dy = playerSpeed * sin(player.zRotation); 
    player.physicsBody.velocity = CGVectorMake(dx, dy); 
} 

在更新功能中更新玩家汽车的速度。并在touchesEnded被设置为NO。问题是这个while循环卡住了(我的手机不再接触任何东西),但只要按下按钮,我就知道没有其他方法可以继续旋转汽车。如果我删除while循环,代码将起作用。任何帮助表示赞赏!

回答

0

你已经有了正确的想法。而不是将您的BOOL触摸到所有触摸的通用YES。创建一个touchLeft和touchRight BOOL。

当您的if([node.name isEqualToString:@"buttonLeft"])将touchLeft BOOL设置为YES后。创建一个if([node.name isEqualToString:@"buttonRight"])并设置touchRight BOOL。

在您的更新方法中,您可以检查BOOL YES值并相应地应用代码。

请记住,您还需要为touchesEnded创建相同的代码逻辑,以便在触摸不再存在时将BOOL设置为NO。

+0

非常感谢!对于有同样问题的其他人,我偶然发现了另一种连续旋转节点的方式。我创建了'SKAction * rotation = [SKAction rotateByAngle:playerAngle duration:0.2];'这个旋转节点,并且在接触中永远重复那个动作,然后在touchesEnded中移除ActionAction。这也让你有一个选择,使它更平稳一段时间。我不知道这会不会在以后造成错误,但现在它的作品! – Jeroen