Egret 2D(1)之项目配置和资源加载

项目配置

比如你创建了一个EUI项目,我想加入game类。

  • 在项目egretProperties.jsonmodules中添加{ "name": "egret" }后保存。
  • 保存后Wing左上角点编译,编译成功后。当然也可以使用命令行egret build -e  。编译后按F5编译运行

资源加载 



class Main extends egret.DisplayObjectContainer {

    /**
     * 加载进度界面
     * Process interface loading
     */
    private loadingView: LoadingUI;

    public constructor() {
        super();
        this.addEventListener(egret.Event.ADDED_TO_STAGE, this.onAddToStage, this);
    }

    private onAddToStage(event: egret.Event) {

        egret.lifecycle.addLifecycleListener((context) => {
            // custom lifecycle plugin

            context.onUpdate = () => {
                console.log('hello,world')
            }
        })

        egret.lifecycle.onPause = () => {
            egret.ticker.pause();
        }

        egret.lifecycle.onResume = () => {
            egret.ticker.resume();
        }


        //设置加载进度界面
        //Config to load process interface
        this.loadingView = new LoadingUI();
        this.stage.addChild(this.loadingView);

        //初始化Resource资源加载库
        //initiate Resource loading library
        RES.addEventListener(RES.ResourceEvent.CONFIG_COMPLETE, this.onConfigComplete, this);
        RES.loadConfig("resource/default.res.json", "resource/");
    }

    /**
     * 配置文件加载完成,开始预加载preload资源组。
     * configuration file loading is completed, start to pre-load the preload resource group
     */
    private onConfigComplete(event: RES.ResourceEvent): void {
        RES.removeEventListener(RES.ResourceEvent.CONFIG_COMPLETE, this.onConfigComplete, this);
        RES.addEventListener(RES.ResourceEvent.GROUP_COMPLETE, this.onResourceLoadComplete, this);
        RES.addEventListener(RES.ResourceEvent.GROUP_LOAD_ERROR, this.onResourceLoadError, this);
        RES.addEventListener(RES.ResourceEvent.GROUP_PROGRESS, this.onResourceProgress, this);
        RES.addEventListener(RES.ResourceEvent.ITEM_LOAD_ERROR, this.onItemLoadError, this);
        //加载preload组,在项目根目录resource下default.res.json中可以看到
        RES.loadGroup("preload");
    }

    /**
     * preload资源组加载完成
     * Preload resource group is loaded
     */
    private onResourceLoadComplete(event: RES.ResourceEvent) {
        if (event.groupName == "preload") {
            this.stage.removeChild(this.loadingView);
            RES.removeEventListener(RES.ResourceEvent.GROUP_COMPLETE, this.onResourceLoadComplete, this);
            RES.removeEventListener(RES.ResourceEvent.GROUP_LOAD_ERROR, this.onResourceLoadError, this);
            RES.removeEventListener(RES.ResourceEvent.GROUP_PROGRESS, this.onResourceProgress, this);
            RES.removeEventListener(RES.ResourceEvent.ITEM_LOAD_ERROR, this.onItemLoadError, this);
            this.createGameScene();
        }
    }

    /**
     * 资源组加载出错
     *  The resource group loading failed
     */
    private onItemLoadError(event: RES.ResourceEvent) {
        console.warn("Url:" + event.resItem.url + " has failed to load");
    }

    /**
     * 资源组加载出错
     *  The resource group loading failed
     */
    private onResourceLoadError(event: RES.ResourceEvent) {
        //TODO
        console.warn("Group:" + event.groupName + " has failed to load");
        //忽略加载失败的项目
        //Ignore the loading failed projects
        this.onResourceLoadComplete(event);
    }

    /**
     * preload资源组加载进度
     * Loading process of preload resource group
     */
    private onResourceProgress(event: RES.ResourceEvent) {
        if (event.groupName == "preload") {
            this.loadingView.setProgress(event.itemsLoaded, event.itemsTotal);
        }
    }

    private textfield: egret.TextField;

    /**
     * 创建游戏场景
     * Create a game scene
     */
    private createGameScene() {
            //加载位图
            var batman:egret.Bitmap =this.LoadBg('bg_jpg');
            batman.width=100;
            batman.height=100;
            this.stage.addChild( batman );
            
            batman.$touchEnabled=true;//能够响应手势
            //手势按到屏幕
            batman.addEventListener(egret.TouchEvent.TOUCH_BEGIN,touchBegin,this);
            function touchBegin(e:egret.TouchEvent):void{
              console.log("手势按到屏幕")
            }
            //手指移动
            this.stage.addEventListener(egret.TouchEvent.TOUCH_MOVE,touchMpove,this);
             function touchMpove(e:egret.TouchEvent):void{
                batman.x=e.stageX- batman.width/2;
                batman.y=e.stageY - batman.height/2;
            }
            //手指离开
            batman.addEventListener(egret.TouchEvent.TOUCH_MOVE,touchEnd,this);
             function touchEnd(e:egret.TouchEvent):void{
              console.log("手指离开")
            }  
            let rect=new RectAngle();
            this.addChild(rect);
            var label:egret.TextField=new egret.TextField();
            label.type=egret.TextFieldType.INPUT;
            label.text="hello world";
            label.x=300;
            label.y=100;
            label.textColor=0x000000;
            
            this.addChild(label);
            
            

    }
    private LoadBg(name:String){
      return   new egret.Bitmap( RES.getRes(`${name}`) )
    }


}

 

猜你喜欢

转载自my.oschina.net/u/3112095/blog/1785373