enough is enough.
like the title says, I'm in a bit of a pickle.
so here's the outline of my problem. I have this ship right. It rests in the center bottom of the screen (240,0). When the user touches to the right at a point greater than 240 the ship moves right. And left if the user touches at a point less than 240. HOWEVER this is great when there's only one finger on the screen at a time. I need it to where if the user touches right, doesn't lift their finger, then touches left (OH CRAP THERE'S TWO FINGERS ON THE SCREEN) the ship keeps moving right until they lift their right finger, then suddenly the ship moves left towards the second touch.
I'm using timers to add/subtract a number from the current x coordinate to get it to move.
This is what I have in a nutshell: (Don't laugh or hate I'm a noob)
-(void)moveShipLeft:(id)sender {
shipView.center = CGPointMake(shipView.center.x - 2, 320 - (shipView.image.size.height / 2));
}
-(void)moveShipRight:(id)sender {
shipView.center = CGPointMake(shipView.center.x + 2, 320 - (shipView.image.size.height / 2));
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [[event allTouches] anyObject];
CGPoint location = [touch locationInView:touch.view];
if (240 < location.x){rightTimer = [NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:@selector(moveShipRight:) userInfo:nil repeats:YES];}
if (240 > location.x){leftTimer = [NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:@selector(moveShipLeft:) userInfo:nil repeats:YES];}
[shipView setCenter:shipView.center];
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
if (rightTimer != nil) //Kill the timer that goes right upon touch removal
[rightTimer invalidate];
rightTimer = nil;
if (leftTimer != nil) //Kill the timer that goes left on touch removal
[leftTimer invalidate];
leftTimer = nil;
}
I'm trying to fully replicate the controls for the game "Radiant" if its any help. I will seriously love you forever if you help.
Just to clarify, I want this to happen:
right now I have multi touch enabled, so now it works like this:
First off, about not activating the leftTimer if the rightTouch is not released.
You could set up two Global Touches. So, if [touch locationInView:self.view] position blah is > 250 globalRightTouch = touch. Same for left.
THen when you hit the right side - you assign the touch to the global Right, and the ship starts to move right -- then you touch the left side simultaneously -- so another touches began thing starts - so you assign it to the globalLeftTouch. Then you can go something like: if(!globalRightTouch){start leftTimer) and vise versa.
In your touhcesEnded function - you are telling the leftTimer to kill if it is not null on touches end - so even if you have your left touch down still -- you are telling both timers to kill on any touch release. You just need to be a little more specific with your touches ended.
Then in your touches ended code you need to go something along the following:
UITouch *touch = [[event allTouches] anyObject];
if (rightTimer != nil && touch == globalRightTouch){ //Kill the timer that goes right upon touch removal
[rightTimer invalidate];
rightTimer = nil;
if(globalLeftTouch)
leftTimerStartFunction //I dont know what your variables are sorry
}
if (leftTimer != nil && touch == globalLeftTouch){ //Kill the timer that goes left on touch removal
[leftTimer invalidate];
leftTimer = nil;
if(globalRightTouch){
rightTimerStartFunction //I dont know what you variables are sorry
}
}
}
so essentially you want to say - If I let go of a touch - check which one it is and kill the timer for that touch if it is not null and if there is another touch currently on the screen - start the timer for that side.
Hope this helps,
Cheers,
Michael