Cocos Creator API

1.5.x

Cocos Creator is the game engine for the future.

PhysicsManager

Module: cc
Parent Module: cc

Physics manager uses box2d as the inner physics system, and hide most box2d implement details(creating rigidbody, synchronize rigidbody info to node). You can visit some common box2d function through physics manager(hit testing, raycast, debug info). Physics manager distributes the collision information to each collision callback when collision is produced. Note: You need first enable the collision listener in the rigidbody.

Properties

DrawBits DrawBits static

The draw bits for drawing physics debug information.

examples:

cc.director.getPhysicsManager().debugDrawFlags =
// cc.PhysicsManager.DrawBits.e_aabbBit |
// cc.PhysicsManager.DrawBits.e_pairBit |
// cc.PhysicsManager.DrawBits.e_centerOfMassBit |
cc.PhysicsManager.DrawBits.e_jointBit |
cc.PhysicsManager.DrawBits.e_shapeBit;

PTM_RATIO Number static

The ratio transform between physics unit and pixel unit, generally is 32.

enabled Boolean

Enabled the physics manager?

debugDrawFlags Number

Debug draw flags.

examples:

// enable all debug draw info
var Bits = cc.PhysicsManager.DrawBits;
cc.director.getPhysicsManager().debugDrawFlags = Bits.e_aabbBit |
Bits.e_pairBit |
Bits.e_centerOfMassBit |
Bits.e_jointBit |
Bits.e_shapeBit;

// disable debug draw info
cc.director.getPhysicsManager().debugDrawFlags = 0;

gravity Number

The physics world gravity.

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

testPoint
(
  • point
)
PhysicsCollider

Test which collider contains the given world point

name type description
point Vec2

the world point

returns:

testAABB
(
  • rect
)
[PhysicsCollider]

Test which colliders intersect the given world rect

name type description
rect Rect

the world rect

returns:

rayCast
(
  • p1
  • p2
  • type
)
[PhysicsRayCastResult]

Raycast the world for all colliders in the path of the ray. The raycast ignores colliders that contain the starting point.

name type description
p1 Vec2

start point of the raycast

p2 Vec2

end point of the raycast

type RayCastType

optional, default is RayCastType.Closest

returns:

attachDebugDrawToCamera
(
  • camera
)

Attach physics debug draw to camera

name type description
camera Camera

detachDebugDrawFromCamera
(
  • camera
)

Detach physics debug draw to camera

name type description
camera Camera

on
(
  • type
  • callback
  • [target ]
  • [useCapture =false]
)
Function

Inherited from EventTarget:

Register an callback of a specific event type on the EventTarget.

name type description
type String

A string representing the event type to listen for.

callback Function

The callback that will be invoked when the event is dispatched. The callback is ignored if it is a duplicate (the callbacks are unique).

target optional Object

The target to invoke the callback, can be null

useCapture optional Boolean false

When set to true, the capture argument prevents callback from being invoked when the event's eventPhase attribute value is BUBBLING_PHASE. When false, callback will NOT be invoked when event's eventPhase attribute value is CAPTURING_PHASE. Either way, callback will be invoked when event's eventPhase attribute value is AT_TARGET.

returns:

type: Function

Just returns the incoming callback so you can save the anonymous function easier.

examples:

node.on(cc.Node.EventType.TOUCH_END, function (event) {
    cc.log("this is callback");
}, node);

off
(
  • type
  • callback
  • [target ]
  • [useCapture =false]
)

Inherited from EventTarget:

Removes the callback previously registered with the same type, callback, target and or useCapture.

name type description
type String

A string representing the event type being removed.

callback Function

The callback to remove.

target optional Object

The target to invoke the callback, if it's not given, only callback without target will be removed

useCapture optional Boolean false

Specifies whether the callback being removed was registered as a capturing callback or not. If not specified, useCapture defaults to false. If a callback was registered twice, one with capture and one without, each must be removed separately. Removal of a capturing callback does not affect a non-capturing version of the same listener, and vice versa.

examples:

// register touchEnd eventListener
var touchEnd = node.on(cc.Node.EventType.TOUCH_END, function (event) {
    cc.log("this is callback");
}, node);
// remove touchEnd eventListener
node.off(cc.Node.EventType.TOUCH_END, touchEnd, node);

targetOff
(
  • target
)

Inherited from EventTarget:

Removes all callbacks previously registered with the same target (passed as parameter). This is not for removing all listeners in the current event target, and this is not for removing all listeners the target parameter have registered. It's only for removing all listeners (callback and target couple) registered on the current event target by the target parameter.

name type description
target Object

The target to be searched for all related listeners

once
(
  • type
  • callback
  • [target ]
  • [useCapture =false]
)

Inherited from EventTarget:

Register an callback of a specific event type on the EventTarget, the callback will remove itself after the first time it is triggered.

name type description
type String

A string representing the event type to listen for.

callback Function

The callback that will be invoked when the event is dispatched. The callback is ignored if it is a duplicate (the callbacks are unique).

target optional Object

The target to invoke the callback, can be null

useCapture optional Boolean false

When set to true, the capture argument prevents callback from being invoked when the event's eventPhase attribute value is BUBBLING_PHASE. When false, callback will NOT be invoked when event's eventPhase attribute value is CAPTURING_PHASE. Either way, callback will be invoked when event's eventPhase attribute value is AT_TARGET.

examples:

node.once(cc.Node.EventType.TOUCH_END, function (event) {
    cc.log("this is callback");
}, node);

dispatchEvent
(
  • event
)

Inherited from EventTarget:

Dispatches an event into the event flow. The event target is the EventTarget object upon which the dispatchEvent() method is called.

name type description
event Event

The Event object that is dispatched into the event flow

emit
(
  • message
  • [detail ]
)

Inherited from EventTarget:

Send an event to this object directly, this method will not propagate the event to any other objects. The event will be created from the supplied message, you can get the "detail" argument from event.detail.

name type description
message String

the message to send

detail optional Any

whatever argument the message needs

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