Skip to content
TheChatArchiveThe Chat Archive
gpt-4-0314· OpenAIAi2 WildChat-4.8M (AI2)

使用CcocosCreator3.0编写一段控制人物移动的摇杆代码

  1. User

    使用CcocosCreator3.0编写一段控制人物移动的摇杆代码

  2. Assistant

    在 Cocos Creator 3.0 中,我们可以通过定义事件来监听背景节点的触摸事件,以此实现人物移动的摇杆功能。首先需要创建一个新的脚本(例如:JoyStick.ts),然后按照以下步骤实现摇杆代码:

    1. 引入所需的引擎模块
    import {
      _decorator,
      Component,
      Node,
      Vec3,
      Touch,
      SystemEventType
    } from "cc";
    const { ccclass, property } = _decorator;
    
    1. 声明摇杆类并继承Component
    @ccclass("JoyStick")
    export class JoyStick extends Component {
      //...接下来的代码
    }
    
    1. 定义摇杆属性(例如,中心点,最大范围,操作杆以及操作杆移动的回调)
    @property(Node)
    centerPoint: Node | null = null;
    
    @property(Node)
    joystick: Node | null = null;
    
    @property
    maxRange: number = 50;
    
    private _callback = null;
    
    1. 定义摇杆初始化方法,传入回调函数
    init(callback: Function) {
      if (this.joystick && this.centerPoint) {
        this.node.on(SystemEventType.TOUCH_START, this._touchStart, this);
        this.node.on(SystemEventType.TOUCH_MOVE, this._touchMove, this);
        this.node.on(SystemEventType.TOUCH_END, this._touchEnd, this);
      }
    
      this._callback = callback;
    }
    
    1. 实现触摸事件处理方法
    _touchStart(touch: Touch) {
      let touchPos = touch.getUILocation();
      this.centerPoint && this.centerPoint.setPosition(touchPos);
      this.joystick && this.joystick.setPosition(touchPos);
    }
    
    _touchMove(touch: Touch) {
      if (!this.centerPoint || !this.joystick) {
        return;
      }
      let touchPos = touch.getUILocation();
      let diff = touchPos.subtract(this.centerPoint.position);
      let clampedMag = Math.min(this.maxRange, diff.magnitude());
      let clampedPos = diff.multiplyScalar(clampedMag / diff.magnitude());
      this.joystick.setPosition(this.centerPoint.position.add(clampedPos));
    
      if (this._callback) {
        let direction = diff.normalize().getClosestAxis();
        this._callback(direction);
      }
    }
    
    _touchEnd() {
      if (this.centerPoint && this.joystick) {
        this.centerPoint.setSiblingIndex(-1);
        this.joystick.setSiblingIndex(-1);
      }
    
      if (this._callback) {
        this._callback(new Vec3());
      }
    }
    
    1. 添加摇杆组件

    在场景中添加一个新节点(例如,JoyStickNode)。将创建的脚本(JoyStick.ts)挂载到该节点上,并将摇杆、中心点节点分别拖动到相应属性面板中。

    1. 在你的角色脚本中使用摇杆

    首先,需要给角色添加脚本并在脚本中提供移动方法。以下是一个简单的移动方法的例子:

    moveCharacter(direction: Vec3) {
      if (direction.equals(Vec3.ZERO)) {
        // 停止角色移动(例如,停止播放移动动画等)
      } else {
        let moveSpeed = 5;
        let moveVector = direction.multiplyScalar(moveSpeed);
        let newPosition = this.node.position.add(moveVector);
        this.node.setPosition(newPosition);
    
        // 更新角色移动(例如,设置移动方向和播放移动动画等)
      }
    }
    

    然后,在onLoad方法中(或其他适当的地方)调用摇杆的init方法并传递移动函数:

    const joyStickNode = this.node.scene!.getChildByName("JoyStickNode");
    const joyStick = joyStickNode.getComponent(JoyStick);
    if (joyStick) {
      joyStick.init(this.moveCharacter.bind(this));
    }
    

    此时,摇杆在触摸屏上移动时,角色会响应移动。