The iOS development USES the cocos2d method to add touch events

  • 2020-05-12 06:15:33
  • OfStack

The CCLayer class is used to receive touch input. However, you need to enable this feature before you can use it. You get the layer to receive touch events by setting isTouchEnabled to YES:

self.isTouchEnabled = YES;

This setting is best set in the init method. You can set it to NO or YES at any time.

Once the isTouchEnabled property is enabled, many methods associated with receiving touch input will begin to be called. These include when a new touch starts, when the finger moves on the touch screen, and after the user leaves the screen. It's rare for a touch event to be canceled, so you can either ignore it in most cases or use the ccTouchesEnded method to handle it.

Method called when a finger touches the screen for the first time:


-(void) ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent*)event

The method called when the finger moves on the screen:


-(void) ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent*)event

Method called when a finger is lifted from the screen:


-(void) ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent*)event

Method called when the touch event is cancelled:


-(void) ccTouchesCancelled:(NSSet *)touches withEvent:(UIEvent*)event

Cancellation events are rare, so in most cases they behave the same as when a touch ends.

Because the touch event is received by Cocoa TouchAPI, the location of the touch must be converted to the coordinates of OpenGL.

Here is a method to convert coordinates:


-(CGPoint) locationFromTouches:(NSSet *)touches 

    UITouch *touch = [touches anyObject]; 
    CGPoint touchLocation = [touch locationInView: [touch view]]; 
    return [[CCDirector sharedDirector] convertToGL:touchLocation]; 

By default, the layer receives the same events as the apple UIResponder class. cocos2d also supports targeted touch handling. The difference from normal processing is that it only receives one touch at a time, while UIResponder always receives one set of touches. Targeted touch event handling simply separates a group of touch events so that the required touch events can be provided according to the needs of the game. More importantly, targeted processing allows you to remove certain touch events from the queue. That way, if the touch occurs in a specific area of the screen, you'll be able to recognize it more easily. Once identified, you can mark the touch as processed, and all other layers do not need to check the area again.

Add the following methods to your layer to enable targeted touch event handling:


-(void) registerWithTouchDispatcher 

    [[CCTouchDispatcher sharedDispatcher] addTargetedDelegate:self priority:kCCMenuTouchPriority swallowsTouches:YES]; 
}

Note: if you leave the registerWithTouchDispatcher method blank, you will not receive any touch events! If you want to keep this method and use its default handling, you must call the [super registerWithTouchDispatcher] method.


You will now use a slightly different set of methods to replace the default touch input handling. They are almost exactly the same, except for one point: replace (UITouch *)touch with (NSSet *)touches as the first parameter of the method:


-(BOOL) ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event {} -(void) ccTouchMoved:(UITouch *)touch withEvent:(UIEvent *)event {} -(void) ccTouchEnded:(UITouch *)touch withEvent:(UIEvent *)event {} -(void) ccTouchCancelled:(UITouch *)touch withEvent:(UIEvent *)event {}

One important point here is that ccTouchBegan returns a Boolean value (BOOL). If you return YES, that means you don't want the current touch event to be passed to other touch event handlers. You actually "swallowed" the touching event.

Here's a complete example:
In your own layer, add


[self setIsTouchEnabled:YES];

The following methods are the methods of the cocos2d class library:


-(void) setIsTouchEnabled:(BOOL)enabled { if( isTouchEnabled_ != enabled ) { isTouchEnabled_ = enabled; if( isRunning_ ) { if( enabled ) [self registerWithTouchDispatcher]; else { CCDirector *director = [CCDirector sharedDirector]; [[director touchDispatcher] removeDelegate:self]; } } } } // This is the key -(void) registerWithTouchDispatcher { CCDirector *director = [CCDirector sharedDirector]; [[director touchDispatcher] addStandardDelegate:self priority:0]; }

Next, the implementation method:


-(void) ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent*)event
0


Related articles: