1. cc.Vec2
cc.Vec2 二维向量坐标, 表结构{x: 120, y: 120}; cc.v2(x, y) 创建一个二维向量 cc.p() 创建一个二外向量
cc.pSub: 向量相减
cc.pAdd: 向量相加;
cc.pLength: 向量长度;
2. cc.Size/cc.Rect
cc.Size: 包含宽度和高度信息的对象 {width: 100, height: 100};
new cc.Size(w, h), cc.size(w, h)创建一个 大小对象;
cc.Rect: 矩形对象 new cc.Rect(x, y, w, h); cc.rect(x, y, w, h); {x, y, width, height}
contains(Point): 点是否在矩形内;
intersects : 两个矩形是否相交;
3. creator坐标系
世界(屏幕)坐标系;
相对(节点)坐标系,两种相对节点原点的方式(1) 左下角为原点, (2) 锚点为原点(AR)
节点坐标和屏幕坐标的相互转换; 我们到底使用哪个?通常情况下带AR;
获取在父亲节点坐标系下(AR为原点)的节点包围盒;
获取在世界坐标系下的节点包围盒;
触摸事件对象世界坐标与节点坐标的转换;
代码 : coord_space.js
// Learn cc.Class:
// - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/class.html
// - [English] http://docs.cocos2d-x.org/creator/manual/en/scripting/class.html
// Learn Attribute:
// - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
// - [English] http://docs.cocos2d-x.org/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
// - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
// - [English] https://www.cocos2d-x.org/docs/creator/manual/en/scripting/life-cycle-callbacks.html
cc.Class({
extends: cc.Component,
properties: {
// foo: {
// // ATTRIBUTES:
// default: null, // The default value will be used only when the component attaching
// // to a node for the first time
// type: cc.SpriteFrame, // optional, default is typeof default
// serializable: true, // optional, default is true
// },
// bar: {
// get () {
// return this._bar;
// },
// set (value) {
// this._bar = value;
// }
// },
},
// LIFE-CYCLE CALLBACKS:
onLoad () {
//定义一个点的三种方式
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.v2(0, 0);
var dst = cc.v2(100, 100);
//var dir = cc.pSub(src, dst); //已过时
//var len = cc.pLength(dir); //已过时
var dir = src.sub(dst); //相减
var len = dir.mag(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.v2(100, 100));
console.log(ret);
//当前矩形与指定矩形是否相交
var rhs = cc.rect(100, 100, 100, 100);
ret = r.intersects(rhs);
console.log(ret);
//坐标系
//将一个相对于节点左下角的坐标位置转换到世界空间坐标系,这个 API 的设计是为了和 cocos2d-x 中行为一致,更多情况下你可能需要使用 convertToWorldSpaceAR
var world_pos = this.node.convertToWorldSpace(cc.v2(0, 0));
console.log(world_pos); //x: 430, y: 270
//将节点坐标系下的一个点转换到世界空间坐标系,这个坐标系以锚点为原点
world_pos = this.node.convertToWorldSpaceAR(cc.v2(0, 0));
console.log(world_pos); //x: 480, y: 320
var world_pos = cc.v2(480, 320);
//将一个点转换到节点 (局部) 坐标系
var node_pos = this.node.convertToNodeSpace(world_pos);
console.log(node_pos); //x: 50, y: 50
//将一个点转换到节点 (局部) 空间坐标系,这个坐标系以锚点为原点。
node_pos = this.node.convertToNodeSpaceAR(world_pos);
console.log(node_pos); //x: 0, y: 0
},
start () {
},
// update (dt) {},
});
contain_box.js
// Learn cc.Class:
// - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/class.html
// - [English] http://docs.cocos2d-x.org/creator/manual/en/scripting/class.html
// Learn Attribute:
// - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
// - [English] http://docs.cocos2d-x.org/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
// - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
// - [English] https://www.cocos2d-x.org/docs/creator/manual/en/scripting/life-cycle-callbacks.html
cc.Class({
extends: cc.Component,
properties: {
// foo: {
// // ATTRIBUTES:
// default: null, // The default value will be used only when the component attaching
// // to a node for the first time
// type: cc.SpriteFrame, // optional, default is typeof default
// serializable: true, // optional, default is true
// },
// bar: {
// get () {
// return this._bar;
// },
// set (value) {
// this._bar = value;
// }
// },
},
// LIFE-CYCLE CALLBACKS:
// onLoad () {},
start () {
//返回父节坐标系下的轴向对齐的包围盒
var box = this.node.getBoundingBox();
console.log(box);
//返回节点在世界坐标系下的对齐轴向的包围盒(AABB)。该边框包含自身和已激活的子节点的世界边框。
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.v2(900, 600));
this.node.setPosition(node_pos);
//end
//获取当前节点的世界坐标
node_pos = this.node.convertToNodeSpaceAR(cc.v2(0, 0));
console.log(node_pos);
},
// update (dt) {},
});
注: creator屏幕左下角为原点
工程截图