Sign in to follow this  
A.Kman

Updating phaser game bounds on resize

Recommended Posts

Hey all!

 

I'm trying to update my game every time the screen is resized or turned from portrait to landscape and visa versa.

What I'm doing right now is using window.onresize = this.resize(); but resize() is being called every frame.

 

Does anyone either know why this isn't working or how to do this better?

 

Thank you!!!

Share this post


Link to post
Share on other sites

@samme is right, you have to use scaleMode.RESIZE.  I found that in a web browser, sometimes the resize callback didn't always get fired on the very last bit of resizing/moving, so here is how I implemented resizing/realigning:

In my Boot state, set the scale mode for the game:

this.game.scale.scaleMode = Phaser.ScaleManager.RESIZE;

 

Then, in my other game states (levels), I set flags for whether things need to be realigned (or not), and in the resize callback, only set the flag, and I do all the actual resizing in update(), like this:

create() {
    this.game.scale.setResizeCallback(this.alignLevel, this);
    this.resized = false;
    this.resizedDoubleCheck = false;
}

alignLevel() {
    this.resized = true;
}

update() {
    if (this.resized || this.resizedDoubleCheck) {
        // do all the realigning of things here
    }

    // set the flags to false, but ensure one final
    // realignment even if the setResizeCallback didn't fire
    if (this.resized) {
        this.resized = false;
        this.resizedDoubleCheck = true;
    } else if (this.resizedDoubleCheck) {
        this.resizedDoubleCheck = false;
    }
}

 

Share this post


Link to post
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...
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.