In the game I am working on you can move a character from left to right by touching the left/right sides of the screen. What I'm trying to do is to make it that if you're touching the left side and then touch the right side, the character starts moving right instead of left; the action to move left is overridden. I have currently made it so that the game is single-touch but the method of overriding the previous touch is where I'm stuck at. My code:
- (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);
}
}
self.userinteractionEnabled
changing is not even needed, you want to keep taking the latest touch, and process its direction – Knight0fDragon