This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
manual:touch_events [2013/05/26 17:13] – daniel | manual:touch_events [2014/02/05 09:54] (current) – daniel | ||
---|---|---|---|
Line 1: | Line 1: | ||
+ | ===== Touch events ===== | ||
+ | One of the most revolutionary features of the iPhone is that it allows you to control it with your fingers. No physical buttons, not even a stylus is required -- and you can even use several fingers simultaneously. Sparrow makes it easy for you to react to touches of one or more fingers. | ||
+ | |||
+ | First of all: if you want to support multitouch, you need to enable it at Sparrow' | ||
+ | |||
+ | <code objc> | ||
+ | _viewController.multitouchEnabled = YES; | ||
+ | </ | ||
+ | |||
+ | When that's settled, you can listen to touch events on any object in your game by listening for events with the type '' | ||
+ | |||
+ | <code objc> | ||
+ | [self addEventListener: | ||
+ | </ | ||
+ | |||
+ | Again, we added the listener to ' | ||
+ | |||
+ | Remember our message box. When the user clicks on the text field, obviously anybody listening to touches on the text field must be notified. But the same is true for somebody listening for touch events on the message box --- the textfield is part of the message box, so the latter was touched as well. And if somebody listens to touch events on the stage, he will also be notified. The message box is part of the stage, after all. | ||
+ | |||
+ | Now, how to handle touch events? Have a look at this sample code. | ||
+ | |||
+ | <code objc> | ||
+ | - (void)onTouch: | ||
+ | { | ||
+ | SPTouch *touch = [[event touchesWithTarget: | ||
+ | if (touch) | ||
+ | { | ||
+ | SPPoint *touchPosition = [touch locationInSpace: | ||
+ | NSLog(@" | ||
+ | touchPosition.x, | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | That's the most basic case: Find out if somebody touched the screen, and log the coordinates (relative to the current display object). The method " | ||
+ | |||
+ | The target " | ||
+ | |||
+ | The touch phase is " | ||
+ | |||
+ | * '' | ||
+ | * '' | ||
+ | * '' | ||
+ | * '' | ||
+ | |||
+ | The method " | ||
+ | |||
+ | So much for single touches. Multiple touches are handled just the same. The only difference is that the set returned by " | ||
+ | |||
+ | <code objc> | ||
+ | NSArray *touches = [[event touchesWithTarget: | ||
+ | |||
+ | if (touches.count == 1) | ||
+ | { | ||
+ | // one finger touching | ||
+ | SPTouch *touch = [touches objectAtIndex: | ||
+ | SPPoint *currentPos = [touch locationInSpace: | ||
+ | SPPoint *previousPos = [touch previousLocationInSpace: | ||
+ | // ... | ||
+ | } | ||
+ | else if (touches.count >= 2) | ||
+ | { | ||
+ | // at least two fingers touching | ||
+ | SPTouch *touch1 = [touches objectAtIndex: | ||
+ | SPTouch *touch2 = [touches objectAtIndex: | ||
+ | // ... | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | Notice the method " | ||
+ | |||
+ | The demo application contains the class " | ||
+ | |||
+ | ------- | ||
+ | |||
+ | //Next Section: [[Animation]] |