laya 摇杆的简单实现

12 篇文章 2 订阅
3 篇文章 0 订阅

在竖版rpg游戏中常常需要摇杆来控制角色移动。我们这里给出在laya引擎中一个摇杆的简单实现。用户只需要在UI界面简单的设置一个摇杆的触摸区、摇杆bg和摇杆柄即可。如下图所示

 

import Script = Laya.Script;
import Image = Laya.Image;
import Event = Laya.Event;
import Sprite = Laya.Sprite;
import Point = Laya.Point;

/** 摇杆半径 */
export const JoystickRadius: number = 64;

export default class Joystick extends Script {
    /** 可触摸区域节点 */
    private _touchAreaSpr: Sprite;
    /** 摇杆背景 */
    private _joystickBg: Image;
    /** 摇杆的柄 */
    private _joystickHandler: Image;

    /** 是否按下摇杆的柄 */
    private _isPressHandler: boolean = false;

    /** 摇杆的中心点 */
    private _centerPoint: Point = new Point();
    /** 摇杆柄的局部位置 */
    private _localPoint: Point = new Point();
    /** 摇杆柄的局部位置相对于stage在水平方向上的增量 */
    private _deltaX: number = 0;
    private _deltaY: number = 0;

    // /** 摇杆角度 0 - 360*/
    // public static JoyAngle: number = -1;
    /** 摇杆方向, 包含尺度(0 - 1) */
    public static JoyDir: Point = new Point();

    onAwake(): void {
        this._touchAreaSpr = this.owner.parent as Sprite;
        this._joystickBg = this.owner as Image;
        this._joystickHandler = this.owner.getChildAt(0) as Image;
    }

    onEnable(): void {
        // this._joystickHandler.on(Event.MOUSE_DOWN, this, this.onMouseDownHandler);

        this._touchAreaSpr.on(Event.MOUSE_DOWN, this, this.onBgMouseDown);
        this._touchAreaSpr.on(Event.MOUSE_MOVE, this, this.onBgMouseMove);
        this._touchAreaSpr.on(Event.MOUSE_UP, this, this.onBgMouseUp);
        this._touchAreaSpr.on(Event.MOUSE_OUT, this, this.onBgMouseUp);

        this._centerPoint.setTo(this._joystickBg.pivotX, this._joystickBg.pivotY);
    }

    onDisable(): void {
        this._joystickHandler.offAll();
        this._touchAreaSpr.offAll();
    }

    // /** 鼠标按下摇杆柄部 */
    // private onMouseDownHandler() {
    //     this._isPressHandler = true;
    // }

    private onBgMouseDown(e: Event): void {
        if(this._isPressHandler) return;

        this._isPressHandler = true;
        this._joystickBg.pos(e.stageX, e.stageY - this._touchAreaSpr.y); // 让摇杆追随玩家手指
        this._deltaX = this._touchAreaSpr.x + this._joystickBg.x - this._joystickBg.pivotX;
        this._deltaY = this._touchAreaSpr.y + this._joystickBg.y - this._joystickBg.pivotY;
        this._joystickBg.set_visible(true);
    }

    private onBgMouseMove(e: Event): void {
        if (!this._isPressHandler) return;

        /** 移动摇杆柄 */
        this._localPoint.x = e.stageX - this._deltaX;
        this._localPoint.y = e.stageY - this._deltaY;

        if (this.distance(this._localPoint, this._centerPoint) > JoystickRadius) { // 超过摇杆半径时处理
            this._localPoint.setTo(this._localPoint.x - this._centerPoint.x, this._localPoint.y - this._centerPoint.y);
            this._localPoint.normalize();
            Joystick.JoyDir.copy(this._localPoint);
            this._localPoint.setTo(this._centerPoint.x + this._localPoint.x * JoystickRadius, this._centerPoint.y + this._localPoint.y * JoystickRadius);
        } else {
            Joystick.JoyDir.setTo((this._localPoint.x - this._centerPoint.x) / JoystickRadius, (this._localPoint.y - this._centerPoint.y) / JoystickRadius);
        }

        this._joystickHandler.pos(this._localPoint.x, this._localPoint.y);
    }

    private onBgMouseUp(e: Event): void {
        if (!this._isPressHandler) return;
        this._isPressHandler = false;

        this._joystickBg.pos(this._touchAreaSpr.width / 2, this._touchAreaSpr.height / 2);
        this._joystickHandler.pos(this._centerPoint.x, this._centerPoint.y);
        Joystick.JoyDir.reset();
        this._joystickBg.set_visible(false);
    }

    private distance(p1:Point, p2:Point): number {
        return Math.sqrt((p1.x - p2.x) * (p1.x - p2.x) + (p1.y - p2.y) * (p1.y - p2.y));
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值