Today’s changes are hard fought but that mostly has to do with bug chasing. Blah.
I extended my audio preloading to also allow for preloading music files. In reality this is just a special case of a sound that is played looped, but by adding another method and refactoring some code, I could allow for music to be stored in a directory different from sounds, which is better for my delicate sensibilities.
A large part of my limited time today was spent banging my head against the dreaded “DOM Exception 11 error”. The situation is this:
The engine allows you to segregate your code logically into scenes, which makes a lot of things easier. You can request a scene change at any point, and the game loop will ensure that the swap happens at the start of the next frame, so that the handler for the frame update and frame render are always called in pairs.
This is all well and good, but what if you want the game to start at some specific scene? In that case your main code would call for a Scene change before it starts. In the general case this is no problem because as soon as you start the game, the first thing that the game loop will do is swap the scene. However, what if some code somewhere needs access to the current scene while everything is still initializing (e.g. in a constructor somewhere).
The Stage class has a property called currentScene that tells you this, but until the scene switch actually happens, it reports what the actual current scene is (as it should, naturally). This makes the above situation fail. In order to get around that, way back in the mists of a couple of months ago I modified the scene switch code so that if you try to switch scenes while the game loop is not running, it just happens right away instead of when the game loop runs. All is well.
Fast forward to this evening when I started getting Dom Exception 11 errors falling out of things. It took a bit to determine that the issue is caused by code trying to access the Audio element before it is fully ready to go. How could this be, with the preloader?! Simple: I set up my demo Scene instance to start music playing when it is activated. Then I switched scenes before I called run.
Since it’s the call to run that kicks off the preload, the scene switch happens before the preload starts and Kaboom.
I’ve set up an issue to look into this, but I’m not sure yet what exactly the best solution is. It seems like the property to return the current scene needs to return what WILL be the current scene if the game is not running, but not actually switch the scene until the run actually starts, so that both of the above situations is covered.
I need to think on that a little bit to ensure that I’m not going to just cause myself another problem.