Touch Handling in Cocos2d 3.x (TRI)

Source: Internet
Author: User

Get Touch position

The most interesting part is the location of the touch. Next we'll use the touch location to add sprites to the screen position each time the player taps. To do this we need to modify the Touchbegan implementation, replacing the old code as follows:

-(void)Touchbegan:(Uitouch*) Touchwithevent:(uievent*) event{//We want to know the location of our touchinchThis sceneCgpointTouchlocation = [TouchLocationinnode: Self];//Create a' hero 'SpriteCcsprite*hero = [Ccsprite spritewithimagenamed:@"Hero.png"]; [ Self AddChild:Hero];//Place the sprite on the touch location hero.position = touchlocation;}

The implementation is fairly straightforward. The most interesting part is the location of the actual touch. The Locationinnode method converts the OpenGL view coordinates of the touch to the coordinates within node that are passed as a parameter to the method. (In this case, because we need to know where the screen touch actually is in mainscene).

Once we have reached the touch position, we simply create a new ccsprite node and add it to the Mainscene. where the "hero.png" Picture resource is part of the starter project that you downloaded from the beginning.

Once you run the project, you should be able to place Mgwu heroes full screen.

Now that you're familiar with the basics, we'll look further at the touch life cycle and how you can create complex touch processing in your game.

Touch Handling in Cocos2d 3.x (TRI)

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.