Cocos2d-html5中的座標系統

在cocos2d-html5中,Scene和Layer的默認錨點是(left,bottom),而其它節點(Node)的默認錨點是(center,center)。無論是Layer還是其它節點,其座標原點都是父節點的左下角,即(left,bottom)。只是有一個例外,cc.MenuItem是以cc.Menu的錨點爲座標原點。

首先測試Layer:

  1. // 獲取尺寸
  2. this.winSize = cc.Director.getInstance().getWinSize();
  3. // 灰色層
  4. this.layer1 = cc.LayerColor.create(cc.c4(200, 200, 200, 255), this.winSize.width, this.winSize.height);
  5. this.addChild(this.layer1, 0);
  6. // 綠色層
  7. this.layer2 = cc.LayerColor.create(cc.c4(0, 255, 0, 255), 200, 200);
  8. this.addChild(this.layer2, 1);

運行結果:

Cocos2d-html5中的座標系統 - 鬼眼邪神 - 鬼眼邪神

改變layer2的位置:

  1. this.layer2.setPosition(200, 200);

運行結果:

Cocos2d-html5中的座標系統 - 鬼眼邪神 - 鬼眼邪神

默認情況下,Scene和Layer是不能設置錨點的,要想設置錨點可以使用方法ignoreAnchorPointForPosition(newValue)。

參數newValue
false:錨點可設置
true:錨點被強制設置爲(0, 0),無法改變

測試Sprite:

  1. // 添加精靈
  2. this.sprite = cc.Sprite.create("res/bear.png");
  3. this.addChild(this.sprite);

運行結果:

Cocos2d-html5中的座標系統 - 鬼眼邪神 - 鬼眼邪神
 

改變sprite的位置:

  1. this.sprite.setPosition(cc.p(100, 100));

運行結果:

Cocos2d-html5中的座標系統 - 鬼眼邪神 - 鬼眼邪神
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章