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

 Share

  • Recently Browsing   0 members

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