LoginSignup
0
0

More than 5 years have passed since last update.

EgretEngineでポップアップを出したい

Last updated at Posted at 2018-11-13

デモ

Egret Developer Centerから配信されているこちらのソシャゲ風デモのタブ右端の「关于」ボタンを押したときに出てくる
http://developer.egret.com/cn/article/index/id/833

こちらの画面のようなポップアップを実装したい
image.png

やること

レイアウトの定義ファイルを作成する

  1. resource/custom_skinsディレクトリを作成する
  2. メニューからNew From TemplateNew Exml Componentを選択する
  3. できたファイルのうちGameOverScene.tssrcへ移動、GameOverScene.exmlresource/custom_skinsへ移動

レイアウト定義

GameOverScene.exml
<?xml version="1.0" encoding="utf-8"?>
<e:Skin class="GameOverSceneSkin" width="400" height="300" xmlns:e="http://ns.egret.com/eui" xmlns:w="http://ns.egret.com/wing" >
    <w:Config id="1670ba1839b" ></w:Config>
</e:Skin>

ここのwidthheightを下記のindex.htmlにあわせて

index.html
<body>
    <div style="margin: auto;width: 100%;height: 100%;" class="egret-player"
         :
         data-content-width="640"
         data-content-height="1136"

こちらにする

... <e:Skin class="GameOverSceneSkin" width="640" height="1136" ... 

Previewしてみるとこうなります

image.png

背景を作成する

ポップアップの背面に透明度50%の黒い板を置きます

  • 画面左端のコンポーネント一覧からRectをドラッグ&ドロップして
  • 画面右のProperty Panelから、x/y座標とwidth/heightを設定して、FillのAlphaを0.5にします image.png

あとはポップアップのウインドウの面(Rect)と、ゲームオーバーのタイトルとスコアを表示するLabelを配置して調整します。

image.png

スコアを表示するLabelは、PropertyPanelのIDフィールドに名前をつけてください。
今回はlastScoreLabelとしました。

ポップアップ画面の実装

自動で生成されたGameOverScene.tsクラスを以下のように書き換えます

  1. コンストラクターのthis.skinName = "resource/custom_skins/GameOverScene.exml";で、読み込みたい定義済みのレイアウトを指定します。
  2. レイアウトで定義したコンポーネントをプログラムで使いたい場合は、レイアウト定義ファイル上でIDを指定します。そのIDと同じコンポーネントをクラスのプロパティーとして実装してください。
  3. 今回の場合は、private lastScoreLabel: eui.Label;で定義されたプロパティーに、先ほどのレイアウト定義で、IDをLastScoreLabelに設定したLabelコンポーネントが自動でBindされます。
GameOverScene.ts
class GameOverScene extends eui.Component implements  eui.UIComponent {
    private lastScoreLabel: eui.Label;
    private lastScore: number;

    public constructor(lastScore: number) {
        super();
        this.lastScore = lastScore;
        this.addEventListener( eui.UIEvent.COMPLETE, this.uiCompHandler, this );
        this.skinName = "resource/custom_skins/GameOverScene.exml";     
    }

    private uiCompHandler():void {
        this.lastScoreLabel.text = `${this.lastScore}`;

        this.addEventListener( egret.TouchEvent.TOUCH_TAP, ( evt:egret.TouchEvent )=>{
            this.parent.addChild(new TitleScene());
            this.parent.removeChild(this);            
        }, this );
    }

}

ポップアップ画面の呼び出し

GameScene.ts
class GameScene extends egret.Sprite {
:
            this.parent.addChild(new GameOverScene(scoreUi.lastScore));
            this.parent.removeChild(this);      
:
0
0
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0