set visible = true在Phaser中没有用

时间:2021-07-10 22:50:33

I'm making a game similar to the traditional magic tower and now I have a problem when dealing with the battles between the monster and the braver.

我正在制作一个类似于传统魔法塔的游戏,现在我在处理怪物和勇士之间的战斗时遇到了问题。

Here's my code in update():

这是我在update()中的代码:

function update(){
   // ...
   game.physics.arcade.collide(braver, monsters, battle, null, this);
   // ...
}

And here's my battle():

这是我的战斗():

function battle(player, monster) {
    var x = (player.attack > monster.defence ? player.attack - monster.defence : 0);
    var y = (monster.attack > player.defence ? monster.attack - player.defence : 0);
    battleScene.visible = true;
    while (1) {
        if (Date.now() > battleTimer) 
        {
            battleTimer = Date.now() + 1000;
            monster.health -= x;
            if (monster.health <= 0) {
                monster.kill();
                player.gold += monster.gold;
                player.exp += monster.exp;
                break;
            }
            player.health -= y;
            if (player.health <= 0) {
                player.kill();
                break;
            }
        }
    }
    battleScene.visible = false;
}

battleScene is a group, the battle box. What makes me crazy is that battleScene doesn't show on the screen at all (even with battleScene.visible = true).

battleScene是一个团体,战斗盒。令我疯狂的是,battleScene根本没有在屏幕上显示(即使使用battleScene.visible = true)。

I've tried game.world.bringToTop(battleScene); but it doesn't work either. So how can I fix that bug?

我试过game.world.bringToTop(battleScene);但它也不起作用。那么我该如何解决这个问题呢?

1 个解决方案

#1


0  

What type of object is the battleScene ? if it's a Phaser.State you need to start that state with the state Manager (Documentation)

什么类型的对象是battleScene?如果是Phaser.State,则需要使用状态管理器(文档)启动该状态

game.state.start('battle_scene_key')

If it's a Phaser.Sprite you need to add it to the game's world in order for it to draw.

如果它是Phaser.Sprite,你需要将它添加到游戏世界才能绘制。

// assuming `this` is a Phaser.State
state.add.existing(battleScene)

#1


0  

What type of object is the battleScene ? if it's a Phaser.State you need to start that state with the state Manager (Documentation)

什么类型的对象是battleScene?如果是Phaser.State,则需要使用状态管理器(文档)启动该状态

game.state.start('battle_scene_key')

If it's a Phaser.Sprite you need to add it to the game's world in order for it to draw.

如果它是Phaser.Sprite,你需要将它添加到游戏世界才能绘制。

// assuming `this` is a Phaser.State
state.add.existing(battleScene)