Touch Handling in Cocos2D 3.x( 2)

Source: Internet
Author: User

Touch Handling in Cocos2D 3.x( 2)

Accept touch

In Cocos2d 3.0, each CCNode and each CCNode subclass can receive and touch. you only need to enable one option. let's complete it in the Custom initiator. replace MainScene. code of the init method in m:

-(Id) init {if (self = [super init]) {// activate touches on this scene self. userInteractionEnabled = TRUE;} return self ;}

Now Cocos2d will know that we want to process the touch in this scenario

Processing touch
Cocos2d will notify us of four different touch events:

When the touch starts, when the touch moves, when the touch ends, when the touch is canceled

These different methods allow you to track the touch on the screen. For our first example, we only need to be notified of the Start event of the touch.

Add the following code to MainScene. m:

-(Void) touchBegan :( UITouch *) touch withEvent :( UIEvent *) event {CCLOG (@ "Received a touch ");}

When you enable a Node interaction, all implemented touch processing methods will be called. now we have implemented the touchBegan method, which will be called at any time when a touch starts. when a touch occurs, we use CCLOG to print debugging information to the console.

Run the app now. Every time you touch the screen, a "Received a touch" message will be displayed on the console. now you know how to receive the touch of any node in your game-this will be very powerful.

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.