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

[tmlib.js]要素を複数作るときはgroupを作ると便利

More than 5 years have passed since last update.

要素を複数作るときはgroupを作ると便利

敵とかエフェクトとかなんもかんもaddChildTo(this)してると、管理用のメンバが増えてややこしくなる。

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

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

        this.enemys = []; // これがあんまり好きくない

        for (var i=0, len=10; i<len; ++i) {
            var enemy = tm.display.RectangleShape()
                .addChildTo(this);
            this.enemys.push(enemy);
        }

        // 全体になにかする
        this.enemys.each(function (l) {
            l.x += 100;
        });
    },
});

そこで、まとめる用グループを作っとけばいい感じになる。

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

    init: function() {
        this.superInit();
        this.fromJSON({
            children: {
                enemyGroup: "tm.display.CanvasElement",
            },
        });

        for (var i=0, len=10; i<len; ++i) {
            var enemy = tm.display.RectangleShape()
                .addChildTo(this.enemyGroup);
        }

        // 全体になにかする
        this.enemyGroup.children.each(function (l) {
            l.x += 100;
        });
    },
});

ついでに、for文かくのも面倒なので、こうするともっとすっきりする。

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

    init: function() {
        this.superInit();
        this.fromJSON({
            children: {
                enemyGroup: "tm.display.CanvasElement",
            },
        });

        (10).times(function () {
            var enemy = tm.display.RectangleShape()
                .addChildTo(this.enemyGroup);
        });

        // 全体になにかする
        this.enemyGroup.children.each(function (l) {
            l.x += 100;
        });
    },
});

というメモでした。

ちなみに、timesっていう便利関数はtmlib.jsのもので、標準じゃないのでご注意。

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