Vec2 类型
继承于 ValueType
表示 2D 向量和坐标
索引
属性(properties)
xNumberyNumberONEVec2新 Vec2 对象。ZEROVec2返回 x = 0 和 y = 0 的 Vec2 对象。UPVec2返回 x = 0 和 y = 1 的 Vec2 对象。RIGHTVec2返回 x = 1 和 y = 0 的 Vec2 对象。
方法
constructor构造函数,可查看 Cc/vec2:method 或者 cc.pclone克隆一个 Vec2 对象set设置向量值。equals当前的向量是否与指定的向量相等。fuzzyEquals近似判断两个点是否相等。toString转换为方便阅读的字符串。lerp线性插值。clampf返回指定限制区域后的向量。addSelf向量加法。add向量加法,并返回新结果。subSelf向量减法。sub向量减法,并返回新结果。mulSelf缩放当前向量。mul缩放向量,并返回新结果。scaleSelf分量相乘。scale分量相乘,并返回新的结果。divSelf向量除法。div向量除法,并返回新的结果。negSelf向量取反。neg返回取反后的新向量。dot当前向量与指定向量进行点乘。cross当前向量与指定向量进行叉乘。mag返回该向量的长度。magSqr返回该向量的长度平方。normalizeSelf向量归一化,让这个向量的长度为 1。normalize返回归一化后的向量。angle夹角的弧度。signAngle带方向的夹角的弧度。rotate返回旋转给定弧度后的新向量。rotateSelf按指定弧度旋转向量。project返回当前向量在指定 vector 向量上的投影向量。transformMat4Transforms the vec2 with a mat4. 3rd vector component is implicitly '0', 4th vector component is implicitly '1'
Details
属性(properties)
x
| meta | description |
|---|---|
| 类型 | Number |
| 定义于 | cocos2d/core/value-types/vec2.js:65 |
y
| meta | description |
|---|---|
| 类型 | Number |
| 定义于 | cocos2d/core/value-types/vec2.js:68 |
ONE
新 Vec2 对象。
| meta | description |
|---|---|
| 类型 | Vec2 |
| 定义于 | cocos2d/core/value-types/vec2.js:608 |
ZERO
返回 x = 0 和 y = 0 的 Vec2 对象。
| meta | description |
|---|---|
| 类型 | Vec2 |
| 定义于 | cocos2d/core/value-types/vec2.js:619 |
UP
返回 x = 0 和 y = 1 的 Vec2 对象。
| meta | description |
|---|---|
| 类型 | Vec2 |
| 定义于 | cocos2d/core/value-types/vec2.js:630 |
RIGHT
返回 x = 1 和 y = 0 的 Vec2 对象。
| meta | description |
|---|---|
| 类型 | Vec2 |
| 定义于 | cocos2d/core/value-types/vec2.js:641 |
方法
constructor
构造函数,可查看 Cc/vec2:method 或者 cc.p
| meta | description |
|---|---|
| 定义于 | cocos2d/core/value-types/vec2.js:44 |
参数列表
clone
克隆一个 Vec2 对象
| meta | description |
|---|---|
| 返回 | Vec2 |
| 定义于 | cocos2d/core/value-types/vec2.js:75 |
set
设置向量值。
| meta | description |
|---|---|
| 返回 | Vec2 |
| 定义于 | cocos2d/core/value-types/vec2.js:85 |
参数列表
newValueVec2 !#en new value to set. !#zh 要设置的新值
equals
当前的向量是否与指定的向量相等。
| meta | description |
|---|---|
| 返回 | Boolean |
| 定义于 | cocos2d/core/value-types/vec2.js:99 |
参数列表
otherVec2
fuzzyEquals
近似判断两个点是否相等。
判断 2 个向量是否在指定数值的范围之内,如果在则返回 true,反之则返回 false。
| meta | description |
|---|---|
| 返回 | Boolean |
| 定义于 | cocos2d/core/value-types/vec2.js:110 |
参数列表
toString
转换为方便阅读的字符串。
| meta | description |
|---|---|
| 返回 | string |
| 定义于 | cocos2d/core/value-types/vec2.js:128 |
lerp
线性插值。
| meta | description |
|---|---|
| 返回 | Vec2 |
| 定义于 | cocos2d/core/value-types/vec2.js:141 |
参数列表
toVec2rationumber the interpolation coefficientoutVec2 optional, the receiving vector, you can pass the same vec2 to save result to itself, if not provided, a new vec2 will be created
clampf
返回指定限制区域后的向量。
向量大于 max_inclusive 则返回 max_inclusive。
向量小于 min_inclusive 则返回 min_inclusive。
否则返回自身。
| meta | description |
|---|---|
| 返回 | Vec2 |
| 定义于 | cocos2d/core/value-types/vec2.js:159 |
参数列表
示例
var min_inclusive = cc.v2(0, 0);
var max_inclusive = cc.v2(20, 20);
var v1 = cc.v2(20, 20).clampf(min_inclusive, max_inclusive); // Vec2 {x: 20, y: 20};
var v2 = cc.v2(0, 0).clampf(min_inclusive, max_inclusive); // Vec2 {x: 0, y: 0};
var v3 = cc.v2(10, 10).clampf(min_inclusive, max_inclusive); // Vec2 {x: 10, y: 10};
addSelf
向量加法。如果你想保存结果到另一个向量,使用 add() 代替。
| meta | description |
|---|---|
| 返回 | Vec2 |
| 定义于 | cocos2d/core/value-types/vec2.js:183 |
参数列表
vectorVec2
示例
var v = cc.v2(10, 10);
v.addSelf(cc.v2(5, 5));// return Vec2 {x: 15, y: 15};
add
向量加法,并返回新结果。
| meta | description |
|---|---|
| 返回 | Vec2 |
| 定义于 | cocos2d/core/value-types/vec2.js:200 |
参数列表
vectorVec2outVec2 optional, the receiving vector, you can pass the same vec2 to save result to itself, if not provided, a new vec2 will be created
示例
var v = cc.v2(10, 10);
v.add(cc.v2(5, 5)); // return Vec2 {x: 15, y: 15};
var v1;
v.add(cc.v2(5, 5), v1); // return Vec2 {x: 15, y: 15};
subSelf
向量减法。如果你想保存结果到另一个向量,可使用 sub() 代替。
| meta | description |
|---|---|
| 返回 | Vec2 |
| 定义于 | cocos2d/core/value-types/vec2.js:220 |
参数列表
vectorVec2
示例
var v = cc.v2(10, 10);
v.subSelf(cc.v2(5, 5));// return Vec2 {x: 5, y: 5};
sub
向量减法,并返回新结果。
| meta | description |
|---|---|
| 返回 | Vec2 |
| 定义于 | cocos2d/core/value-types/vec2.js:237 |
参数列表
vectorVec2outVec2 optional, the receiving vector, you can pass the same vec2 to save result to itself, if not provided, a new vec2 will be created
示例
var v = cc.v2(10, 10);
v.sub(cc.v2(5, 5)); // return Vec2 {x: 5, y: 5};
var v1;
v.sub(cc.v2(5, 5), v1); // return Vec2 {x: 5, y: 5};
mulSelf
缩放当前向量。如果你想结果保存到另一个向量,可使用 mul() 代替。
| meta | description |
|---|---|
| 返回 | Vec2 |
| 定义于 | cocos2d/core/value-types/vec2.js:257 |
参数列表
numnumber
示例
var v = cc.v2(10, 10);
v.mulSelf(5);// return Vec2 {x: 50, y: 50};
mul
缩放向量,并返回新结果。
| meta | description |
|---|---|
| 返回 | Vec2 |
| 定义于 | cocos2d/core/value-types/vec2.js:274 |
参数列表
numnumberoutVec2 optional, the receiving vector, you can pass the same vec2 to save result to itself, if not provided, a new vec2 will be created
示例
var v = cc.v2(10, 10);
v.mul(5); // return Vec2 {x: 50, y: 50};
var v1;
v.mul(5, v1); // return Vec2 {x: 50, y: 50};
scaleSelf
分量相乘。
| meta | description |
|---|---|
| 返回 | Vec2 |
| 定义于 | cocos2d/core/value-types/vec2.js:294 |
参数列表
vectorVec2
示例
var v = cc.v2(10, 10);
v.scaleSelf(cc.v2(5, 5));// return Vec2 {x: 50, y: 50};
scale
分量相乘,并返回新的结果。
| meta | description |
|---|---|
| 返回 | Vec2 |
| 定义于 | cocos2d/core/value-types/vec2.js:311 |
参数列表
vectorVec2outVec2 optional, the receiving vector, you can pass the same vec2 to save result to itself, if not provided, a new vec2 will be created
示例
var v = cc.v2(10, 10);
v.scale(cc.v2(5, 5)); // return Vec2 {x: 50, y: 50};
var v1;
v.scale(cc.v2(5, 5), v1); // return Vec2 {x: 50, y: 50};
divSelf
向量除法。如果你想结果保存到另一个向量,可使用 div() 代替。
| meta | description |
|---|---|
| 返回 | Vec2 |
| 定义于 | cocos2d/core/value-types/vec2.js:331 |
参数列表
divisornumber
示例
var v = cc.v2(10, 10);
v.divSelf(5); // return Vec2 {x: 2, y: 2};
div
向量除法,并返回新的结果。
| meta | description |
|---|---|
| 返回 | Vec2 |
| 定义于 | cocos2d/core/value-types/vec2.js:348 |
参数列表
divisorVec2outVec2 optional, the receiving vector, you can pass the same vec2 to save result to itself, if not provided, a new vec2 will be created
示例
var v = cc.v2(10, 10);
v.div(5); // return Vec2 {x: 2, y: 2};
var v1;
v.div(5, v1); // return Vec2 {x: 2, y: 2};
negSelf
向量取反。如果你想结果保存到另一个向量,可使用 neg() 代替。
| meta | description |
|---|---|
| 返回 | Vec2 |
| 定义于 | cocos2d/core/value-types/vec2.js:368 |
示例
var v = cc.v2(10, 10);
v.negSelf(); // return Vec2 {x: -10, y: -10};
neg
返回取反后的新向量。
| meta | description |
|---|---|
| 返回 | Vec2 |
| 定义于 | cocos2d/core/value-types/vec2.js:384 |
参数列表
outVec2 optional, the receiving vector, you can pass the same vec2 to save result to itself, if not provided, a new vec2 will be created
示例
var v = cc.v2(10, 10);
var v1;
v.neg(v1); // return Vec2 {x: -10, y: -10};
dot
当前向量与指定向量进行点乘。
| meta | description |
|---|---|
| 返回 | number |
| 定义于 | cocos2d/core/value-types/vec2.js:402 |
参数列表
vectorVec2
示例
var v = cc.v2(10, 10);
v.dot(cc.v2(5, 5)); // return 100;
cross
当前向量与指定向量进行叉乘。
| meta | description |
|---|---|
| 返回 | number |
| 定义于 | cocos2d/core/value-types/vec2.js:416 |
参数列表
vectorVec2
示例
var v = cc.v2(10, 10);
v.cross(cc.v2(5, 5)); // return 0;
mag
返回该向量的长度。
| meta | description |
|---|---|
| 返回 | number |
| 定义于 | cocos2d/core/value-types/vec2.js:430 |
示例
var v = cc.v2(10, 10);
v.mag(); // return 14.142135623730951;
magSqr
返回该向量的长度平方。
| meta | description |
|---|---|
| 返回 | number |
| 定义于 | cocos2d/core/value-types/vec2.js:443 |
示例
var v = cc.v2(10, 10);
v.magSqr(); // return 200;
normalizeSelf
向量归一化,让这个向量的长度为 1。
| meta | description |
|---|---|
| 返回 | Vec2 |
| 定义于 | cocos2d/core/value-types/vec2.js:456 |
示例
var v = cc.v2(10, 10);
v.normalizeSelf(); // return Vec2 {x: 0.7071067811865475, y: 0.7071067811865475};
normalize
返回归一化后的向量。
注意,当前向量不变,并返回一个新的归一化向量。如果你想来归一化当前向量,可使用 normalizeSelf 函数。
| meta | description |
|---|---|
| 返回 | Vec2 |
| 定义于 | cocos2d/core/value-types/vec2.js:482 |
参数列表
outVec2 optional, the receiving vector, you can pass the same vec2 to save result to itself, if not provided, a new vec2 will be created
angle
夹角的弧度。
| meta | description |
|---|---|
| 返回 | number |
| 定义于 | cocos2d/core/value-types/vec2.js:505 |
参数列表
vectorVec2
signAngle
带方向的夹角的弧度。
| meta | description |
|---|---|
| 返回 | number |
| 定义于 | cocos2d/core/value-types/vec2.js:527 |
参数列表
vectorVec2
rotate
返回旋转给定弧度后的新向量。
| meta | description |
|---|---|
| 返回 | Vec2 |
| 定义于 | cocos2d/core/value-types/vec2.js:539 |
参数列表
radiansnumberoutVec2 optional, the receiving vector, you can pass the same vec2 to save result to itself, if not provided, a new vec2 will be created
rotateSelf
按指定弧度旋转向量。
| meta | description |
|---|---|
| 返回 | Vec2 |
| 定义于 | cocos2d/core/value-types/vec2.js:554 |
参数列表
radiansnumber
project
返回当前向量在指定 vector 向量上的投影向量。
| meta | description |
|---|---|
| 返回 | Vec2 |
| 定义于 | cocos2d/core/value-types/vec2.js:571 |
参数列表
vectorVec2
示例
var v1 = cc.v2(20, 20);
var v2 = cc.v2(5, 5);
v1.project(v2); // Vec2 {x: 20, y: 20};
transformMat4
Transforms the vec2 with a mat4. 3rd vector component is implicitly '0', 4th vector component is implicitly '1'
| meta | description |
|---|---|
| 返回 | Vec2 |
| 定义于 | cocos2d/core/value-types/vec2.js:586 |
参数列表
mMat4 matrix to transform withm00Number Component in column 0, row 0 position (index 0)m01Number Component in column 0, row 1 position (index 1)m02Number Component in column 0, row 2 position (index 2)m03Number Component in column 0, row 3 position (index 3)m10Number Component in column 1, row 0 position (index 4)m11Number Component in column 1, row 1 position (index 5)m12Number Component in column 1, row 2 position (index 6)m13Number Component in column 1, row 3 position (index 7)m20Number Component in column 2, row 0 position (index 8)m21Number Component in column 2, row 1 position (index 9)m22Number Component in column 2, row 2 position (index 10)m23Number Component in column 2, row 3 position (index 11)m30Number Component in column 3, row 0 position (index 12)m31Number Component in column 3, row 1 position (index 13)m32Number Component in column 3, row 2 position (index 14)m33Number Component in column 3, row 3 position (index 15)
outVec2 the receiving vector, you can pass the same vec2 to save result to itself, if not provided, a new vec2 will be created