Cocos2dx 3.0 transition article (25) greedy snake (touch Version)

Source: Internet
Author: User

The gravity sensor control version of the greedy snake written in the previous article. Let's talk about the touch control version. Well, I don't know if I have obtained the book from instructor Shen. If not, I don't want to take a watermelon knife to cut down the occasional E-past events!

Most of the code for the touch version and gravity version is the same. The difference is that you need to replace the Enable gravity sensor with creating a touch listener.
. The process is as follows:

1. Create a touch listening event in init () of GameLayer.

// Initialize the game layer bool GameLayer: init () {if (! Layer: init () {return false;} auto labhelp = LabelTTF: create ("this is game", "", 15 ); labhelp-> setPosition (Point (0,340); this-> addChild (labhelp); auto labback = LabelTTF: create ("MainMenu", "", 15 ); auto miback = MenuItemLabel: create (labback, [] (Ref * sender) {Director: getInstance ()-> replaceScene (HelloWorld: createScene ());}); miback-> setPosition (Point (360,200); // receives the touch event auto listener = EventListenerTouchOneByOne: create (); listener-> onTouchBegan = CC_CALLBACK_2 (HelloWorld: onTouchBegan, this); // specifies the touch callback function listener-> onTouchMoved = CC_CALLBACK_2 (HelloWorld: onTouchMoved, this); listener-> onTouchEnded = CC_CALLBACK_2 (HelloWorld: onTouchEnded, this ); _ eventDispatcher-> addEventListenerWithSceneGraphPriority (listener, this); // put the listener into the event Delegate. // initialize the coordinates of the snake header and food. sHead = new snail kenode (); sHead-> row = rand () % 10; sHead-> col = rand () % 10; // initialize the coordinates of the food sFood = new snail kenode (); sFood-> row = rand () % 10; sFood-> col = rand () % 10; // execute the scheduled task this-> schedule (schedule_selector (GameLayer: logic01 ), 0.5); return true ;}

2. Check the position of the touch in touchBegan,
Bool GameLayer: onTouchBegan (Touch * touch, Event * event) {auto p = touch-> getLocation (); // gets the current contact int nowrow; // The row that the current user touches: nowrow = (int) p. y)/32; int nowcol; // column nowcol = (int) p. x)/32; // If the Y coordinate of the contact is greater than the Y coordinate of the contact and the X coordinate of the Snake Head, It is up and down, otherwise, move left and right if (abs (nowrow-sHead-> row)> abs (nowcol-sHead-> col) // move up and down {if (nowrow> sHead-> row) {sHead-> dir = DIR_DEF: UP;} else {sHead-> dir = DIR_DEF: DOWN ;}} else // move left and RIGHT {if (nowcol> sHead-> col) {sHead-> dir = DIR_DEF: RIGHT;} else {sHead-> dir = DIR_DEF :: LEFT ;}} CCLOG ("rand % d", rand (); CCLOG ("you touchbegan % f, % f", p. x, p. y); return true ;}

3. The third point is just word count. I feel sorry for writing such a thing. Therefore, I wish everyone good health and good luck, good luck in the future, family joy, happiness and well-being, good luck and good luck. As soon as possible, get rid of your right hand... Ah, get rid of being single!

Respect Original, reprinted please indicate Source: http://blog.csdn.net/start530/article/details/23784985

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.