Cocos2dx 3.0 transition article (14) use touch to drag the female Master to defeat John!

Source: Internet
Author: User

First of all, I want to explain: This is a story about the competition between a female Master and John three for a male master! The story scene is a huge scale and closely related to the story,
Aison apartment in love apartment...
Episode on the plot Introduction: http://blog.csdn.net/start530/article/details/20153367

-------------------------------------
Boy and girl have had a sweet relationship for two years, but as boy is an outstanding man of both arts and sciences, John has to jump out! John designed various conspiracy
To break up boys and girls. Finally, they had a big fight. During the Cold War, John joined the company with boy! After discovering this situation, the female realized that everything was a zombie, but she couldn't pull her face to apologize to the boy. Our story starts from here.
Main story: drag the girl to the red box next to the male master.
Line-by-line plot: as soon as the girl enters the red box, she will be immediately attracted by the temperament of the boy arts and sciences, and fly to him.
Line-by-line plot: According to the plot arrangement, John must leave immediately when the female comes back!

-------------------------------------------------------
Implementation process:

1. Add a touch listening event:

<喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> Listener = "brush: java;"> auto listener = EventListenerTouchOneByOne: create (); // create a touch listener (single touch) listener-> onTouchBegan = CC_CALLBACK_2 (HelloWorld :: callback, this); // specifies the touch callback function listener-> onTouchEnded = CC_CALLBACK_2 (HelloWorld: onTouchEnded, this); listener-> onTouchMoved = CC_CALLBACK_2 (HelloWorld: onTouchMoved, this); _ eventDispatcher-> addEventListenerWithSceneGraphPriority (listener, this); // put the listener into the event Delegate
2. Create boy, girl, girl_3

Boy = Sprite: create ("boy.png"); // create a male master boy-> setPosition (Point (400,370); this-> addChild (boy, 1 ); girl_1 = Sprite: create ("girl_1.png"); // create a female Master girl_1-> setPosition (Point (250,250); this-> addChild (girl_1, 1 ); girl_3 = Sprite: create ("girl_3.png"); // create a small three girl_3-> setPosition (Point (450,370); this-> addChild (girl_3, 1 );

3. Receive listener, touchBegan

Bool HelloWorld: onTouchBegan (Touch * touch, Event * event) {return true; // return true to receive Touch events}

4. Move the female Master

Void HelloWorld: onTouchMoved (Touch * touch, Event * event) {auto beginPos = touch-> getLocationInView (); // obtain the Touch position beginPos = Director: getInstance () -> convertToGL (beginPos); // coordinates auto girlPos = girl_1-> getPosition (); // obtains the location auto girlSize = girl_1-> getContentSize () of the female Master (); // obtain the circumference (size) of the female owner. // customizes the coordinates and dimensions of the female owner .. box auto rect = Rect (girlPos. x-girlSize. width/2, girlPos. y-girlSize. height/2, girlSize. width + 10, girlSize. he Ight + 10); if (rect. containsPoint (beginPos) // determines whether the contact is on the female Master! {Auto endPos = touch-> getpreviuslocationinview (); // obtain the endPos from the previous position of the touch = Director: sharedDirector ()-> convertToGL (endPos ); // convert the Coordinate auto offset = Point (beginPos-endPos); // obtain the offset. 2.14 is the ccpSub, after 3.0, you can use the-number to auto nextPos = Point (girlPos + offset). // obtain the next plan of the female Master. 2.14 uses ccpAdd, girl_1-> setPosition (nextPos );}}

5. Determine the last position of the female owner's movement. If it is in the red box, the owner will be attracted and John will leave. If not, the female remains unchanged.

Void HelloWorld: onTouchEnded (Touch * touch, Event * event) {auto lastPos = touch-> getLocationInView (); lastPos = Director: getInstance ()-> convertToGL (lastPos ); auto rect = Rect (400,300,100,150); // create a selected area. If the female owner is dragged to this box, it can be instantly moved in. Otherwise, John comes in with MoveTo * moveTo_1; // female action MoveTo * moveto3; // if (rect. containsPoint (lastPos) {moveTo_1 = MoveTo: create (0.1f, Point (450,370); moveto3 = MoveTo: create (0.1f, Point (250,250 )); } Else {moveTo_1 = MoveTo: create (0.1f, Point (250,250); moveto3 = MoveTo: create (0.1f, Point (450,370 ));} girl_1-> runAction (moveTo_1); // compete for male masters ~~ Girl_3-> runAction (moveto3 );}

--------------------------------------------------------
This story tells us that couples must cherish each other's feelings. We are not playing the Three Kingdoms, but splitting is risky. A negligence occurs naturally.


This blog is purely entertaining;
Reprinted please indicate from star te 530: http://blog.csdn.net/start530/article/details/20319009



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.