Cocos2dx, Layer Anchor and scale, cocos2dxscale

Source: Internet
Author: User

Cocos2dx, Layer Anchor and scale, cocos2dxscale

Recently, code writing requires scaling and Layer. However, we found that setting the location is incorrect, so we decided to study it.

First of all, the basic code is well handled, and there is no set or scale of the anchor.

Class TestLayer: public Layer {public: bool init (); CREATE_FUNC (TestLayer) ;}; bool TestLayer: init () {if (! Layer: init () {return false;} Sprite * dd = Sprite: create ("item_gift1.png"); this-> addChild (dd ); this-> setContentSize (Size (200,200); // you can use LayerColor * lc1 = LayerColor: create (Color4B: RED, 10, 10) to print several points ); lc1-> setPosition (50, 0); this-> addChild (lc1, 9001); LayerColor * lc4 = LayerColor: create (Color4B: RED, 10, 10 ); lc4-> setPosition (100, 0); this-> addChild (lc4, 9001); LayerColor * lc2 = LayerColor: create (Color4B: RED, 10, 10 ); lc2-> setPosition (150, 0); this-> addChild (lc2, 9001); LayerColor * lc6 = LayerColor: create (Color4B: RED, 10, 10 ); lc6-> setPosition (200, 0); this-> addChild (lc6, 9001); LayerColor * lc3 = LayerColor: create (Color4B: RED, 10, 10 ); lc3-> setPosition (0, 50); this-> addChild (lc3, 9001); LayerColor * lc5 = LayerColor: create (Color4B: RED, 10, 10 ); lc5-> setPosition (0,100); this-> addChild (lc5, 9001); LayerColor * lc7 = LayerColor: create (Color4B: RED, 10, 10 ); lc7-> setPosition (0,150); this-> addChild (lc7, 9001); LayerColor * lc8 = LayerColor: create (Color4B: RED, 10, 10 ); lc8-> setPosition (0,200); this-> addChild (lc8, 9001); return true ;}

 

    TestLayer *t = TestLayer::create();    t->setPosition(200, 200);    this->addChild(t, 9000);

Code running result:

We can see that TestLayer is placed in (200,200), and The anchpoint is in)

Scale down TestLayer by 0.5

TestLayer *t = TestLayer::create();t->setPosition(200, 200);t->setScale(0.5f);

Running result:

(The yellow line is time-drawn)

We can see that TestLayer is scaled to 50%, but not in the lower left corner (200,200)

From this code, we can see that the child in the Layer is the (0, 0) as the anchor, but it is indeed scaled to (0.5, 0.5) at scale.

This place is quite difficult. Why not use a unified point? I have never written the bottom-layer drawing code. Anyone knows, and I hope to give me an answer.

Can this place be unified?

Check the Code:

this->_ignoreAnchorPointForPosition = false;this->setAnchorPoint(Point(0, 0));

Add the above Code to TestLayer and run the result:

 

You can see that the scaling is consistent with the child position of the anchor.

 

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.