CollisionManager 类型
一个简单的碰撞组件管理类,用于处理节点之间的碰撞组件是否产生了碰撞,并调用相应回调函数。
示例
// Get the collision manager.
let manager = cc.director.getCollisionManager();
// Enabled the colider manager.
manager.enabled = true;
// Enabled draw collider
manager.enabledDebugDraw = true;
// Enabled draw collider bounding box
manager.enabledDrawBoundingBox = true;
// Collision callback
onCollisionEnter: function (other, self) {
this.node.color = cc.Color.RED;
this.touchingNumber ++;
// let world = self.world;
// let aabb = world.aabb;
// let preAabb = world.preAabb;
// let m = world.matrix;
// for circle collider
// let r = world.radius;
// let p = world.position;
// for box collider and polygon collider
// let ps = world.points;
},
onCollisionStay: function (other, self) {
console.log('on collision stay');
},
onCollisionExit: function (other, self) {
this.touchingNumber --;
if (this.touchingNumber === 0) {
this.node.color = cc.Color.WHITE;
}
}
索引
属性(properties)
enabledBoolean是否开启碰撞管理,默认为不开启enabledDrawBoundingBoxBoolean是否绘制碰撞组件的包围盒,默认为不绘制enabledDebugDrawBoolean是否绘制碰撞组件的形状,默认为不绘制
方法
hasEventListener检查事件目标对象是否有为特定类型的事件注册的回调。on注册事件目标的特定事件类型回调。off删除之前用同类型,回调,目标或 useCapture 注册的事件监听器,如果只传递 type,将会删除 type 类型的所有事件监听器。targetOff在当前 EventTarget 上删除指定目标(target 参数)注册的所有事件监听器。once注册事件目标的特定事件类型回调,回调会在第一时间被触发后删除自身。dispatchEvent通过事件对象派发事件
Details
属性(properties)
enabled
是否开启碰撞管理,默认为不开启
| meta | description |
|---|---|
| 类型 | Boolean |
| 定义于 | cocos2d/core/collider/CCCollisionManager.js:121 |
enabledDrawBoundingBox
是否绘制碰撞组件的包围盒,默认为不绘制
| meta | description |
|---|---|
| 类型 | Boolean |
| 定义于 | cocos2d/core/collider/CCCollisionManager.js:129 |
enabledDebugDraw
是否绘制碰撞组件的形状,默认为不绘制
| meta | description |
|---|---|
| 类型 | Boolean |
| 定义于 | cocos2d/core/collider/CCCollisionManager.js:468 |
方法
hasEventListener
检查事件目标对象是否有为特定类型的事件注册的回调。
| meta | description |
|---|---|
| 返回 | Boolean |
| 定义于 | cocos2d/core/event/event-target.js:69 |
参数列表
typeString The type of event.
on
注册事件目标的特定事件类型回调。这种类型的事件应该被 emit 触发。
| meta | description |
|---|---|
| 返回 | Function |
| 定义于 | cocos2d/core/event/event-target.js:77 |
参数列表
typeString A string representing the event type to listen for.callbackFunction The callback that will be invoked when the event is dispatched.The callback is ignored if it is a duplicate (the callbacks are unique).arg1Any arg1arg2Any arg2arg3Any arg3arg4Any arg4arg5Any arg5
targetObject The target (this object) to invoke the callback, can be null
示例
eventTarget.on('fire', function () {
cc.log("fire in the hole");
}, node);
off
删除之前用同类型,回调,目标或 useCapture 注册的事件监听器,如果只传递 type,将会删除 type 类型的所有事件监听器。
| meta | description |
|---|---|
| 定义于 | cocos2d/core/event/event-target.js:119 |
参数列表
typeString A string representing the event type being removed.callbackFunction The callback to remove.targetObject The target (this object) to invoke the callback, if it's not given, only callback without target will be removed
示例
// register fire eventListener
var callback = eventTarget.on('fire', function () {
cc.log("fire in the hole");
}, target);
// remove fire event listener
eventTarget.off('fire', callback, target);
// remove all fire event listeners
eventTarget.off('fire');
targetOff
在当前 EventTarget 上删除指定目标(target 参数)注册的所有事件监听器。 这个函数无法删除当前 EventTarget 的所有事件监听器,也无法删除 target 参数所注册的所有事件监听器。 这个函数只能删除 target 参数在当前 EventTarget 上注册的所有事件监听器。
| meta | description |
|---|---|
| 定义于 | cocos2d/core/event/event-target.js:163 |
参数列表
targetObject The target to be searched for all related listeners
once
注册事件目标的特定事件类型回调,回调会在第一时间被触发后删除自身。
| meta | description |
|---|---|
| 定义于 | cocos2d/core/event/event-target.js:182 |
参数列表
typeString A string representing the event type to listen for.callbackFunction The callback that will be invoked when the event is dispatched.The callback is ignored if it is a duplicate (the callbacks are unique).arg1Any arg1arg2Any arg2arg3Any arg3arg4Any arg4arg5Any arg5
targetObject The target (this object) to invoke the callback, can be null
示例
eventTarget.once('fire', function () {
cc.log("this is the callback and will be invoked only once");
}, node);
dispatchEvent
通过事件对象派发事件
| meta | description |
|---|---|
| 定义于 | cocos2d/core/event/event-target.js:208 |
参数列表
eventEvent