eui-eui.CollectionEvent

eui.CollectionEvent 使用範例


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 ac = new eui.ArrayCollection();
        ac.source = [1, 2, 3]; // 設置資料源
        ac.addEventListener(eui.CollectionEvent.COLLECTION_CHANGE, this.onCollectionChange, this);
        ac.replaceItemAt(7,1); // 索引 1 用 7 取代
    }

    private onCollectionChange(e: eui.CollectionEvent): void {
        console.log(e.currentTarget.source); // [1, 7, 3], 更換後的陣列
        console.log(e.items); // [7], 更換項目
        console.log(e.kind); // replace, 事件種類
        console.log(e.location); // 1, 目前索引
        console.log(e.oldItems); // [2], 舊 items
        console.log(e.oldLocation); // -1, 7 原本不存在所以索引找不到
    }

}

spacer

沒有留言:

張貼留言