Jump to content

How to immediately add a sprite to the stage?


nouse4aname
 Share

Recommended Posts

I'm trying to display a sprite at the same time I'm setting game.paused=true;  For some reason, it doesn't work.  Here's what the code is doing:

class Announcement extends Phaser.Sprite {
    constructor(game, str, ttl) {
        super(game, game.width / 2, game.height);
        this.health = ttl;
        this.fixedToCamera = true;
        //this.anchor.setTo(0.5, 0.5);

        var style = {font: "28px Arial", fill: "#ffffff", wordWrap: false, align: "center"};
        var text = game.make.text(0, 0, str, style);
        text.setShadow(2, 2, 'rgba(0, 0, 0, 0.5)', 1);
        text.anchor.set(0.5, 1);
        this.addChild(text);
    }
}

// elsewhere

game.layers.ui.add(new Announcement(game, "Testing", 3000));
game.paused = true;

game.layers.ui is already added to the stage and is visible and works perfectly while the game is running.  But when I run this code, nothing is displayed while the game is paused.  Only once I set game.paused = false; does the sprite (which is only text) show up on the screen.  If, however, I change the code to say:

game.layers.ui.add(new Announcement(game, "Testing", 3000));
setTimeout(function() { game.paused = true; }, 100);

Then everything works perfectly, with the exception of the game continuing for an addition 100ms.

So what gives?

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...