core-egret.GradientType

egret.GradientType 使用範例


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 給你最乾淨的程式碼, 從這行以下開始寫喔 ---

        // 顏色比例分部陣列
        const ratiosArray = [[255, 0], [230, 0], [205, 0], [180, 0], [155, 0], [0, 155], [0, 180], [0, 205], [0, 230], [0, 255]];
        // 建立 shape 並給予名字
        for (let i = 0; i < ratiosArray.length; i++) this.addChild(new egret.Shape).name = `s${i}`;
        // 遍歷陣列
        ratiosArray.forEach((item, index) => {
            const s = this.getChildByName(`s${index}`) as egret.Shape;
            // egret.GradientType.LINEAR 可以改成 egret.GradientType.RADIAL 試試
            s.graphics.beginGradientFill(egret.GradientType.LINEAR, [0xFF0000, 0x0000FF], [1, 1], item);// ratio 參數測試
            s.graphics.drawRect(0, 0, 100, 100);
            s.graphics.endFill();
            s.y = index * 100;
        });

    }

}

spacer

沒有留言:

張貼留言