【文件属性】:
文件名称:CocosCreator实现的 解救人质 游戏,学会碰撞检测rescue.7z
文件大小:642KB
文件格式:7Z
更新时间:2023-01-02 04:42:27
CocosCreator 碰撞检测
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');
}
});