Cocos Creator 碰撞事件

分为两种碰撞

准备条件
因为碰撞和物理引擎默认都是不开启的, 所以下面这两个方法必须先要开启才能使用, 否则白搭
条件, 在scene根目录或者根节点下初始启动代码, 必须在onLoad事件中执行

onLoad () {
    //开启碰撞功能(非物理)
    var collider = cc.director.getCollisionManager();
    collider.enabled = true;
    // collider.enabledDebugDraw = true;
    // collider.enabledDrawBoundingBox = true;

    //开启物理引擎
    var physics = cc.director.getPhysicsManager();
    physics.enabled = true;
    // physics.gravity.y = 1000;
    var db = cc.PhysicsManager.DrawBits;
    // physics.debugDrawFlags = db.e_aabbBit|db.e_jointBit|db.e_shapeBit;
}

形状碰撞 Collider
在这里插入图片描述
包括box collider / circle collider / poly collider
添加碰撞事件
要求碰撞的两个对象, 都必须添加collider组建

onCollisionEnter(other:cc.Component, self){
  console.log("collision enter");
}

onCollisionStay(other:cc.Component, self){
  console.log("collision stay");
}

onCollisionExit(other:cc.Component, self){
  console.log("collision exit");
}

物理碰撞 PhysicsCollider
需要两个对象都添加RigidBody 刚体, PhysicsCollider 碰撞体
两个刚体必须都开启碰撞侦听 (Enable Collider Listen)
如果不想产生碰撞, 而只是产生碰撞事件, 那么勾选sensor

在这里插入图片描述
在这里插入图片描述

onBeginContact(contact, selfCollider, otherCollider){
    console.log("onBeginContact");
}

onEndContact(contact, selfCollider, otherCollider){
    console.log("onEndContact");
}

onPreSolve(contact, selfCollider, otherCollider) {
    
}

onPostSolve(contact, selfCollider, otherCollider) {
    
}
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
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
发出的红包

打赏作者

reg183

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值