Help us understand the problem. What is going on with this article?

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

More than 5 years have passed since last update.

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); });
    },
});

お試しあれ!

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away