Cocos2DForIPhoneApplication Development Learning is what we will introduce in this article.Cocos2DLet's look at the content.
Except that the Layer can accept touch events, a new feature is added after Cocos2D 0.8 so that all objects can accept touch events. I don't know how to use this method. Here is a brief introduction.
First, add the event receiver:
- [[TouchDispatcher sharedDispatcher] addTargetedDelegate:self priority:1 swallowsTouches:NO];
// Self is the receiver. The smaller the priority parameter is from 0, the higher the priority is, the more events are received first. The last parameter indicates whether the event is blocked.
Then three methods are implemented:
- # Pragma mark TouchDispatcherDelegate
- -(BOOL) ccTouchBegan :( UITouch *) touch withEvent :( UIEvent *) event {
- // Your code
- Return YES; // if NO is returned here, this touch will be ignored.
- }
- -(Void) ccTouchMoved :( UITouch *) touch withEvent :( UIEvent *) event
- {
- // Your code
- }
- -(Void) ccTouchEnded :( UITouch *) touch withEvent :( UIEvent *) event {
- // Your code
- }
In this way, coco2d can be processed just like events in UIView.
Edit: Don't forget to delete the listener, or else ......
- [[TouchDispatcher sharedDispatcher] removeDelegate:self];
Summary:Cocos2DForIPhoneI hope this article will be helpful to you!