在cocos2d-html5中,Scene和Layer的默認錨點是(left,bottom),而其它節點(Node)的默認錨點是(center,center)。無論是Layer還是其它節點,其座標原點都是父節點的左下角,即(left,bottom)。只是有一個例外,cc.MenuItem是以cc.Menu的錨點爲座標原點。
首先測試Layer:
- // 獲取尺寸
- this.winSize = cc.Director.getInstance().getWinSize();
- // 灰色層
- this.layer1 = cc.LayerColor.create(cc.c4(200, 200, 200, 255), this.winSize.width, this.winSize.height);
- this.addChild(this.layer1, 0);
- // 綠色層
- this.layer2 = cc.LayerColor.create(cc.c4(0, 255, 0, 255), 200, 200);
- this.addChild(this.layer2, 1);
運行結果:
改變layer2的位置:
- this.layer2.setPosition(200, 200);
運行結果:
默認情況下,Scene和Layer是不能設置錨點的,要想設置錨點可以使用方法ignoreAnchorPointForPosition(newValue)。
- 參數newValue
- false:錨點可設置
- true:錨點被強制設置爲(0, 0),無法改變
測試Sprite:
- // 添加精靈
- this.sprite = cc.Sprite.create("res/bear.png");
- this.addChild(this.sprite);
運行結果:
改變sprite的位置:
- this.sprite.setPosition(cc.p(100, 100));
運行結果: