Egretシロの入門編を学ぶ

6626 ワード

公式シロ入門文書による一歩一歩の抄録テスト


Main.tsファイル
class Main extends eui.UILayer {
    private webSocket:egret.WebSocket;
    private craateGameScene():void{
        this.webSocket = new egret.WebSocket();
        this.webSocket.addEventListener(egret.ProgressEvent.SOCKET_DATA, this.onReceiveMessage, this);
        this.webSocket.addEventListener(egret.Event.CONNECT, this.onSocketOpen,this);
        this.webSocket.connect("echo.websocket.org",80);

    }
    private onSocketOpen():void {    
    var cmd = "Hello Egret WebSocket";    
    console.log("The connection is successful, send data: " + cmd);    
    this.webSocket.writeUTF(cmd); 
}
private onReceiveMessage(e:egret.Event):void {    
    var msg = this.webSocket.readUTF();    
    console.log("Receive data:" + msg); 
}
    private times:number;    
    // // touch 
    // private touchHandler( evt:egret.TouchEvent ):void{
    //     var tx:egret.TextField = evt.currentTarget;
    //     tx.textColor = 0x00ff00;
    //     tx.size = 60;
    // }


    protected createChildren(): void {
        super.createChildren();

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

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

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

        //inject the custom material parser
        // 
        let assetAdapter = new AssetAdapter();
        egret.registerImplementation("eui.IAssetAdapter", assetAdapter);
        egret.registerImplementation("eui.IThemeAdapter", new ThemeAdapter());


        this.runGame().catch(e => {
            console.log(e);
        })
    }

    private async runGame() {
        await this.loadResource()
        this.createGameScene();
        const result = await RES.getResAsync("description_json")
        //this.startAnimation(result);
        await platform.login();
        const userInfo = await platform.getUserInfo();
        console.log(userInfo);

    }

    private async loadResource() {
        try {
            const loadingView = new LoadingUI();
            this.stage.addChild(loadingView);
            await RES.loadConfig("resource/default.res.json", "resource/");
            await this.loadTheme();
            await RES.loadGroup("heroes", 0, loadingView);
            this.stage.removeChild(loadingView);
        }
        catch (e) {
            console.error(e);
        }
    }

    private loadTheme() {
        return new Promise((resolve, reject) => {
            // load skin theme configuration file, you can manually modify the file. And replace the default skin.
            // , 。 。
            let theme = new eui.Theme("resource/default.thm.json", this.stage);
            theme.addEventListener(eui.UIEvent.COMPLETE, () => {
                resolve();
            }, this);

        })
    }

    private textfield: egret.TextField;
    /**
     *  
     * Create scene interface
     */
// ---------------------------------------------- ---------------------------------------------------------
    protected createGameScene(): void {
        // egret.Shape() 
        var bg:egret.Shape = new egret.Shape();
        bg.graphics.beginFill(0x0000ff);
        bg.graphics.drawRect(0,0,this.stage.stageWidth,this.stage.stageHeight);
        bg.graphics.endFill();
        this.addChild(bg);
        
        
        // var tx:egret.TextField = new egret.TextField();
        // tx.text = "Hellow World I'm shenyubin";
        // tx.size = 30;
        // tx.x = 50;
        // tx.y = 50;
        // tx.width = this.stage.stageWidth - 100;
        // tx.touchEnabled = true;
        // // tx.addEventListener(egret.TouchEvent.TOUCH_TAP,this.touchHandler,this);
        // tx.addEventListener(egret.TouchEvent.TOUCH_TAP,function(evt:egret.TouchEvent):void{
        //     tx.textColor = 0x00ff00;
        // },this);
        // this.addChild(tx);

        // console.log("createGameScene",RES.getRes("hero-01"));
        var superman:egret.Bitmap = new egret.Bitmap(RES.getRes("hero-01_jpg"));
        superman.x = 20;
        superman.y = 20;
        this.addChild(superman);
        var batman:egret.Bitmap = new egret.Bitmap(RES.getRes("hero-03_jpg"));
        batman.x = 120;
        batman.y = 20;
        this.addChild(batman);
        var hulk:egret.Bitmap = new egret.Bitmap(RES.getRes("hero-02_jpg"));
        hulk.x = 220;
        hulk.y = 20;
        this.addChild(hulk);
        var captain:egret.Bitmap = new egret.Bitmap(RES.getRes("hero-04_jpg"));
        captain.x = 320;
        captain.y = 20;
        this.addChild(captain);
        this.setChildIndex( batman,this.getChildIndex(hulk));
        //console.log( "display indexes:", this.getChildIndex( bg ), this.getChildIndex( superman ),  this.getChildIndex( batman ), this.getChildIndex( hulk ), this.getChildIndex( captain ) );

        hulk.anchorOffsetX = 30;
        hulk.anchorOffsetY = 40;
        hulk.x += 30;
        hulk.y += 40;

        this.times = -1;
        var self = this;
        this.stage.addEventListener(egret.TouchEvent.TOUCH_TAP,function(){
            switch( ++ self.times % 3){
                case 0:
                    //batman and superman  
                    egret.Tween.get(batman).to({x:superman.x},300,egret.Ease.circIn);
                    egret.Tween.get(superman).to({x:batman.x},300,egret.Ease.circIn); 
                    break;
                case 1: 
                    //superman 
                    egret.Tween.get(captain).to({alpha:.4},300,egret.Ease.circIn).to({alpha:1},300,egret.Ease.circIn);
                    break;
                case 2: 
                    //hulk 
                    egret.Tween.get(hulk).to({scaleX:.4,scaleY:.4},600,egret.Ease.circIn).to({scaleX:1,scaleY:1},600,egret.Ease.circIn);
                    break;
            }
        },this);
        
        // 
        var sound:egret.Sound = RES.getRes("bonus_mp3");
        var channel:egret.SoundChannel = sound.play(0,1);
        
        // 
        var urlreq:egret.URLRequest = new egret.URLRequest("http://httpbin.org/user-agent");
        var urlloader:egret.URLLoader = new egret.URLLoader();
        urlloader.addEventListener(egret.Event.COMPLETE,function(evt:egret.Event):void{
            console.log(evt.target.data);
        },this);
        urlloader.load(urlreq);


    }
    
}