cocos2dx,Layer錨點與scale縮放


最近寫代碼需要用到縮放,而且是Layer的,但是發現怎么設置位置都是錯誤,於是決定研究下。

首先,基礎代碼,代碼上不錯特殊處理,沒有錨點設置和縮放

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));
    
    // 打印幾個點參照用
    LayerColor *lc1 = LayerColor::create(Color4B::RED, 10, 10);
    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);

 代碼運行結果:

可以看到,TestLayer被放置在了(200,200),錨點在(0,0)

下面對TestLayer進行縮放0.5

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

 運行結果:

(黃線是截圖時畫的)

可以看到TestLayer縮放了50%,但是左下角沒有在(200,200)

通過這個代碼可以看出,Layer中的child是以(0,0)為錨點,但是在scale時,確實以(0.5,0.5)進行縮放。

這個地方感覺比較坑,為什么沒有用統一的點呢?沒有寫過底層畫圖代碼,哪位同學知道,希望給我一個答案。

那這個地方可不可以統一呢?

看代碼:

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

 在TestLayer中增加上面代碼,運行結果:

 

可以看到縮放和child位置錨點一致了。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM