Jump to content

Resize and onSizeChange fires many times + example code


Recommended Posts

I'm working on a game and I'm trying to implement dynamic resizing, but I have some unexplained issues. My game is optimized for a phone portrait layout. Depending on the phone screen size (or browser window size) it should adjust the layout, the width is fixed size but the height is adjusted. So make it taller if necessary and center some sprites or texts.

I've created a code example which works, but the resize function gets called many times. So to isolate and test the resize function see this github example:

The code example has 4 states: Boot, Preloader, MainMenu, Game. I won't post all the code here, but what I've done is essentially this:

// Boot state, in create() setup the scale.setResizeCallback event handler.
mygame.Bootup.prototype = {
	create: function(){
		this.game.scale.setResizeCallback(this.resizeCallback, this);
		this.game.scale.onSizeChange.add(this.onSizeChange, this);

	resizeCallback: function(manager) {
		// browser window changes size, resize the game
		var ratio = window.innerHeight / window.innerWidth;
		if (GAME_WIDTH * ratio > GAME_HEIGHT) {
			this.scale.setGameSize(GAME_WIDTH, Math.floor(GAME_WIDTH * ratio)) // too tall, adjust height resolution
		} else {
			this.scale.setGameSize(GAME_WIDTH, GAME_HEIGHT); // too wide, just use fixed width and empty side bars

	onSizeChange: function() {
		// fire the game resize event for the current state (make sure each state has this)

And then both the MainMenu and Game have a resize function that adjusts its layout and positions of sprites, text etc., it's a little similar to this topicMy github code example works, however the resizeCallback function from the Bootup state gets called all the time. It fires many times right from the beginning even before I do anything. I'm not resizing and not switching state or anything, and it get called not once but continually.

So my questions is: Why is the onSizeChange event called so often? :wacko:

Is this normal and should I use a different event?

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.

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.


  • Recently Browsing   0 members

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