ResourceManager.hx
The ResourceManager is where the resources needed by the application are defined and loaded. In this application we are loading one 3D model from the fighter1.dae collada file.
In the constructor we initialise the properties. The static instance property is set to the current instance of the ResourceManager. This property is used later in the some anonymous functions. [code]
The successCount property holds a count of the number of resources that were successfully loaded. The failureCount hold a count of the number of resources that failed to load. The resourceCount defines how many resources are to be loaded. These variables are used to determine if all the resources were successfully loaded. [code]
Next we actually load the resources. A Parser is created, specifying the location of the resource to load, and the type of resource (defined by Parser.COLLADA in our case). Sandy-HX can load a number of file types, each defined by a constant in the Parser class (ASE, MD2, MAX_3DS and COLLADA formats are currently supported). [code]
Functions are then set to be triggered on either the ParserEvent.FAIL event (where the resource was not loaded) or the ParserEvent.INIT event (where the resource was loaded successfully). In order to save typing we have used some anonymous functions to deal with these events. Unlike other languages, Haxe anonymous functions are essentially static functions, so in order for them to interact with the current instance of the ResourceManager they reference the static instance property we initialised earlier.
If the resource is successfully loaded it is assigned to a property of the ResourceManager, and the updateLoadCount function is called to notify the ResourceManager that a resource has finished loading. We set the success parameter to true to indicate that the resource loaded successfully. [code]
If the resource failed to load the updateLoadCount function is called with the success parameter set to false. [code]
Finally the parse function is called on the Parser, telling it to go ahead and load the resources. [code]
The updateLoadCount is called every time a resource finishes loading, successfully or otherwise. Once all the resources has been loaded (by checking the total of the successCount and failureCount properties) either the EngineManagers resourcesLoaded or resourceLoadError functions are called, depending on whether or not all the resources loaded properly. [code].
ApplicationManager.hx
The Application manager makes use of the loaded resources to create a new RotatingMeshObject (a very simple class that extends the MeshObject and rotates the model). You can see that the ApplicationManager doesn’t need to worry about the asynchronous resource loading process, because by the time it is initialised the resources have been loaded and are ready to use.
The ResourceManager provides a very easy way to load additional resources in a manner that allows any other class to ignore the fact that the resources are loaded asynchronously. This simplifies what would otherwise be a fairly error prone procedure.
Check out the online demo here, and download the source code here.
Read more in the Flash and JavaScript 3D with Sandy-HX series