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