b5.Xoml
The Xoml class loads a XOML format JSON file (exported from the Booty5 game Editor) and turns it into Booty5 compatible objects, such as scenes, actors, resources etc..
Methods
parseResources(parent, objects) – Parse the XOML resources objects and turn it into Booty5 objects
- parent – The parent where the generated objects will be placed usually an instance of the App
- objects – XOML resources objects array
parseResource(parent, resource) – Parse the XOML resource object and turn it into Booty5 objects
- parent – The parent where the generated objects will be placed usually an instance of the App or a Scene
- resource – XOML resources object
- Returns the created Booty5 parent object
findResource(objects, name, type) – Searches the XOML objects array / object for the named resource of the specified type
- objects – XOML object or array of XOML objects to search
- name – The name of the resource to find
- type – The type of resource to find
- Returns the found resource or null
Examples
Generate Booty5 objects from XOML
Here is a small example XOML file that was exported from the Booty5 game Editor:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
window.globals = [ {"ResourceType": "Geometry", "Name": "rect3762", "GeometryType": "Poly", "Absolute": false, "Vertices": [-126.336,-106.399, -42.018,-90.992, 153.614,46.411, -66.468,106.4, -153.614,-7.28]}, {"ResourceType": "Shape", "Name": "rect3761", "ShapeType": "Polygon", "Width": 0, "Height": 0, "Absolute": false, "Vertices": [-126.336,-106.399, -42.018,-90.992, 153.614,46.411, -66.468,106.4, -153.614,-7.28]} ]; |
And here is the code that loads it and converts it to Booty5 objects:
1 2 3 4 5 |
// Create XOML loader var xoml = new b5.Xoml(app); // Parse and create global resources placing them into the app xoml.parseResources(app, xoml_globals); |
Dynamically creating an actor from XOML template
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
var app = b5.app; // This scene will receive a copy of ball object var game_scene = app.findScene("gamescene"); // Search Xoml gamescene for ball icon actor resource var ball_template = b5.Xoml.findResource(window.gamescene, "ball", "icon"); // Create ball from the Xoml template and add it to game_scene var xoml = new b5.Xoml(app); xoml.current_scene = game_scene; // Xoml system needs to know current scene so it knows where to look for dependent resources var ball = xoml.parseResource(game_scene, ball_template); ball.setPosition(0, -350); ball.vx = 4; ball.fill_style = "rgb(" + ((Math.random() * 255) << 0) + "," + ((Math.random() * 255) << 0) + "," + ((Math.random() * 255) << 0) + ")"; |
711 total views, 1 views today