Cocos Creator API

1.0.0

Cocos Creator is a highly customizable game development tool that utilizes the power of cocos2d-x.

Vec2

Extends ValueType
Module: cc

Representation of 2D vectors and points.

Properties

ONE Vec2 static

return a Vec2 object with x = 1 and y = 1.

ZERO Vec2 static

return a Vec2 object with x = 0 and y = 0.

up Vec2 static

return a Vec2 object with x = 0 and y = 1.

RIGHT Vec2 static

return a Vec2 object with x = 1 and y = 0.

There are no properties that match your current filter settings. You can change your filter settings in the index section on this page. index

Methods

Vec2
(
  • [x =0]
  • [y =0]
)
Vec2

Constructor see Cc/vec2:method or cc.p

name type description
x optional number 0
y optional number 0

returns:

type: Vec2

clone ( ) Vec2

clone a Vec2 value

returns:

type: Vec2

set
(
  • newValue
)
Vec2

TODO

name type description
newValue Vec2

!#en new value to set. !#zh 要设置的新值

  • x number optional , default: 0
  • y number optional , default: 0

returns:

type: Vec2

returns this

equals
(
  • other
)
Boolean

TODO

name type description
other Vec2

!#en Constructor see Cc/vec2:method or cc.p !#zh 构造函数,可查看 Cc/vec2:method 或者 cc.p

  • x number optional , default: 0
  • y number optional , default: 0

returns:

type: Boolean

toString ( ) string

TODO

returns:

type: string

lerp
(
  • to
  • ratio
  • [out ]
)
Vec2

TODO

name type description
to Vec2

!#en Constructor see Cc/vec2:method or cc.p !#zh 构造函数,可查看 Cc/vec2:method 或者 cc.p

  • x number optional , default: 0
  • y number optional , default: 0
ratio number

the interpolation coefficient

out optional Vec2

optional, the receiving vector

  • x number optional , default: 0
  • y number optional , default: 0

returns:

type: Vec2

addSelf
(
  • vector
)
Vec2

Adds this vector. If you want to save result to another vector, use add() instead.

name type description
vector Vec2

!#en Constructor see Cc/vec2:method or cc.p !#zh 构造函数,可查看 Cc/vec2:method 或者 cc.p

  • x number optional , default: 0
  • y number optional , default: 0

returns:

type: Vec2

returns this

examples:

var v = cc.v2(10, 10);
v.addSelf(cc.v2(5, 5));// return Vec2 {x: 15, y: 15};

add
(
  • vector
  • [out ]
)
Vec2

Adds two vectors, and returns the new result.

name type description
vector Vec2

!#en Constructor see Cc/vec2:method or cc.p !#zh 构造函数,可查看 Cc/vec2:method 或者 cc.p

  • x number optional , default: 0
  • y number optional , default: 0
out optional Vec2

optional, the receiving vector

  • x number optional , default: 0
  • y number optional , default: 0

returns:

type: Vec2

the result

examples:

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
(
  • vector
)
Vec2

Subtracts one vector from this. If you want to save result to another vector, use sub() instead.

name type description
vector Vec2

!#en Constructor see Cc/vec2:method or cc.p !#zh 构造函数,可查看 Cc/vec2:method 或者 cc.p

  • x number optional , default: 0
  • y number optional , default: 0

returns:

type: Vec2

returns this

examples:

var v = cc.v2(10, 10);
v.subSelf(cc.v2(5, 5));// return Vec2 {x: 5, y: 5};

sub
(
  • vector
  • [out ]
)
Vec2

Subtracts one vector from this, and returns the new result.

name type description
vector Vec2

!#en Constructor see Cc/vec2:method or cc.p !#zh 构造函数,可查看 Cc/vec2:method 或者 cc.p

  • x number optional , default: 0
  • y number optional , default: 0
out optional Vec2

optional, the receiving vector

  • x number optional , default: 0
  • y number optional , default: 0

returns:

type: Vec2

the result

examples:

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
(
  • num
)
Vec2

Multiplies this by a number. If you want to save result to another vector, use mul() instead.

name type description
num number

returns:

type: Vec2

returns this

examples:

var v = cc.v2(10, 10);
v.mulSelf(5);// return Vec2 {x: 50, y: 50};

mul
(
  • num
  • [out ]
)
Vec2

Multiplies by a number, and returns the new result.

name type description
num number
out optional Vec2

optional, the receiving vector

  • x number optional , default: 0
  • y number optional , default: 0

returns:

type: Vec2

the result

examples:

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
(
  • vector
)
Vec2

Multiplies two vectors.

name type description
vector Vec2

!#en Constructor see Cc/vec2:method or cc.p !#zh 构造函数,可查看 Cc/vec2:method 或者 cc.p

  • x number optional , default: 0
  • y number optional , default: 0

returns:

type: Vec2

returns this

examples:

var v = cc.v2(10, 10);
v.scaleSelf(cc.v2(5, 5));// return Vec2 {x: 50, y: 50};

scale
(
  • vector
  • [out ]
)
Vec2

