1000字范文,内容丰富有趣,学习的好帮手!
1000字范文 > Cocos Creator-5.物理与碰撞系统

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

时间:2019-09-17 13:52:50

相关推荐

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

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

Cocos Creator系列文章目录[Cocos Creator--1.介绍](/joyyi9/article/details/121882189)[Cocos Creator-2.UI系统](/joyyi9/article/details/121907986)[Cocos Creator-3.缓冲系统,动作系统,计时器](/joyyi9/article/details/121942040)[Cocos Creator-4.监听,发射事件,节点系统事件,全局系统事件](/joyyi9/article/details/121965742)[Cocos Creator-5.物理与碰撞系统](/joyyi9/article/details/121987382)[Cocos Creator-6.TS-属性检查器](/joyyi9/article/details/122031963)一、介绍二、碰撞系统三.物理系统四.重要点记录1.预制体生成2.更换刚体类型3.预制体回调销毁(bullet).sys.localStorage5.通过按钮进行Toggle6.像进度条一样的图片7.有关于粒子播放总结

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;@ccclassexport default class NewClass extends ponent {@propertyspeed: number = 100;onCollisionEnter(other,self){//当碰撞产生的时候调用//other 产生碰撞的另一个碰撞组件self 产生碰撞的自身的碰撞组件this.node.destroy();other.color=cc.Color.RED;}update (dt) {this.node.y+=this.speed*dt;}}

.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 playedmyParticle.stopSystem(); // stop particle system} else {myParticle.resetSystem(); // restart particle system}


总结

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

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。