Ways to add touch events using cocos2d in IOS development _ios

Source: Internet
Author: User

The Cclayer class is used to receive touch input. However, you must first enable this feature before you can use it. You let the layer receive the touch event by setting istouchenabled to Yes:

Copy Code code as follows:
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 of the methods associated with receiving touch input will begin to be invoked. These events include: When the new touch starts, when the finger moves on the touchscreen, and after the user's finger leaves the screen. It is rare for a touch event to be canceled, so you can ignore it in most cases, or use the Cctouchesended method to handle it.

The method that is invoked when the finger first touches the screen:

Copy Code code as follows:

-(void) Cctouchesbegan: (Nsset *) touches withevent: (uievent*) event

The method that is invoked when the finger moves on the screen:

Copy Code code as follows:

-(void) cctouchesmoved: (Nsset *) touches withevent: (uievent*) event

The method to invoke when the finger is lifted from the screen:

Copy Code code as follows:

-(void) cctouchesended: (Nsset *) touches withevent: (uievent*) event

The method to invoke when the touch event is canceled:

Copy Code code as follows:

-(void) cctouchescancelled: (Nsset *) touches withevent: (uievent*) event

Canceling an event rarely occurs, so in most cases it behaves the same as when the touch ends.

Because the touch event is received by Cocoa Touchapi, the touch position must be converted to OpenGL coordinates.

Here's a way to convert the coordinates:

Copy Code code as follows:

-(Cgpoint) Locationfromtouches: (Nsset *) touches
{
Uitouch *touch = [touches anyobject];
Cgpoint touchlocation = [Touch locationinview: [Touch view]];
return [[Ccdirector Shareddirector] converttogl:touchlocation];
}

By default, the events received by the layer are the same as those received by the Apple Uiresponder class. Cocos2d also supports targeted touch processing. The difference between ordinary processing is that it receives only one touch at a time, and Uiresponder always receives a set of touches. Targeted touch event processing simply separates a set 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. In this case, if the touch occurs in a specified area of the screen, you will be more likely to identify it, and when you recognize it you can mark the touch as being processed, and all other layers will no longer need to check the area again.

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

Copy Code code as follows:

-(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 method, you must call [super Registerwithtouchdispatcher] This method.


Now you're going to use a slightly different approach instead of the default touch input approach. They are almost exactly the same, except for one thing: use (Uitouch *) touch instead (Nsset *) touches as the first parameter of the method:

Copy Code code as follows:

-(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 {}

The important point here is that Cctouchbegan returns a Boolean value (BOOL). If you return yes, that means you do not want the current touch event to be transmitted to other touch event handlers. You are actually "swallowing" this touch event.

Let's look at a complete example:
Inside their own layer, add

Copy Code code as follows:

[Self setistouchenabled:yes];

The following methods are methods for Cocos2d class libraries:

Copy Code code as follows:

-(void) setistouchenabled: (BOOL) enabled

{

if (istouchenabled_!= enabled) {

Istouchenabled_ = enabled;

if (isrunning_) {

if (enabled)

[Self registerwithtouchdispatcher];

else {

Ccdirector *director = [Ccdirector shareddirector];

[[director Touchdispatcher] removedelegate:self];

}

}

}

}

That's the key.

-(void) Registerwithtouchdispatcher

{

Ccdirector *director = [Ccdirector shareddirector];

[[director Touchdispatcher] addstandarddelegate:self priority:0];

}

The next step is to implement the method:

Copy Code code as follows:

-(void) cctouchesmoved: (Nsset *) touches withevent: (Uievent *) event

{

For (Uitouch *touch in touches) {

Cgpoint location = [Touch Locationinview: [Touch view]];

//

location = [[Ccdirector shareddirector] converttogl:location];

Cgpoint Touchpos = location;

//

NSLog (@ "point==%@", Nsstringfromcgpoint (Touchpos));

// }

uitouch* touch = [touches anyobject];

Cgpoint location = [Touch Locationinview: [Touch view]];

location = [[Ccdirector shareddirector] converttogl:location];

self.position = location;

NSLog (@ "point==%@", Nsstringfromcgpoint (location));

}

-(void) cctouchended: (Uitouch *) Touch withevent: (uievent *) Event {

self.ismoving = FALSE;

}

-(BOOL) Cctouchbegan: (Uitouch *) Touch withevent: (uievent *) Event {

can set up a check here if you don't interested in handling every touch.

For example if your player are already moving, return no ...

BOOL Handletouch = FALSE;

if (!self.ismoving) {

Handletouch = TRUE;

self.ismoving = TRUE;

}

return Handletouch;

}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.