让子弹碰到飞机后子弹消失,飞机怪兽掉血
第一步:碰撞分组建立2组
第二步:给子弹增加circle碰撞体,给飞机怪兽添加box 碰撞体,并把子弹加入到子弹的碰撞体组,把飞机怪兽加入到monster组
第三步:开启碰撞
https://docs.cocos.com/creator/manual/zh/physics/collision/collision-manager.html
碰撞检测系统的使用
碰撞系统接口
获取碰撞检测系统:
var manager = cc.director.getCollisionManager();
默认碰撞检测系统是禁用的,如果需要使用则需要以下方法开启碰撞检测系统:
manager.enabled = true;
默认碰撞检测系统的 debug 绘制是禁用的,如果需要使用则需要以下方法开启 debug 绘制:
manager.enabledDebugDraw = true;
// Learn cc.Class:
// - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/class.html
// - [English] http://docs.cocos2d-x.org/creator/manual/en/scripting/class.html
// Learn Attribute:
// - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
// - [English] http://docs.cocos2d-x.org/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
// - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
// - [English] https://www.cocos2d-x.org/docs/creator/manual/en/scripting/life-cycle-callbacks.html
cc.Class({
extends: cc.Component,
properties: {
// 背景移动速度
bgMoveSpeed:0,
// 背景
bg:{
default:null,
type:cc.Node
},
bg2:{
default:null,
type:cc.Node
},
// 子弹预制体资源
bulletPrefab:{
default:null,
type:cc.Prefab
},
// 玩家节点
player:{
default:null,
type:cc.Node
},
monsterPrefab:{
default:null,
type:cc.Prefab
},
// foo: {
// // ATTRIBUTES:
// default: null, // The default value will be used only when the component attaching
// // to a node for the first time
// type: cc.SpriteFrame, // optional, default is typeof default
// serializable: true, // optional, default is true
// },
// bar: {
// get () {
// return this._bar;
// },
// set (value) {
// this._bar = value;
// }
// },
},
// LIFE-CYCLE CALLBACKS:
// 加载脚本时自动调用
onLoad () {
// 子弹发射计时器
this.bulletTimer = 0;
// 子弹发射频率
this.shootDuration = 0.5;
//怪兽飞机生成计时器
this.monsterTimer=0;
this.monsterDuration=1.5
//获取碰撞检测系统
var manager=cc.director.getCollisionManager();
//开启碰撞检测系统
manager.enabled=true;
//debug绘制
manager.enabledDebugDraw=true;
},
start () {
},
// 发射子弹
shoot(dt){
// 累计时间
this.bulletTimer += dt;
// 是否达到发射时间
if(this.bulletTimer >= this.shootDuration)
{
// 计时器归零
this.bulletTimer = 0;
// 实例化一个预制体对象
var bullet = cc.instantiate(this.bulletPrefab);
// 添加到画布上
this.node.addChild(bullet);
// 获取英雄的位置设置给子弹
var pos = cc.v2(this.player.x + this.player.width/2,this.player.y);
bullet.setPosition(pos);
}
},
//自动生成怪兽
produceMonster(dt){
this.monsterTimer+=dt;
if(this.monsterTimer>=this.monsterDuration){
//计时器归零
this.monsterTimer=0;
//实例化一个怪兽飞机对象
var monster=cc.instantiate(this.monsterPrefab);
//添加到canvas下,因为此脚本game.js是挂在canvas下的,所以this.node代表canvas的节点
this.node.addChild(monster);
//设置怪兽飞机的初始位置
var xx=427;
var yy=(Math.random()-0.5)*480;
monster.setPosition(cc.v2(xx,yy));
}
},
// 帧调度器。dt:两帧之间的时间间隔 speed s=vt
update (dt) {
this.bg.x = this.bg.x - this.bgMoveSpeed*dt;
this.bg2.x = this.bg2.x - this.bgMoveSpeed*dt;
if(this.bg.x <= -this.bg.width)
{
this.bg.x = this.bg2.x + this.bg2.width;
}
if(this.bg2.x <= -this.bg2.width)
{
this.bg2.x = this.bg.x + this.bg.width;
}
// 发射子弹
this.shoot(dt);
//自动生成怪兽飞机
this.produceMonster(dt);
},
});
第四步–写碰撞后的代码
bullet.js挂在bullet预制体上
// Learn cc.Class:
// - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/class.html
// - [English] http://docs.cocos2d-x.org/creator/manual/en/scripting/class.html
// Learn Attribute:
// - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
// - [English] http://docs.cocos2d-x.org/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
// - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
// - [English] https://www.cocos2d-x.org/docs/creator/manual/en/scripting/life-cycle-callbacks.html
cc.Class({
extends: cc.Component,
properties: {
// 子弹移动速度
moveSpeed:0
// foo: {
// // ATTRIBUTES:
// default: null, // The default value will be used only when the component attaching
// // to a node for the first time
// type: cc.SpriteFrame, // optional, default is typeof default
// serializable: true, // optional, default is true
// },
// bar: {
// get () {
// return this._bar;
// },
// set (value) {
// this._bar = value;
// }
// },
},
// LIFE-CYCLE CALLBACKS:
// onLoad () {},
start () {
},
onCollisionEnter:function(other,self){
this.node.destroy();
},
update (dt) {
// 每一帧移动的距离
var s = this.moveSpeed * dt;
// 修改子弹的位置
this.node.x = this.node.x + s;
},
});
monster.js:挂在monster预制体上(飞机)
// Learn cc.Class:
// - https://docs.cocos.com/creator/manual/en/scripting/class.html
// Learn Attribute:
// - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
// - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html
cc.Class({
extends: cc.Component,
properties: {
//移动速度
moveSpeed:-100,
//血量
hp:100
// foo: {
// // ATTRIBUTES:
// default: null, // The default value will be used only when the component attaching
// // to a node for the first time
// type: cc.SpriteFrame, // optional, default is typeof default
// serializable: true, // optional, default is true
// },
// bar: {
// get () {
// return this._bar;
// },
// set (value) {
// this._bar = value;
// }
// },
},
// LIFE-CYCLE CALLBACKS:
//onLoad () {},
/**
* onCollisionEnter: function (other, self) {
console.log('on collision enter');
// 碰撞系统会计算出碰撞组件在世界坐标系下的相关的值,并放到 world 这个属性里面
var world = self.world;
// 碰撞组件的 aabb 碰撞框
var aabb = world.aabb;
// 节点碰撞前上一帧 aabb 碰撞框的位置
var preAabb = world.preAabb;
// 碰撞框的世界矩阵
var t = world.transform;
// 以下属性为圆形碰撞组件特有属性
var r = world.radius;
var p = world.position;
// 以下属性为 矩形 和 多边形 碰撞组件特有属性
var ps = world.points;
},
*/
onCollisionEnter:function(other,self){
this.hp-=30+Math.random()*30;
if(this.hp<=0){
this.node.destroy();
}
},
start () {
},
update (dt) {
this.node.x+=this.moveSpeed*dt;
},
});
game.js(挂在canvas下)
// Learn cc.Class:
// - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/class.html
// - [English] http://docs.cocos2d-x.org/creator/manual/en/scripting/class.html
// Learn Attribute:
// - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
// - [English] http://docs.cocos2d-x.org/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
// - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
// - [English] https://www.cocos2d-x.org/docs/creator/manual/en/scripting/life-cycle-callbacks.html
cc.Class({
extends: cc.Component,
properties: {
// 背景移动速度
bgMoveSpeed:0,
// 背景
bg:{
default:null,
type:cc.Node
},
bg2:{
default:null,
type:cc.Node
},
// 子弹预制体资源
bulletPrefab:{
default:null,
type:cc.Prefab
},
// 玩家节点
player:{
default:null,
type:cc.Node
},
monsterPrefab:{
default:null,
type:cc.Prefab
},
// foo: {
// // ATTRIBUTES:
// default: null, // The default value will be used only when the component attaching
// // to a node for the first time
// type: cc.SpriteFrame, // optional, default is typeof default
// serializable: true, // optional, default is true
// },
// bar: {
// get () {
// return this._bar;
// },
// set (value) {
// this._bar = value;
// }
// },
},
// LIFE-CYCLE CALLBACKS:
// 加载脚本时自动调用
onLoad () {
// 子弹发射计时器
this.bulletTimer = 0;
// 子弹发射频率
this.shootDuration = 0.5;
//怪兽飞机生成计时器
this.monsterTimer=0;
this.monsterDuration=1.5
//获取碰撞检测系统
var manager=cc.director.getCollisionManager();
//开启碰撞检测系统
manager.enabled=true;
//debug绘制
manager.enabledDebugDraw=true;
},
start () {
},
// 发射子弹
shoot(dt){
// 累计时间
this.bulletTimer += dt;
// 是否达到发射时间
if(this.bulletTimer >= this.shootDuration)
{
// 计时器归零
this.bulletTimer = 0;
// 实例化一个预制体对象
var bullet = cc.instantiate(this.bulletPrefab);
// 添加到画布上
this.node.addChild(bullet);
// 获取英雄的位置设置给子弹
var pos = cc.v2(this.player.x + this.player.width/2,this.player.y);
bullet.setPosition(pos);
}
},
//自动生成怪兽
produceMonster(dt){
this.monsterTimer+=dt;
if(this.monsterTimer>=this.monsterDuration){
//计时器归零
this.monsterTimer=0;
//实例化一个怪兽飞机对象
var monster=cc.instantiate(this.monsterPrefab);
//添加到canvas下,因为此脚本game.js是挂在canvas下的,所以this.node代表canvas的节点
this.node.addChild(monster);
//设置怪兽飞机的初始位置
var xx=427;
var yy=(Math.random()-0.5)*480;
monster.setPosition(cc.v2(xx,yy));
}
},
// 帧调度器。dt:两帧之间的时间间隔 speed s=vt
update (dt) {
this.bg.x = this.bg.x - this.bgMoveSpeed*dt;
this.bg2.x = this.bg2.x - this.bgMoveSpeed*dt;
if(this.bg.x <= -this.bg.width)
{
this.bg.x = this.bg2.x + this.bg2.width;
}
if(this.bg2.x <= -this.bg2.width)
{
this.bg2.x = this.bg.x + this.bg.width;
}
// 发射子弹
this.shoot(dt);
//自动生成怪兽飞机
this.produceMonster(dt);
},
});