import { Laya } from "Laya" ; import { Stage } from "laya/display/Stage" ; import { Event } from "laya/events/Event" ; import { TiledMap } from "laya/map/TiledMap" ; import { Rectangle } from "laya/maths/Rectangle" ; import { Resource } from "laya/resource/Resource" ; import { Browser } from "laya/utils/Browser" ; import { Handler } from "laya/utils/Handler" ; import { Stat } from "laya/utils/Stat" ; import { WebGL } from "laya/webgl/WebGL" ; import { Main } from "./../Main" ; export class TiledMap_ScrollMap { private tiledMap : TiledMap ; private mLastMouseX : number = 0 ; private mLastMouseY : number = 0 ; private mX : number = 0 ; private mY : number = 0 ; Main : typeof Main = null ; constructor ( maincls ) { this . Main = maincls ; Laya . init ( Browser . clientWidth , Browser . clientHeight , WebGL ) ; Stat . show ( ) ; this . createMap ( ) ; Laya . stage . on ( Event . MOUSE_DOWN , this , this . mouseDown ) ; Laya . stage . on ( Event . MOUSE_UP , this , this . mouseUp ) ; } private createMap ( ) { this . tiledMap = new TiledMap ( ) ; this . mX = this . mY = 0 ; this . tiledMap . createMap ( "res/tiledMap/desert.json" , new Rectangle ( 0 , 0 , Browser . width , Browser . height ) , new Handler ( this , this . completeHandler ) ) ; } private onLoaded ( ) { this . tiledMap . mapSprite ( ) . removeSelf ( ) ; this . Main . box2D . addChild ( this . tiledMap . mapSprite ( ) ) ; } /**
     * 地图加载完成的回调
     */ private completeHandler ( e = null ) { Laya . stage . on ( Event . RESIZE , this , this . resize ) ; this . resize ( ) ; this . onLoaded ( ) ; } private mouseDown ( e = null ) { this . mLastMouseX = Laya . stage . mouseX ; this . mLastMouseY = Laya . stage . mouseY ; Laya . stage . on ( Event . MOUSE_MOVE , this , this . mouseMove ) ; } private mouseMove ( e = null ) { this . tiledMap . moveViewPort ( this . mX - ( Laya . stage . mouseX - this . mLastMouseX ) , this . mY - ( Laya . stage . mouseY - this . mLastMouseY ) ) ; } private mouseUp ( e = null ) { this . mX = this . mX - ( Laya . stage . mouseX - this . mLastMouseX ) ; this . mY = this . mY - ( Laya . stage . mouseY - this . mLastMouseY ) ; Laya . stage . off ( Event . MOUSE_MOVE , this , this . mouseMove ) ; } private resize ( e = null ) { this . tiledMap . changeViewPort ( this . mX , this . mY , Browser . width , Browser . height ) ; } dispose ( ) { Laya . stage . off ( Event . MOUSE_DOWN , this , this . mouseDown ) ; Laya . stage . off ( Event . MOUSE_UP , this , this . mouseUp ) ; Laya . stage . off ( Event . RESIZE , this , this . resize ) ; Laya . stage . off ( Event . MOUSE_MOVE , this , this . mouseMove ) ; if ( this . tiledMap ) { this . tiledMap . destroy ( ) ; } } }