egret.RenderTexture 使用範例
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 給你最乾淨的程式碼, 從這行以下開始寫喔 ---
// 建立繪圖物件 s1, s2 給 RenderTexture 繪製
const s1 = new egret.Shape;
s1.graphics.beginFill(0);
s1.graphics.drawCircle(50, 50, 50);
s1.graphics.endFill();
const s2 = new egret.Shape;
s2.graphics.beginFill(0xFFCC00);
s2.graphics.drawCircle(0, 0, 50);
s2.graphics.endFill();
const texture = new egret.RenderTexture;
// 繪製
texture.drawToTexture(s1, new egret.Rectangle(0, 0, 100, 100));
texture.drawToTexture(s2, new egret.Rectangle(0, 0, 100, 100));
// 第二次 drawToTexture 會重新繪製材質, 所以第一次 drawToTexture 繪圖 s1 會不見
// 如果要同時畫 s1, s2, 可以把他們包在 egret.DisplayObjectContainer 命名 doc
// const doc = new egret.DisplayObjectContainer();
// doc.addChild(s1);
// doc.addChild(s2);
// texture.drawToTexture(doc, new egret.Rectangle(0, 0, 100, 100));
this.addChild(new eui.Image(texture));
}
}
沒有留言:
張貼留言