layabox中的遥感

 

class YaoGan extends Laya.View

{

    constructor () { super(); this.initEven() }

    private lastX: number = 0; private lastY: number = 0;

    private dis_w: number = 100;/**摇杆可以移动的最大距离 */

    private initEven ()

    {

        this.YaoGanVis( false );

        this.scene.box_click.on( Laya.Event.MOUSE_DOWN, this, this.onMouseDow );

    }

    private onMouseDow ( e: Laya.Event )

    {

        this.lastX = Laya.stage.mouseX;

        this.lastY = Laya.stage.mouseY;

        this.YaoGanVis( true );

        this.scene.box_click.on( Laya.Event.MOUSE_MOVE, this, this.onTouchMove );

        this.scene.box_click.once( Laya.Event.MOUSE_UP, this, this.onMouseUp );

        this.scene.box_click.once( Laya.Event.MOUSE_OUT, this, this.onMouseUp );

    }

    private onMouseUp ( e: Laya.Event )

    {

        this.scene.box_click.off( Laya.Event.MOUSE_MOVE, this, this.onTouchMove );

        this.scene.box_click.off( Laya.Event.MOUSE_UP, this, this.onMouseUp );

        this.scene.box_click.off( Laya.Event.MOUSE_OUT, this, this.onMouseUp );

    }

    onTouchMove ( e: Laya.Event )

    {

        let mouseX: number = Laya.stage.mouseX;

        let mouseY: number = Laya.stage.mouseY;

        let mX = mouseX - this.lastX;

        let mY = mouseY - this.lastY;

        let dis_w = Math.sqrt( mX * mX + mY * mY );

        /**方法一 */

        if ( dis_w > this.dis_w )

        {

            let dir = new Laya.Point( mouseX - this.lastX, mouseY - this.lastY );

            dir.normalize();

            this.scene.img_stick.x = this.lastX + dir.x * this.dis_w;

            this.scene.img_stick.y = this.lastY + dir.y * this.dis_w;

        } else

        {

            this.scene.img_stick.x = mouseX;

            this.scene.img_stick.y = mouseY;

        }

        /**方法二待验证 */

        if ( dis_w > this.dis_w )

        {

            let dir = new Laya.Point( mouseX - this.lastX, mouseY - this.lastY );

            let angle = Math.atan2( dir.y, dir.x );

            this.scene.img_stick.x = this.lastX + Math.cos( angle ) * this.dis_w;

            this.scene.img_stick.y = this.lastY + Math.sin( angle ) * this.dis_w;

        } else

        {

            this.scene.img_stick.x = mouseX;

            this.scene.img_stick.y = mouseY;

        }

    }

    /**设置摇杆的显示状态 */

    private YaoGanVis ( bool: boolean )

    {

        this.scene.img_shank.visible = bool;

        this.scene.img_stick.visible = bool;

    }

}

  • 9
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值