1: 世界(屏幕)坐標系;坐標原點在左下角
2: 相對(節點)坐標系,兩種相對節點原點的方式
(1) 左下角為原點,
this.node.convertToWorldSpace(cc.v2(0, 0));
this.node.convertToNodeSpace(w_pos);
(2) 錨點為原點(AR)
this.node.convertToWorldSpaceAR(cc.v2(0, 0));
this.node.convertToNodeSpaceAR(w_pos);
兩套API,帶AR后綴和不帶
3: 節點坐標和屏幕坐標的相互轉換;通常情況下帶AR;
4: 獲取在父親節點坐標系下(AR為原點)的節點包圍盒;
this.node.getBoundingBox();
5: 獲取在世界坐標系下的節點包圍盒;
this.node.getBoundingBoxToWorld();
例子:
// 節點坐標轉到屏幕坐標 cc.p(0, 0),以節點左下角為原點
var w_pos = this.node.convertToWorldSpace(cc.p(0, 0)); // 左下角為原點的 cc.p(430, 270)
console.log(w_pos);
//加上AR后為以錨點為原點
w_pos = this.node.convertToWorldSpaceAR(cc.p(0, 0)); // 錨點為原點 cc.p(480, 320)
console.log(w_pos);
//-----------------將世界坐標轉換為相對節點坐標
var w_pos = cc.p(480, 320);
//以w_pos節點左下角為原點,將該坐標(Wpos)轉換為相對于w_pos節點左下角的相對節點坐標
var node_pos = this.node.convertToNodeSpace(w_pos);
console.log(node_pos); // cc.p(50, 50)
//以w_pos節點錨點為原點,將該坐標(Wpos)轉換為相對于w_pos節點錨點的相對節點坐標
node_pos = this.node.convertToNodeSpaceAR(w_pos);
console.log(node_pos); // cc.p(0, 0)
// 獲取節點的包圍盒, 相對于父親節點坐標系下的包圍盒
var box = this.node.getBoundingBox();
console.log(box);
// 世界坐標系下的包圍盒
var w_box = this.node.getBoundingBoxToWorld();
console.log(w_box);
this.node.on(cc.Node.EventType.TOUCH_START, function(t) {
var w_pos = t.getLocation();
var pos = this.node.convertToNodeSpaceAR(w_pos);
console.log(pos);
pos = this.node.convertTouchToNodeSpaceAR(t);
console.log("====", pos);
}, this);
// 把當前這個sub移動到世界坐標為 900, 600;
//
// 把世界坐標轉到相對于它的父親節點的坐標
var node_pos = this.node.parent.convertToNodeSpaceAR(cc.p(900, 600));
this.node.setPosition(node_pos); // 相對于this.node.parent這個為參照物,AR為原點的坐標系
// end
// 獲取當前節點的世界坐標;
this.node.convertToWorldSpaceAR(cc.p(0, 0));
//觸摸事件所返回的坐標對象
var pos = this.node.on(cc.Node.EventType.TOUCH_START,function(e){
//方法1
var Wpos = e.getLocation();
var pos = this.node.convertToNodeSpaceAR(Wpos);
cc.log(Wpos+"<----->"+pos);
//方法2
pos = this.node.convertTouchToNodeSpaceAR(e);
cc.log("**********"+pos);
},this);
//.把該點轉成世界坐標(只能老爸轉)
var pos1 = this.cocos2.parent.convertToWorldSpaceAR(this.cocos2.getPosition());
console.log(pos1)
//把該點(世界坐標)轉成節點坐標
var pos2 = this.cocos1.convertToNodeSpaceAR(pos1);
console.log(pos2)