LoginSignup
8
8

More than 5 years have passed since last update.

[tmlib.js]シーンまわりでよく使うイベント

Last updated at Posted at 2014-04-16

tmlib.jsの、シーン周りでよく使うイベント

備忘録としてまとめておきます。

tm.define("HogeScene", {
    superClass: "tm.app.Scene",

    init: function(){
        this.superInit();
    },

    /** クラス生成・破棄まわり **/
    onenter: function(){/*このシーンがappにreplaceScene(or pushScene)されたときに呼ばれる*/},
    onexit: function(){/*他のシーンにreplaceSceneしたとき(or 自分自身をpopScene)に呼ばれる*/},
    onresume: function(){/*pushしたシーンがpopSceneを実行し、このシーンに戻ってきたときに呼ばれる*/},
    onpause: function() {/*自分の上にシーンがpushSceneされたときに呼ばれる*/},

    /** pointingまわり **/
    onpointingstart: function(){ /*タッチ開始時*/ },
    onpointingmove: function(){  /*タッチし続ける間、ずっと呼ばれ続ける*/ },
    onpointingend: function(){   /*タッチ終了時*/ },

    /** updateまわり **/
    onenterframe: function(){ /*this.update関数が呼ばれる前に、毎フレーム呼ばれる*/ },
});

以下のように書くこともできます。

tm.define("HogeScene", {
    superClass: "tm.app.Scene",

    init: function(){
        this.superInit();

        // onを省いて、以下のように書くこともできる
        this.on("enter", function(){});
        this.on("exit", function(){});
        this.on("resume", function(){});
        this.on("pause", function(){});

        this.on("pointingstart", function(){});
        this.on("pointingmove", function(){});
        this.on("pointingend", function(){});

        this.on("enterframe", function(){});
    },
});

違いは
- on関数を使うと複数のイベントを登録することができること。
- onenterとかで書くと、下記のように途中で書き換えることができること。

tm.define("HogeScene", {
    superClass: "tm.app.Scene",

    init: function(){
        this.superInit();
        this.counter = 0;
    },

    onenterframe: function(){
        ++this.counter;
        console.log(this.counter);

        // 一度実行したら書き換える
        this.onenterframe = function(){};
    },
});

tm.define("Hoge2Scene", {
    superClass: "tm.app.Scene",

    init: function(){
        this.superInit();
        this.counter = 0;
        var self = this;
        // たくさん登録できる
        this.on("pointingstart", function(){ console.log(++self.counter); });
        this.on("pointingstart", function(){ console.log(++self.counter); });
        this.on("pointingstart", function(){ console.log(++self.counter); });
        this.on("pointingstart", function(){ console.log(++self.counter); });
    },
});

お試しあれ!

8
8
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
8
8