Game Development Reference
In-Depth Information
▪ This is called whenever the finger moves on the screen:
-(void)
ccTouchesMoved:(NSSet
*)touches
withEvent:(UIEvent *)event
▪ This is called when a finger is lifted off the screen:
-(void)
ccTouchesEnded:(NSSet
*)touches
withEvent:(UIEvent *)event
▪ This is called to cancel a touch:
-(void)
ccTouchesCancelled:(NSSet
*)touches
withEvent:(UIEvent *)event
Cancel events are rare and in most cases should behave just like touches ended.
In many cases, you want to know where a touch occurred. Because the touch events are
received by the Cocoa Touch API, the location must be converted to OpenGL coordin-
ates. The following method does this for you:
-(CGPoint) locationFromTouches:(NSSet *)touches
{
UITouch *touch = touches.anyObject;
CGPoint touchLocation = [touch locationInView:touch.view];
return [[CCDirector sharedDirector] convertToGL:touchLocation];
}
This method works only with a single touch because it uses [touches anyOb-
ject] . To keep track of multitouch locations, you have to keep track of each touch in-
dividually.
By default, the layer receives the same events as Apple's UIResponder class. Co-
cos2d also supports targeted touch handlers. The difference is that targeted touches re-
ceive only one touch at a time, in contrast to the UIResponder touch events that al-
ways receive a set of touches. The targeted touch handler simply splits those touches
into separate events that, depending on your game's needs, may be easier to work with.
More importantly, the targeted touch handler lets you remove certain touches from the
event queue, specifying that you've handled this touch and don't want it to be forwar-
ded to other layers. This makes it easy to sort out if touches are in a specific area of the
screen; if they are, you mark the touch as claimed, and all the remaining layers don't
need to do this area check again.
Search Nedrilad ::




Custom Search