在我正在制作的游戏中,您可以通过触摸屏幕的左/右侧从左向右移动角色。我想要做的是,如果你触摸左侧然后触摸右侧,角色开始向右而不是向左移动;向左移动的动作被覆盖。我目前已经做到这一点,以便游戏是单点触控,但是覆盖之前触摸的方法是我被困住的地方。我的代码:
- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInNode:self];
if (location.x < screenWidth/2){
leftMovement = YES;
[player runLeft];
}
else {
rightMovement = YES;
[player runRight];
}
self.userInteractionEnabled = NO;
}
- (void) touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
[self playerStop];
self.userInteractionEnabled = YES;
}
- (void) update:(NSTimeInterval)currentTime {
if (isTouched && leftMovement){
player.physicsBody.velocity=CGVectorMake(-PLAYERSPEED,
player.physicsBody.velocity.dy);
}
else if (isTouched && rightMovement){
player.physicsBody.velocity=CGVectorMake(PLAYERSPEED,
player.physicsBody.velocity.dy);
}
}
答案 0 :(得分:2)
您应该考虑的一件事是当用户触摸屏幕的另一侧时更改leftMovement
和rightMovement
的值,否则,update
方法中的正确移动将永远不要被援引。
if (location.x < screenWidth/2){
leftMovement = YES;
rightMovement = NO;
[player runLeft];
}
else {
rightMovement = YES;
leftMovement = NO;
[player runRight];
}