Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
8
Help us understand the problem. What is going on with this article?
@omatoro

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

お試しあれ!

8
Help us understand the problem. What is going on with this article?
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
omatoro

Comments

No comments
Sign up for free and join this conversation.
Sign Up
If you already have a Qiita account Login
8
Help us understand the problem. What is going on with this article?