cocos creator简单碰撞检测

这篇博客介绍了如何利用cc模块中的CollisionManager类来处理游戏或应用中的节点碰撞检测。通过启用碰撞管理器,设置绘制选项,以及定义不同的碰撞回调函数(onCollisionEnter、onCollisionStay和onCollisionExit),可以实现节点间的碰撞交互,如颜色变化和触摸计数。示例代码展示了如何设置和使用这些功能。
摘要由CSDN通过智能技术生成

CollisionManager 类型

模块: cc

一个简单的碰撞组件管理类,用于处理节点之间的碰撞组件是否产生了碰撞,并调用相应回调函数。

示例

// 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;

//如果不用回调函数那么就是
/*
onCollisionEnter(other,self){
    
}
*/

// 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;
    }
}
CocosCreator实现的 解救人质 游戏,学会碰撞检测rescue.7z // Bullet.js cc.Class({ extends: cc.Component, properties: { mSpeed: 300, }, // LIFE-CYCLE CALLBACKS: // onLoad () {}, start() { var manager = cc.director.getCollisionManager(); // 获取碰撞检测系统 manager.enabled = true; }, update(dt) { // 设置子弹移动,当超出屏幕范围未发生碰撞时自动销毁 this.node.y += this.mSpeed * dt; if (this.node.y > 580) { console.log('超出屏幕范围,子弹销毁!'); this.node.destroy(); } }, /** * 当碰撞产生的时候调用 * @param {Collider} other 产生碰撞的另一个碰撞组件 * @param {Collider} self 产生碰撞的自身的碰撞组件 */ onCollisionEnter: function (other, self) { console.log('on collision enter'); if (other.tag == 1) { // 子弹碰到人质时,解救失败! console.log('解救人质失败!'); var failLabel = this.node.parent.getChildByName('failLabel'); failLabel.active = true; this.node.destroy(); } else if (other.tag == 2) { // 子弹碰到敌人时,解救成功! console.log('解救人质成功!'); var successLabel = this.node.parent.getChildByName('successLabel'); successLabel.active = true; this.node.destroy(); } }, /** * 当碰撞产生后,碰撞结束前的情况下,每次计算碰撞结果后调用 * @param {Collider} other 产生碰撞的另一个碰撞组件 * @param {Collider} self 产生碰撞的自身的碰撞组件 */ onCollisionStay: function (other, self) { console.log('on collision stay'); }, /** * 当碰撞结束后调用 * @param {Collider} other 产生碰撞的另一个碰撞组件 * @param {Collider} self 产生碰撞的自身的碰撞组件 */ onCollisionExit: function (other, self) { console.log('on collision exit'); } });
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值
>