独自のイベントを登録・発行する【Phina.js】

独自のイベントを登録・発行する

// グローバルに展開
phina.globalize();

// メインシーン
phina.define("MainScene", {

  // 継承
  superClass: 'DisplayScene',

  //初期化
  init: function () {
    //親クラス初期化
    this.superInit();
    //背景色
    this.backgroundColor = 'lightgrey';
    // Shapeを作成してシーンに追加
    var shape = Shape().addChildTo(this).setPosition(320, 480);
    //背景色設定
    shape.backgroundColor = 'red';
    //タッチを可能にする
    shape.setInteractive(true);
    //タッチイベント登録
    shape.onpointstart = function () {
      //hitイベント発行
      shape.flare('hit');
    };
    //hitイベント登録
    shape.onhit = function () {
      alert('Hit!');
    };
    // shape.on('hit', function () {
    //   alert('Hit!');
    // });

  },
});

//メイン処理

phina.main(function () {
  // アプリケーションを生成
  var app = GameApp({
    // MainSceneから開始
    startLabel: 'main',
  });
  // fps表示
  //app.enableStats();
  //実行
  app.run();
});

 

▷独自イベントの登録

shape.onhit = function() {
  alert('Hit!');  
};

独自イベントを登録するためには、on + イベント名の関数をshapeに実装させる。
今回のイベント名はhitとし、onhit関数とした。

 

▷独自イベント発行

shape.onpointstart = function () {
  shape.flare('hit');

イベントの発行はflare(‘イベント名’)で行う。

 

コメントを残す

あなたのメールアドレスは公開されません。必須項目には印がついています *