-1

I want to create a game, but I need to detect if the screen was touched on the left or the right side of the screen (in landscape). How can I do that?

DrummerB
  • 39,814
  • 12
  • 105
  • 142
Robho
  • 31
  • 4
  • 4
    Where is the problem? What did you try so far? Where did you fail? You'll need to show some effort. No one is going to create your app for you. – DrummerB May 15 '13 at 14:45
  • Yes, I know guy... Don't be so aggressive... I ask this question because I'm looking for a new method to do this. I know how to detect a simple touch anywhere on the screen, and I know how to detect a touch over a object, but not how to virtually "separate" the screen in two... I could put a transparent image on the right side for exemple, an detect when the user touch it, but I don't want to do this, I want to discover new methods ;) – Robho May 15 '13 at 14:56
  • 1
    get the coord of the touch, if its x value is less than 160, its on the left, if not, its on the right – Fonix May 15 '13 at 15:02
  • If you know how to detect a touch anywhere on the screen, why don't you just check if the x coordinate is higher or smaller then the middle? – DrummerB May 15 '13 at 15:02
  • Robho drop it, until it's too late(I mean your app is appstored) :)) – Stas May 15 '13 at 15:06
  • I simply didn't think about that possibility! Thank you guys, and sorry for this "stupid" question... I'll think twice the next time ;) – Robho May 15 '13 at 15:06
  • I know my app is appstored, I do this just for fun ;) – Robho May 15 '13 at 15:08

1 Answers1

3

I suggest looking at a good tutorial for iOS before posting questions, e.g. iOS Stanford. However, in the mean time, something like this would to the trick:

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *myTouch = [touches anyObject];

    CGPoint aPoint = [myTouch locationInView:self.view];

    if(aPoint.x < 160) { //Left
        NSLog(@"Tapping on the left side of the screen is for communists!");
    else // Right
        NSLog(@"User tapped on the right side! Ohh Yeah!");
}
Max MacLeod
  • 26,115
  • 13
  • 104
  • 132
Groot
  • 13,943
  • 6
  • 61
  • 72
  • Just one more question, what is "view in the CGPoint? Xcode says me: "Proprety 'view' not found on object of type 'HelloWorldLayer *' – Robho May 15 '13 at 21:21