我目前正在从事某种自上而下的赛车手,如果你愿意的话。这个想法是有两个按钮,每个都可以将汽车左转或右转。这一切都有效,但只能一触即发。我想让玩家只需按住按钮即可转动汽车。我现在有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循环,代码将起作用。任何帮助表示赞赏!
非常感谢!对于有同样问题的其他人,我偶然发现了另一种连续旋转节点的方式。我创建了'SKAction * rotation = [SKAction rotateByAngle:playerAngle duration:0.2];'这个旋转节点,并且在接触中永远重复那个动作,然后在touchesEnded中移除ActionAction。这也让你有一个选择,使它更平稳一段时间。我不知道这会不会在以后造成错误,但现在它的作品! – Jeroen