cc.Vec2
1: cc.Vec2 二維向量坐標(biāo), 表結(jié)構(gòu){x: 120, y: 120}; cc.v2(x, y) 創(chuàng)建一個(gè)二維向量 cc.p() 創(chuàng)建一個(gè)二維向量(這個(gè)2.0被廢棄了衡奥,用cc.v2替代)
2: cc.pSub: 向量相減 cc.pSub(v1, v2) 被廢棄,用v1.sub(v2)替代
3: cc.pAdd: 向量相加; cc. cc.pAdd(v1, v2) 被廢棄含友,用v1.add(v2)替代
4: cc.pLength: 向量長(zhǎng)度; cc.pLength(dir)被廢棄,用dir.mag(); 替代
cc.Size/cc.Rect
cc.Size即將被取消吏颖,用 cc.Vec2替代
1: cc.Size: 包含寬度和高度信息的對(duì)象 {width: 100, height: 100};
2: new cc.Size(w, h), cc.size(w, h)創(chuàng)建一個(gè) 大小對(duì)象;
3: cc.Rect: 矩形對(duì)象 new cc.Rect(x, y, w, h); cc.rect(x, y, w, h); {x, y, width, height}
4: contains(Point): 點(diǎn)是否在矩形內(nèi);
???? var r = new cc.Rect(0, 0, 100, 100); // new
???? var ret = r.contains(cc.v2(200, 200));
5: intersects : 兩個(gè)矩形是否相交;
???? var r2 = new cc.Rect(100, 100, 100, 100); // new
????r.intersects(r2);
creator坐標(biāo)系
1: 世界(屏幕)坐標(biāo)系;
????坐標(biāo)原點(diǎn)在左下角
2: 相對(duì)(節(jié)點(diǎn))坐標(biāo)系,兩種相對(duì)節(jié)點(diǎn)原點(diǎn)的方式
????(1) 左下角為原點(diǎn),
???? this.node.convertToWorldSpace(cc.v2(0, 0));
???? this.node.convertToNodeSpace(w_pos);
????(2) 錨點(diǎn)為原點(diǎn)(AR)
???? this.node.convertToWorldSpaceAR(cc.v2(0, 0));
????this.node.convertToNodeSpaceAR(w_pos);
????兩套API,帶AR后綴和不帶
3: 節(jié)點(diǎn)坐標(biāo)和屏幕坐標(biāo)的相互轉(zhuǎn)換; 我們到底使用哪個(gè)辜窑?通常情況下帶AR;
4: 獲取在父親節(jié)點(diǎn)坐標(biāo)系下(AR為原點(diǎn))的節(jié)點(diǎn)包圍盒;
????this.node.getBoundingBox();
5: 獲取在世界坐標(biāo)系下的節(jié)點(diǎn)包圍盒;
????this.node.getBoundingBoxToWorld();
6: 觸摸事件對(duì)象世界坐標(biāo)與節(jié)點(diǎn)坐標(biāo)的轉(zhuǎn)換;
cc.Class({
? ? extends: cc.Component,
? ? properties: {
? ? ? ? // foo: {
? ? ? ? //? ? default: null,? ? ? // The default value will be used only when the component attaching
? ? ? ? //? ? ? ? ? ? ? ? ? ? ? ? ? ?to a node for the first time
? ? ? ? //? ? url: cc.Texture2D,? // optional, default is typeof default
? ? ? ? //? ? serializable: true, // optional, default is true
? ? ? ? //? ? visible: true,? ? ? // optional, default is true
? ? ? ? //? ? displayName: 'Foo', // optional
? ? ? ? //? ? readonly: false,? ? // optional, default is false
? ? ? ? // },
? ? ? ? // ...
? ? },
? ? // use this for initialization
? ? onLoad: function () {
? ? ? ? /*var w_pos = new cc.Vec2(100, 100);
? ? ? ? console.log(w_pos);
? ? ? ? w_pos = cc.v2(200, 200);
? ? ? ? console.log(w_pos);
? ? ? ? w_pos = cc.p(300, 300);
? ? ? ? console.log(w_pos);
? ? ? ? var src = cc.p(0, 0);
? ? ? ? var dst = cc.p(100, 100);
? ? ? ? var dir = cc.pSub(dst, src);
? ? ? ? var len = cc.pLength(dir);
? ? ? ? console.log(len);
? ? ? ? var s = new cc.Size(100, 100);
? ? ? ? console.log(s);
? ? ? ? s = cc.size(200, 200);
? ? ? ? console.log(s);
? ? ? ? var r = new cc.Rect(0, 0, 100,100);
? ? ? ? console.log(r);
? ? ? ? r = cc.rect(0, 0, 200, 200);
? ? ? ? console.log(r);
? ? ? ? var ret = r.contains(cc.p(300, 300));
? ? ? ? console.log(ret);
? ? ? ? var rhs = cc.rect(100, 100, 100, 100);
? ? ? ? ret = r.intersects(rhs);
? ? ? ? console.log(ret);*/
? ? ? ? // 節(jié)點(diǎn)坐標(biāo)轉(zhuǎn)到屏幕坐標(biāo) cc.p(0, 0)
? ? ? ? var w_pos = this.node.convertToWorldSpace(cc.p(0, 0)); // 左下角為原點(diǎn)的? ?cc.p(430, 270)
? ? ? ? console.log(w_pos);
? ? ? ? w_pos = this.node.convertToWorldSpaceAR(cc.p(0, 0)); // 錨點(diǎn)為原點(diǎn) cc.p(480, 320)
? ? ? ? console.log(w_pos);
? ? ? ? // end?
? ? ? ? var w_pos = cc.p(480, 320);
? ? ? ? var node_pos = this.node.convertToNodeSpace(w_pos);
? ? ? ? console.log(node_pos); // cc.p(50, 50)
? ? ? ? node_pos = this.node.convertToNodeSpaceAR(w_pos);
? ? ? ? console.log(node_pos); // cc.p(0, 0)
? ? },
? ? start: function(){
? ? ? ? // 獲取節(jié)點(diǎn)的包圍盒, 相對(duì)于父親節(jié)點(diǎn)坐標(biāo)系下的包圍盒
? ? ? ? var box = this.node.getBoundingBox();
? ? ? ? console.log(box);
? ? ? ? // 世界坐標(biāo)系下的包圍盒
? ? ? ? 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);
? ? ? ? // 我要把當(dāng)前這個(gè)sub移動(dòng)到世界坐標(biāo)為 900, 600;
? ? ? ? //?
? ? ? ? // 把世界坐標(biāo)轉(zhuǎn)到相對(duì)于它的父親節(jié)點(diǎn)的坐標(biāo)
? ? ? ? var node_pos = this.node.parent.convertToNodeSpaceAR(cc.p(900, 600));
? ? ? ? this.node.setPosition(node_pos); // 相對(duì)于this.node.parent這個(gè)為參照物,AR為原點(diǎn)的坐標(biāo)系
? ? ? ? // end?
? ? ? ? // 獲取當(dāng)前節(jié)點(diǎn)的世界坐標(biāo);
? ? ? ? this.node.convertToWorldSpaceAR(cc.p(0, 0));
? ? }
? ? // called every frame, uncomment this function to activate update callback
? ? // update: function (dt) {
? ? // },
});