Cocos Creator-5.物理与碰撞系统

Cocos Creator系列文章目录

Cocos Creator–1.介绍

Cocos Creator-2.UI系统

Cocos Creator-3.缓冲系统,动作系统,计时器

Cocos Creator-4.监听,发射事件,节点系统事件,全局系统事件

Cocos Creator-5.物理与碰撞系统

Cocos Creator-6.TS-属性检查器


# 前言

一切从简

一、介绍

物理与碰撞系统默认是关闭的,首先需要做的事情就是开启物理与碰撞系统,否则你在编辑器里做的所有物理编辑都不会产生任何效果。

 cc.director.getPhysicsManager().enabled=true;
 cc.director.getCollisionManager().enabled = true;
 cc.director.getCollisionManager().enabledDebugDraw = true;
 //默认碰撞检测系统的 debug 绘制是禁用的,

二、碰撞系统

回调函数

/**
 * 当碰撞产生的时候调用
 * @param  {Collider} other 产生碰撞的另一个碰撞组件
 * @param  {Collider} self  产生碰撞的自身的碰撞组件
 */
onCollisionEnter(other, self){}
onCollisionStay(other, self){}
onCollisionExit(other, self){}

三.物理系统

回调函数


 // 只在两个碰撞体开始接触时被调用一次
    onBeginContact: function (contact, selfCollider, otherCollider) {
    },

    // 只在两个碰撞体结束接触时被调用一次
    onEndContact: function (contact, selfCollider, otherCollider) {
    },

    // 每次将要处理碰撞体接触逻辑时被调用
    onPreSolve: function (contact, selfCollider, otherCollider) {
    },

    // 每次处理完碰撞体接触逻辑时被调用
    onPostSolve: function (contact, selfCollider, otherCollider) {
    }

四.重要点记录

1.预制体生成

 var canvas = cc.find('Canvas');
 canvas.on(cc.Node.EventType.TOUCH_START, this.onTouchBegan, this);

onTouchBegan(event: any) {
     var scene = cc.director.getScene();
     var touchLoc = event.touch.getLocation();//获取鼠标点击坐标
     if (this.bullet == null) return;//解决方案
     var bullet = cc.instantiate(this.bullet);//(prefab,node都可以!!)
     bullet.position = touchLoc;
     bullet.active = true;
     scene.addChild(bullet);
     // bullet.parent = cc.director.getScene();//这种也可以将预制体加到画布上
 }

2.更换刚体类型

this.node.getComponent(cc.RigidBody).type =cc.RigidBodyType.Static;
console.log( this.node.getComponent(cc.RigidBody).type);
this.node.getComponent(cc.PhysicsBoxCollider).apply();
    

3.预制体回调销毁(bullet)

const {ccclass, property} = cc._decorator;
@ccclass
export default class NewClass extends cc.Component {
    @property
    speed: number = 100;
    onCollisionEnter(other,self){//当碰撞产生的时候调用//other 产生碰撞的另一个碰撞组件self  产生碰撞的自身的碰撞组件
        this.node.destroy();
        other.color=cc.Color.RED;
    }

    update (dt) {
        this.node.y+=this.speed*dt;
    }
}   

4.cc.sys.localStorage

//this.history_s这是一个标签
 if (cc.sys.localStorage.getItem(this.history_s) == null) {//如果历史记录为null就初始化历史记录
     cc.sys.localStorage.setItem(this.history_s, 0);//初始化 

5.通过按钮进行Toggle

通过三位运算符更加好!!!

@property(cc.Node)
    cocos: cc.Node = null;
    changcolor: boolean=false;

    changColor(){
        this.cocos.color=this.changcolor?cc.Color.WHITE:cc.Color.BLUE;
        this.changcolor=!this.changcolor;
    }

6.像进度条一样的图片

这里在update里面没写的函数十分值得学习

update(dt){
        this.changeWdith(this.cocos,1400,dt);

    }
changeWdith(node,range,dt){
    let width=node.width;
    width=width<range?width+=this.speed*dt:0;
    node.width=width;
}

7.有关于粒子播放

var myParticle = this.particle;
if (myParticle.particleCount > 0) { // check if particle has fully played
      myParticle.stopSystem(); // stop particle system
  } else {
      myParticle.resetSystem(); // restart particle system
  }

总结

其实特别特别重要,在我们做游戏过程中,不可缺少的!!

  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

joyyi9

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

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

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

打赏作者

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

抵扣说明:

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

余额充值