使用坐标系改变坐标的移动(简易)

   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;
    }

}
Logo

北京人形旗下天工造物具身智能开源社区,聚焦具身天工与慧思开物两大平台

更多推荐