The popular HTML5 library Phaser have just released 2.3. It’s not a huge new functionality release, but it’s got some major architecture changes that will affect you greatly if you are a Phaser user. First off, they went down a more modular approach leading to fewer god classes. This is a good thing™. Then thanks to this redesign, they also made the Phaser build process different, allow you to disuse unneeded portions.
From the release notes:
GAME OBJECTS AND COMPONENTS
All of the core Game Objects have received an important internal restructuring. We have moved all of the common functions to a new set of Component classes. They cover functionality such as ‘Crop’, ‘Physics Body’, ‘InCamera’ and more. You can find the source code to each component in the
src/gameobjects/componentsfolder of the repo.
All of the Game Object classes have been restructured to use the new component approach. This puts an end to the "God classes" structure we had before and removes literally hundreds of lines of duplicate code. It also allowed us to add features to Game Objects; for example Bitmap Text objects are now full-class citizens with regard to physics capabilities.
Although this was a big internal shift from an API point of view not much changed – you still access the same methods and properties in the same way as before. Phaser is just a lot leaner under the hood now.
You can create your own custom Phaser classes, with your own set of active components by copying any of the pre-existing Game Objects and modifying them.
As a result of the shift to components we went through the entire source base and optimised everything we could. Redundant paths were removed, debug flags removed and new stub classes and hooks were created. What this means is that it’s now easier than ever to "disable" parts of Phaser and build your own custom version.
We have always included a couple of extra custom builds with Phaser. For example a build without P2 Physics included. But now you can strip out lots of additional features you may not require, saving hundreds of KB from your build file in the process. Don’t use any Sound in your game? Then you can now exclude the entire sound system. Don’t need Keyboard support? That can be stripped out too.
As a result of this work the minimum build size of Phaser is now just 83KB (minified and gzipped).
Please see this tutorial on how to create custom builds.
We’ve updated the core of Arcade Physics in a number of significant ways.
First we’ve dropped lots of internal private vars and moved to using non-cached local vars. Array lengths are no longer cached and we’ve implemented
physicsTypeproperties on Game Objects to speed-up the core World collideHandler. All of these small changes have lead to a nice improvement in speed as a result, and also allows us to now offer things like physics enabled BitmapText objects.
More importantly we’re now using a spacial pre-sort for all Sprite vs. Group and Group vs. Group collisions. You can define the direction the sort will prioritize via the new
sortDirectionproperty. By default it is set to
Phaser.Physics.Arcade.LEFT_RIGHT. For example if you are making a horizontally scrolling game, where the player starts on the left of the world and moves to the right, then this sort order will allow the physics system to quickly eliminate any objects to the right of the player bounds. This cuts down on the sheer volume of actual collision checks needing to be made. In a densely populated level it can improve the fps rate dramatically.
There are 3 other directions available (
BOTTOM_TOP) and which one you need will depend on your game type. If you were making a vertically scrolling shoot-em-up then you’d pick
BOTTOM_TOPso it sorts all objects above and can bail out quickly. There is also
SORT_NONEif you would like to pre-sort the Groups yourself or disable this feature.
Another handy feature is that you can switch the
sortDirectionat run-time with no loss of performance. Just make sure you do it before running any collision checks. So if you had a large 8-way scrolling world you could set the
sortDirectionto match the direction the player was moving in and adjust it in real-time, getting the benefits as you go. My thanks to Aaron Lahman for inspiring this update.
The Phaser.Loader has been updated to support parallel downloads which is now enabled by default (you can toggle it via the
Loader.enableParallelflag) as well as adding future extensibility points with a pack/file unified filelist and an inflight queue.
There are no known incompatibilities with the previous Loader. Be aware that with parallel downloading enabled the order of the Loader events may vary (as can be seen in the "Load Events" example).
The parallel file concurrency limit is available in
Loader.maxParallelDownloadsand is set to 4 by default. Under simulated slower network connections parallel loading was a good bit faster than sequential loading. Even under a direct localhost connection parallel loading was never slower, but benefited most when loading many small assets (large assets are more limited by bandwidth); both results are fairly expected.
The Loader now supports synchronization points. An asset marked as a synchronization point must be loaded (or fail to load) before any subsequent assets can be loaded. This is enabled by using the
addSyncPointmethods. Packs (‘packfile’ files) and Scripts (‘script’ files) are treated as synchronization points by default. This allows parallel downloads in general while allowing synchronization of select resources if required (packs, and potentially other assets in the future, can load-around synchronization points if they are written to delay final ‘loading’).
Additional error handling / guards have been added, and the reported error message has been made more consistent. Invalid XML (when loading) no longer throws an exception but fails the particular file/asset that was being loaded.
Some public methods/properties have been marked as protected, but no (except in case of a should-have-been-private-method) public-facing interfaces have been removed. Some private methods have been renamed and/or removed.
A new XHR object is created for each relevant asset (as there must be a different XHR for each asset loaded in parallel). Online searches indicated that there was no relevant benefit of XHR (as a particular use-case) re-use; and time will be dominated with the resource fetch. With the new flight queue an XHR cache could be re-added, at the cost of some complexity.
The URL is always transformed through transformUrl, which can make adding some one-off special cases like #1355 easier to deal with.
This also incorporates the fast-cache path for Images tags that can greatly speed up the responsiveness of image loading.
Loader.resetLocked is a boolean that allows you to control what happens when the loader is reset, which happens automatically on a State change. If you set
trueit allows you to populate the loader queue in one State, then swap to another State without having the queue erased, and start the load going from there. After the load has completed you could then disable the lock again as needed.
Thanks to @pnstickne for vast majority of this update.
We are now using our own custom build of Pixi v2. The Pixi project has moved all development resources over to Pixi v3, but it wasn’t ready in time for the release of Phaser 2.3 so we’ve started applying our own fixes to the version of Pixi that Phaser uses.
As a result we have removed all files from the
src/pixifolder that Phaser doesn’t use, in order to make this distinction clearer. This includes
EventTarget, so if you were relying on that in your game you’ll need to add it back in to your local build.
We’ve also removed functions and properties from Pixi classes that Phaser doesn’t require: such as the Interaction Manager, Stage.dirty, etc. This has helped us cut down the source code size and make the docs less confusing, as they no longer show properties for things that weren’t even enabled.
We’ve rolled our own fixes into our version of Pixi, ensuring we keep it as bug-free as possible.
You can read the entire release notes here.