记录一下使用物理系统时踩过的坑!
one:修改type属性以及修改修改重力值
想实现点击之后改变type属性,将物体由static静止状态改编成dynamic运动状态
就不展示我那蹩脚的代码写法了
正确的写法:
onClickCollierStartBtn(){
this.cake.getComponent(cc.RigidBody).type = RigidBodyType.Dynamic;
this.cake.getComponent(PhysicsBoxCollider).apply();
this.purple.getComponent(cc.RigidBody).type = RigidBodyType.Dynamic;
this.purple.getComponent(PhysicsBoxCollider).apply();
this.blue.getComponent(cc.RigidBody).type = RigidBodyType.Dynamic;
this.blue.getComponent(PhysicsBoxCollider).apply();
this.red.getComponent(cc.RigidBody).type = RigidBodyType.Dynamic;
this.red.getComponent(PhysicsBoxCollider).apply();
}
首先,修改的方式要注意,直接在type后等于,而不是let一个变量
其次,一定要apply()应用!!!!!!我就是在没有应用的时候怎么修改都没作用中挣扎了近两小时。。。
重力值的修改同理:
change_physics_property(){
this.stock1.getComponent(cc.RigidBody).gravityScale = 8;
this.stock1.getComponent(cc.RigidBody).linearVelocity = cc.v2(20,20);
this.stock1.getComponent(cc.PhysicsCircleCollider).apply();
}
首先,别看gravity Scale数值是个数字,但是它依然是通过直接传值
let gravity = this.stock1.getComponent(cc.RigidBody).gravityScale;
gravity = 8;
这样写是会被屏蔽掉的,数值完全传不过来!
其次,注意!!!!!!!!!想让它动一定要给个力,不然修改了也不会动。。。。。
two:修改重力值后并没有达到我想要的点击之后添加重力实现掉落的效果
如上句话所说
更新一下,最近写刚体写的有点多,天天getComponent也get烦了,想着写了个公共脚本
之前是将this.xxxRigid = this.xxx.getComponent(cc.RigidBody);了,然后直接调用this.xxxRigid,但是后来还是写的很烦
不多废话直接上代码
以前每次改状态之类的都要写好几排的现在一排就搞定了,可能还有更好的封装办法,但是这个是我目前能想出来最好的了,并且用着很顺手
以此类推,我把刚体的各种参数都封了下
等等
以上