当前位置:   article > 正文

cocos creator 之角色移动_cocoscreator来回移动

cocoscreator来回移动

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

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

}
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小丑西瓜9/article/detail/105115
推荐阅读
相关标签
  

闽ICP备14008679号