core-egret.Matrix

egret.Matrix 使用範例


class Main extends eui.UILayer {

    protected createChildren(): void {
        super.createChildren();
        egret.lifecycle.onPause = () => egret.ticker.pause();
        egret.lifecycle.onResume = () => egret.ticker.resume();
        egret.registerImplementation('eui.IAssetAdapter', new AssetAdapter());
        egret.registerImplementation('eui.IThemeAdapter', new ThemeAdapter());
        this.runGame();
    }

    private async loadResource() {
        let loadingView = this.stage.addChild(new LoadingUI()) as LoadingUI;
        await RES.loadConfig('resource/default.res.json', 'resource/');
        await new Promise(resolve => new eui.Theme('resource/default.thm.json', this.stage).once(eui.UIEvent.COMPLETE, resolve, this));
        await RES.loadGroup('preload', 0, loadingView);
        this.stage.removeChild(loadingView);
    }

    private async runGame() {
        await this.loadResource();
        // --- Edwin 給你最乾淨的程式碼, 從這行以下開始寫喔 ---

        // 建立 image 物件
        const image1 = new eui.Image('bg_jpg');
        // 顯示於場景
        this.addChild(image1);
        // 建立 Matrix 物件
        const matrix = new egret.Matrix;
        // 平移(x = -image1.width / 2, y = -image1.height / 2)
        matrix.translate(-image1.width / 2, -image1.height / 2);
        // 讓陣圖旋轉 45 度(要轉換成徑度)
        matrix.rotate(45 / 180 * Math.PI);
        image1.matrix = matrix;
        console.log(image1.rotation); // 45, matrix 會直接改變 x, y, rotation, scale 等參數
        console.log(image1.x, image1.y); // 175.36248173426372 -627.9108216936542

        // 補充, 若有 image2 物件, image2.matrix = image1.matrix 可以讓 image2 位移, 旋轉, 縮放狀態完全拷貝於 image1

    }

}

spacer

沒有留言:

張貼留言