Screen resize causes errors
I have started working on a new game framework as my last one started getting too messy. I have functional infinite paralax scrolling backgrounds as part of the camera in isomath, and a cool module based system for game objects to make object behaviors more dynamic. I have made a wee asteroids ship example to test out the basic physics module.
I have run into a problem though. When I try to resize the play area during game play, I suddenly get errors and nothing drawn to the canvas.
https://microstudio.io/i/SynKrown/darkmatter/
Thats my project. I dont know at which point the screen resize stopped working properly, whether it is the tiled backgrounds or something earlier on.
Cheers
A few days ago I also found this error and tried to understand and solve it.
In JavaScript, when a given object or data is not looked at by other objects or other data does not have pointers to this area, memory is freed - the reference counter to a given area has dropped to zero.
In my opinion this is a MicroStudio (frame work) bug.
When you change the window size, the screen object is re-initialized and during this process some objects must be removed by the JavaScript engine (memory cleaning - garbage collection).
I have several theories about what's going on
- Apparently the data is only connected to the object that is re-created during resize (maybe it's only connected to screen).
- the runtime loses context to part of the data
- I looked at the Microstudio code, it creates an interface for screen. I think that this interface must also be modified during resize. This is probably not being done correctly.
I noticed that the asteroids object turns into a function during resize.
draw = function() {
print(system.time() +" asteroids = " + typeof asteroids );
asteroids.draw();
}
The solution I found is to add a second variable that looks at the object you lose during resize.
init = function() {
catchConsole();
a = asteroids();
asteroids = new a.Asteroids();
b = asteroids;
asteroids.init();
}
update = function() {
if(typeof asteroids==="function"){
print("asteroids is function !!!!")
asteroids = b;
}
asteroids.update();
}
draw = function() {
print(system.time() +" asteroids = " + typeof asteroids );
asteroids.draw();
}
Wow man thats some awesome debugging skills you have there. I will implement that and add a comment explaining. Thanks. Oh and hey, I took your advice with having the physics in a separate class to the game objects and built on the idea by havig the game objects use a modular behavior system. So I can inherit the Mod class for a new Mod, set up everything needed for the functionality intended and the game object processes the mods methods like update etc. Keeps things tidy and separated