Multiplies two vectors, and returns the new result.

name type description
vector Vec2

!#en Constructor see Cc/vec2:method or cc.p !#zh 构造函数,可查看 Cc/vec2:method 或者 cc.p

  • x number optional , default: 0
  • y number optional , default: 0
out optional Vec2

optional, the receiving vector

  • x number optional , default: 0
  • y number optional , default: 0

returns:

type: Vec2

the result

examples:

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
(
  • vector
)
Vec2

Divides by a number. If you want to save result to another vector, use div() instead.

name type description
vector Vec2

!#en Constructor see Cc/vec2:method or cc.p !#zh 构造函数,可查看 Cc/vec2:method 或者 cc.p

  • x number optional , default: 0
  • y number optional , default: 0

returns:

type: Vec2

returns this

examples:

var v = cc.v2(10, 10);
v.divSelf(5); // return Vec2 {x: 2, y: 2};

div
(
  • vector
  • [out ]
)
Vec2

Divides by a number, and returns the new result.

name type description
vector Vec2

!#en Constructor see Cc/vec2:method or cc.p !#zh 构造函数,可查看 Cc/vec2:method 或者 cc.p

  • x number optional , default: 0
  • y number optional , default: 0
out optional Vec2

optional, the receiving vector

  • x number optional , default: 0
  • y number optional , default: 0

returns:

type: Vec2

the result

examples:

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 ( ) Vec2

Negates the components. If you want to save result to another vector, use neg() instead.

returns:

type: Vec2

returns this

examples:

var v = cc.v2(10, 10);
v.negSelf(); // return Vec2 {x: -10, y: -10};

neg
(
  • [out ]
)
Vec2

Negates the components, and returns the new result.

name type description
out optional Vec2

optional, the receiving vector

  • x number optional , default: 0
  • y number optional , default: 0

returns:

type: Vec2

the result

examples:

var v = cc.v2(10, 10);
var v1;
v.neg(v1);  // return Vec2 {x: -10, y: -10};

dot
(
  • [vector ]
)
number

Dot product

name type description
vector optional Vec2

!#en Constructor see Cc/vec2:method or cc.p !#zh 构造函数,可查看 Cc/vec2:method 或者 cc.p

  • x number optional , default: 0
  • y number optional , default: 0

returns:

type: number

the result

examples:

var v = cc.v2(10, 10);
v.dot(cc.v2(5, 5)); // return 100;

cross
(
  • [vector ]
)
number

Cross product

name type description
vector optional Vec2

!#en Constructor see Cc/vec2:method or cc.p !#zh 构造函数,可查看 Cc/vec2:method 或者 cc.p

  • x number optional , default: 0
  • y number optional , default: 0

returns:

type: number

the result

examples:

var v = cc.v2(10, 10);
v.cross(cc.v2(5, 5)); // return 0;

mag ( ) number

Returns the length of this vector.

returns:

type: number

the result

examples:

var v = cc.v2(10, 10);
v.mag(); // return 14.142135623730951;

magSqr ( ) number

Returns the squared length of this vector.

returns:

type: number

the result

examples:

var v = cc.v2(10, 10);
v.magSqr(); // return 200;

normalizeSelf ( ) Vec2

Make the length of this vector to 1.

returns:

type: Vec2

returns this

examples:

var v = cc.v2(10, 10);
v.normalizeSelf(); // return Vec2 {x: 0.7071067811865475, y: 0.7071067811865475};

normalize
(
  • [out ]
)
Vec2

Returns this vector with a magnitude of 1.

Note that the current vector is unchanged and a new normalized vector is returned. If you want to normalize the current vector, use normalizeSelf function.

name type description
out optional Vec2

optional, the receiving vector

  • x number optional , default: 0
  • y number optional , default: 0

returns:

type: Vec2

result var v = cc.v2(10, 10); v.normalize(); // return Vec2 {x: 0.7071067811865475, y: 0.7071067811865475};

angle
(
  • vector
)
number

Get angle in radian between this and vector.

name type description
vector Vec2

!#en Constructor see Cc/vec2:method or cc.p !#zh 构造函数,可查看 Cc/vec2:method 或者 cc.p

  • x number optional , default: 0
  • y number optional , default: 0

returns:

type: number

from 0 to Math.PI

signAngle
(
  • vector
)
number

Get angle in radian between this and vector with direction.

name type description
vector Vec2

!#en Constructor see Cc/vec2:method or cc.p !#zh 构造函数,可查看 Cc/vec2:method 或者 cc.p

  • x number optional , default: 0
  • y number optional , default: 0

returns:

type: number

from -MathPI to Math.PI

rotate
(
  • radians
  • [out ]
)
Vec2

rotate

name type description
radians number
out optional Vec2

optional, the receiving vector

  • x number optional , default: 0
  • y number optional , default: 0

returns:

type: Vec2

the result

rotateSelf
(
  • radians
)
Vec2

rotate self

name type description
radians number

returns:

type: Vec2

returns this

There are no methods that match your current filter settings. You can change your filter settings in the index section on this page. index