5
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

phina.jsのフレーム更新サイクル

5
Last updated at Posted at 2018-11-29

前々から気になったのですが、気運が高まったのでまとめてみようと思います。
対象バージョンはv0.2.3です。

基本

こちらの図が大変分かりやすいです。

いくつか補足すると以下のような順番になっています。

0. (app更新)
1. input(mouse/touch/keyboard)イベント発火
2. app更新
3. scene更新
4. scene子要素(element)更新
5. interactionのチェック(onpointstart等の発火)
6. 描画処理
7. statsのupdate(あれば)
(最初に戻る)

  • 最初の方のカッコ書き何?って思うかもですが、これについては後述します。
  • interactionと描画処理も逆になってますが、これは後にタイミングが変更されました。

こちらのサンプルで実際に動作確認できます。

onenterframeとon('enterframe', func)とupdate

app/scene/element系クラスについては以上の更新処理関数を定義しているとそれが毎フレーム実行されます。updateはおなじみですね。

// 毎フレーム、3種類のログが表示される
phina.define('MainScene', {
  superClass: 'DisplayScene',

  init: function(options) {
    this.superInit(options);
        
    this.on('enterframe', function(e) {
      console.log('scene: enterframe');
    });
  },
  
  onenterframe: function(e) {
    console.log('scene: onenterframe');
  },

  update: function(app) {
    console.log('scene: update');
  },
});

実はこれらも順番が決まってて、

onenterframe -> on('enterframe', func) -> update

の順で実行されます。

on('enterframe', func)は複数定義でき、定義した順番に実行されます。
また基本的にオーバーライドされないので、派生クラスにも何らかの更新処理を仕込みたいときも便利です。

onenterframeは…あんまり使い所が浮かびませんが(というかそもそも使用が推奨されていない)、真っ先に実行したい処理を明示したいときに使うといいかもしれません。

inputイベントに関する補足

inputイベント発火はappのon('enterframe')によって実現しています。
なので厳密に言えば最初の方は

app.onenterframe -> input発火 -> 他のapp.on('enterframe') -> app.update -> scene更新...

の順になっています。
ちなみにmouse -> touch -> keyboardの順で発火します。

描画処理は?

図のとおりですが、何か思うところがあったら追記するかも…。

5
3
0

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
5
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?