Cocos2d-x Learning Notes Ccscene, Cclayer, ccsprite default coordinates and default anchor points experiment _c language

Source: Internet
Author: User
Tags addchild

Conclusion: Practice proves that the default coordinates of these three things are 0,0 the default anchor points are 0.5, 0.5.

BOOL Helloworld::init ()
  bool BRet = false;
    cc_break_if (! Cclayer::init ());

		Ccsprite * Sprite = ccsprite::create ("Image1.png");

		This->addchild (sprite);

		Obtains the Ccsprite the default coordinates, Ccsprite coordinates the time to use own anchor point position occupies on the coordinate.
		Cclog ("Ccsprite.x=%f,ccsprite.y=%f", Sprite->getposition (). X,sprite->getposition (). y);

		Gets the default anchor point for Ccsprite
		cclog ("Sprite1anchor.x=%f,sprite1anchor.y=%f", Sprite->getanchorpoint () .x,sprite-> Getanchorpoint (). y);

		Gets the default coordinates of the Cclayer
		cclog ("Cclayer.x=%f,cclayer.y=%f", This->getposition (). X,this->getposition (). y);

		Obtain the default anchor point
		= This->getanchorpoint ()
		of Cclayer Ccpoint. Cclog ("layeranchor.x=%f,layeranchor.y=%f", point.x,point.y);

    BRet = true;
  } while (0);

  return bRet;
ccscene* Helloworld::scene ()
  ccscene * scene = NULL;
    //' scene ' was an Autorelease object
    scene = Ccscene::create ();
    CC_BREAK_IF (! scene);

    ' Layer ' is a Autorelease object
    HelloWorld *layer = Helloworld::create ();
    CC_BREAK_IF (! layer);

    Add layer as a child to scene
    Scene->addchild (layer);
		Layer->show ();
  } while (0);

  Return the scene return
void Helloworld::show ()
	//obtain ccscene default coordinates
	ccscene * scene = (Ccscene *) this->getparent ();
	Cclog ("Ccscene.x=%f,ccscene.y=%f", Scene->getposition (). X,scene->getposition (). y);

	Obtain the default anchor point
	= Scene->getanchorpoint ()
	of Ccscene Ccpoint. Cclog ("sceneanchor.x=%f,sceneanchor.y=%f", Point.x,point.y);

One thing that is very noteworthy here is the function ignoreachorpointforposition (), which means whether to ignore anchor points when setting the coordinates of a node (including Ccscene,cclayer,ccsprite). We all know that when we set the coordinates, we use the anchor point to occupy the coordinates, and if we ignore the anchor point, it is the position that occupies the coordinates in the lower left corner, or the anchor point is the point in the lower left corner. Query the official API to get the conclusion: this is a internal method, only used by Cclayer and Ccscene. Don ' t call it outside framework. The default value is False, while in Cclayer and Ccscene are true. The default value for this function is false, but for Ccscene and cclayer its value is true. If that's the case, we're setting the coordinates of Ccscene and Cclayer in the lower-left corner of the anchor, not (0.5,0.5). But for Ccsprite, the default value for this function is false, which is that the anchor is the default anchor point (0.5,0.5).

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: 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.