【问题标题】:Phaser random generation of sprites in groupPhaser随机生成组中的精灵
【发布时间】:2016-05-14 00:23:37
【问题描述】:

我有一个组,我希望从左到右生成两种类型的块,反之亦然。这并不重要。我的随机化似乎工作正常,但问题是它们只有在我刷新游戏时才会改变。但是,如果我按原样运行它,在我刷新之前它始终是同一种块。我该如何解决这个问题?

createOnebloque: function () {
    this.bloquecs = ["bloqueSprite", "bloquelSprite"]; ////// Here are the 2 sprites
    this.bloquesr = this.bloquecs[Math.floor(Math.random() * 2)]; /// Here I randomize, but it only works when I refresh.
    this.bloque = this.game.add.group();
    this.bloque.createMultiple(5, this.bloquesr);
    this.bloque.setAll('anchor.x', 0.5);
    this.bloque.setAll('anchor.y', 0.5);
    this.bloque.setAll('outOfBoundsKill', true);
    this.bloque.setAll('checkWorldBounds', true);
},

makeBloques: function () {
    this.bloques = this.bloque.getFirstExists(false);
    if (this.bloques) {
        this.bloques.reset(1440, 1400);
        this.game.physics.arcade.enable(this.bloques);
        this.bloques.enableBody = true;
        this.bloques.body.immovable = true;
        this.bloques.body.velocity.x = -2000;
    }
}

下面是创建函数:

this.game.physics.arcade.collide(this.bullets, this.bloque, this.collisionBulletBloque, null, this);

【问题讨论】:

    标签: phaser-framework


    【解决方案1】:

    你的问题来自这里:

    this.bloquesr = this.bloquecs[Math.floor(Math.random() * 2)];
    

    您选择一个随机密钥并从那时起使用它。

    你也许可以像这样重写第一个函数:

    createOnebloque: function () {
        this.bloquecs = ["bloqueSprite", "bloquelSprite"];
        this.bloque = this.game.add.group();
    
        for (var i = 0; i < 5; i++) {
            this.bloque.create(this.bloquecs[Math.floor(Math.random() * this.bloquecs.length]);
        }
        this.bloque.setAll('anchor.x', 0.5);
        this.bloque.setAll('anchor.y', 0.5);
        this.bloque.setAll('outOfBoundsKill', true);
        this.bloque.setAll('checkWorldBounds', true);
    }
    

    我还没有测试过,但它应该可以工作。

    【讨论】:

    • 非常感谢让我测试并回来非常感谢,我想像这样但不确定如何继续谢谢。
    • 好吧,有点工作,如果我的 (this.bloquecs) 上有 2 个元素,它会重复显示 1 和 2,但如果我添加 3 个更多元素以使它们成为数组中的 5 个元素,它只会随机播放其中3个,这是一种常见的行为吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-03-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-27
    • 2019-01-12
    • 2014-08-26
    相关资源
    最近更新 更多