Hi,
So, I am trying to recreate one of those "virtual joysticks" on some of those iPhone games. And basically, heres the problem I can make sure that the user is touching the circle (that is the "virtual joystick") and I can also move the thing around (or at least was able to move it around because since then I have messed around with it). But, I can not give it a range.
What the heck do I mean by a range?
Let me, hopefully, explain correctly what I mean a range is. So when messing with an actual joystick on something like a Playstation controller, you can obviously not drag it everywhere like I could with that circle i was describing. So, it has a range which is anywhere within an imaginary circle that you cannot see.
I have tried the following:
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
CGPoint currentPosition = [touch locationInView:self.view];
if (g == true) {
if (joystick.center.x > 330 && currentPosition.x > 330 || joystick.center.x < 130 && currentPosition.x < 130) {
joystick.center = CGPointMake(joystick.center.x, currentPosition.y);
j = true;
}
if (joystick.center.y > 508 && currentPosition.y > 508 || joystick.center.y < 308 && currentPosition.y < 308) {
joystick.center = CGPointMake(currentPosition.x, joystick.center.y);
j = true;
}
if (j == false) {
joystick.center = CGPointMake(currentPosition.x, currentPosition.y);
}
}
}
One note before I begin explain what my logic for my attempt was, the joystick is located on the screen at (230, 408) and I didn't do anything to the coordinate system (so its based off the top left hand corner). Now, let me explain what this is supposedly doing:
- Well first I get the coordinates of the touch with the first two lines of code in the method.
- Then
if (g == true)
actually has to do if the touch is on the circle or not and is set bytouchesBegan
. - Then the second if is checking if the joystick's x location is greater than 330 or less then 130 which will give the radius of the imaginary range that is a circle. Also, note the additional
&& currentPosition.x > 330
andcurrentPosition.x < 130
this is for if the person is moving the virtual joystick back toward the center since you don't want the user from no being able to do so. - The
else if
does the same as 3 except for the y direction. - For the final
if
uses a bool j to determine if the other two`if statements have been executed.
This plan doesn't work obviously, or else I wouldn't be asking for your help, so do you perchance know what I am doing wrong?