Jump to content

resize question


giorg
 Share

Recommended Posts

hello,

I have this in my scene's create:

window.addEventListener("resize", this.resize(), false);

then at the same level of the create I put:

resize() {
        alert(window.innerWidth)
    }

anybody can explain me why this is working only the first time I load the game, but not when I resize the window?

thanks

Link to comment
Share on other sites

Since english isn't my native,  it is a little hard for me to explain the problem. It is working 1 time because the way you add the resize function to the event listener is wrong.  When you add it as this.resize(), it runs the function and adds the returned value (it is undefined in your code) as the callback function for the event listener. So it doesn't call anything. If you want to access this(scene) in the resize function you can use bind or old that=this method :)  Bind is usually a more elegant solution, so this should work in your case:

window.addEventListener("resize", resize.bind(this), false);

 

 

Link to comment
Share on other sites

13 minutes ago, mapacarta said:

Since english isn't my native,  it is a little hard for me to explain the problem. It is working 1 time because the way you add the resize function to the event listener is wrong.  When you add it as this.resize(), it runs the function and adds the returned value (it is undefined in your code) as the callback function for the event listener. So it doesn't call anything. If you want to access this(scene) in the resize function you can use bind or old that=this method :)  Bind is usually a more elegant solution, so this should work in your case:


window.addEventListener("resize", resize.bind(this), false);

thank you so much!

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...