giorg Posted November 21, 2018 Share Posted November 21, 2018 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 More sharing options...
Fric Posted November 22, 2018 Share Posted November 22, 2018 Because it is in create function. As the function name explains it self, it's creating and that's it. You should place your code in to update function. Link to comment Share on other sites More sharing options...
Andrew Chen Posted November 22, 2018 Share Posted November 22, 2018 do not write the resize listener in phaser create function, just the same as above saying ,create function only implement one time , you should put the addlistener in window.onload function, here is an example ,http://www.ifiero.com/index.php/archives/1027 hope it will help you Link to comment Share on other sites More sharing options...
giorg Posted November 22, 2018 Author Share Posted November 22, 2018 hey andrew, the problem is that on resize (even at runtime) I have to interact with phaser scenes, so I cannot put it outside the game... thanks Link to comment Share on other sites More sharing options...
mapacarta Posted November 26, 2018 Share Posted November 26, 2018 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); giorg and jamespierce 1 1 Link to comment Share on other sites More sharing options...
giorg Posted November 26, 2018 Author Share Posted November 26, 2018 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 More sharing options...
Recommended Posts