赞
踩
move(dt, direction: Direction) {
switch (direction) {
case Direction.default:
break;
case Direction.top:
this.node.y += this.speed.y * dt;
break;
case Direction.left:
this.node.x -= this.speed.x * dt;
break;
case Direction.right:
this.node.x += this.speed.x * dt;
break;
case Direction.down:
this.node.y -= this.speed.y * dt;
break;
}
}
1.开启物理引擎
cc.director.getPhysicsManager().enabled = true;
2.给节点添加刚体
静态(面板添加):
动态(代码添加): this.node.addComponent(cc.RigidBody);
/** * * @param dt * @param direction 移动的方向 */ move(dt, direction: Direction) { const rigidBody = this.node.getComponent(cc.RigidBody); switch (direction) { case Direction.default: rigidBody.linearVelocity = cc.v2(0, 0); break; case Direction.top: rigidBody.linearVelocity = cc.v2(0,this.speed); break; case Direction.left: rigidBody.linearVelocity = cc.v2(-this.speed,0); break; case Direction.right: rigidBody.linearVelocity = cc.v2(this.speed,0); break; case Direction.down: rigidBody.linearVelocity = cc.v2(0,-this.speed); break; } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。