The use of Converttonodespace and Converttoworldspace

Source: Internet
Author: User

Converttoworldspace this is to convert coordinates to the game world coordinates. Because an elf has a coordinate that comes through the getposition, but this coordinate is a coordinate relative to the parent, so the actual absolute coordinates are determined by the position of the parent. So the coordinates can be converted to the absolute coordinates of the game by GetParent ()->converttoworldspace.

After converting to world coordinates, you can convert to the same coordinate system as other sprites that are not in a coordinate system so that you can calculate the coordinates.

Calculate coordinates if you need to reset the wizard's coordinates then it will be converted back to relative coordinates (because setposition is also set relative coordinates) this time call GetParent ()->converttonodespace

Can be converted back to call SetPosition to set.

Converttonodespace: Converts the world coordinates into the local coordinate system of the current node.
  1. can be applied to determine whether a child node is clicked, it is necessary to convert the coordinates from the world coordinate system to the parent node's coordinate system.
  2. Most of the time, of course, Ccmenu is used, but sometimes ccmenu does not apply, such as long presses to trigger a response.
  3. void helloworld::myinit14 ()
  4. {
  5. Ccsize size = Ccdirector::shareddirector ()->getwinsize ();
  6. //Create parent node
  7. ccsprite* back = Ccsprite::create ("ui_miss_butt_bj2song.png");
  8. Back->setposition (CCP (size.width*0.5, size.height*0.5));
  9. This->addchild (back,1,123);
  10. //Create child nodes
  11. ccsprite* BUTTONSP = ccsprite::create ("team_ok_butt.png");
  12. Buttonsp->setposition (CCP (Back->getcontentsize () width*0.8, Back->getcontentsize (). height*0.5));
  13. Back->addchild (buttonsp,1,321);
  14. }
  15. BOOL Helloworld::cctouchbegan (Cctouch *ptouch, ccevent *pevent)
  16. {
  17. Beganpoint = Ptouch->getlocation ();
  18. return true;
  19. }
  20. void helloworld::cctouchended (Cctouch *ptouch, ccevent *pevent)
  21. {
  22. Ccpoint endPoint = Ptouch->getlocation ();
  23. ccsprite* back = (ccsprite*)This->getchildbytag (123);
  24. ccsprite* BUTTONSP = (ccsprite*) back->getchildbytag (321);
  25. The //converttonodespace transforms the world coordinates into the local coordinate system of the current node.
  26. Ccpoint nodebeganpoint = Back->converttonodespace (beganpoint);
  27. Ccpoint nodeendpoint = Back->converttonodespace (endPoint);
  28. //Determine if the child node is clicked
  29. if (Buttonsp->boundingbox (). Containspoint (Nodebeganpoint) && Buttonsp->boundingbox (). Containspoint (Nodeendpoint)) {
  30. Cclog ("was ordered to");
  31. }
  32. }

Converttoworldspace: Converts coordinates in the local coordinate system based on the current node to the world coordinate system.
  1. SP3 is the child node of SP2, SP2 is the child node of SP1.
  2. void helloworld::myinit13 ()
  3. {
  4. Ccsize size = Ccdirector::shareddirector ()->getwinsize ();
  5. ccsprite* SP1 = ccsprite::create ("icon.png");
  6. Sp1->setposition (CCP (size.width*0.5, size.height*0.5));
  7. This->addchild (SP1);
  8. ccsprite* SP2 = ccsprite::create ("icon.png");
  9. Sp2->setcolor (Ccyellow);
  10. Sp2->setposition (CCP (sp1->getcontentsize () width, sp1->getcontentsize (). height));
  11. Sp1->addchild (SP2);
  12. ccsprite* SP3 = ccsprite::create ("icon.png");
  13. Sp3->setcolor (Ccblue);
  14. Sp3->setposition (CCP (sp2->getcontentsize () width, sp2->getcontentsize (). height));
  15. Sp2->addchild (SP3);
  16. //converttoworldspace: Converts coordinates in the local coordinate system based on the current node to the world coordinate system.
  17. Ccpoint worldpoint = Sp2->converttoworldspace (Sp3->getposition ());
  18. Cclog ("WORLDX = =%f, Worldy = =%f", worldpoint.x,worldpoint.y);
  19. //SP4 Just covers the SP3, explaining that the coordinates are converted to the world coordinate system correctly.
  20. ccsprite* SP4 = ccsprite::create ("icon.png");
  21. Sp4->setcolor (Ccgreen);
  22. Sp4->setposition (Worldpoint);
  23. This->addchild (SP4);
  24. }

Summary: for example

R0 as root node

The parent node of the R1 is R0

The parent node of the R2 is R1

To get the world coordinates of R2 r2->getparent ()->converttoworldspace (R2->getpostion ());

[Self-understanding: Convert R2->getparent () to world coordinates and then convert r2->getpostion () to coordinates under current world coordinates]

To get the coordinates of the R2 relative parent node r2->getparent ()->converttonodespace (R2->getpostion ());

[Oneself Understand: convert r2->getpostion () coordinates to relative r2->getparent () relative coordinates]

The use of Converttonodespace and Converttoworldspace

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.