new b5.Scene(){b5.Scene}
A Scene is a container for game objects. You should create scenes to hold your content (Actors and resources) then add them to App to be processed and rendered. You can add logic to the scene via its update() method and or by attaching an onTick event handler. A Scene has the following features:
- Manages scene local resources
- Manages scene local Timeline animations using a TimelineManager
- Manages a collection of local action lists using an ActionsListManager
- Manages an events manager for scene wide events
- Manages a task manager
- Manages a collection of Actors
- Supports a camera
- Camera can target actors and follow them on x and y axis
- Position and scaling
- Touch panning (user can drag the camera around)
- Box2D world physics
- Extents which limit camera movement
- Can detect when an actor in the scene has been touched
- Clipping of child actors against scene, also supports clipping shapes
- Scene wide opacity
- Layer ordering
Supports the following event handlers:
- onCreate() - Called just after Scene has been created
- onDestroy() - Called just before Scene is destroyed
- onTick(dt) - Called each time the Scene is updated (every frame)
- onBeginTouch(touch_pos) - Called when the Scene is touched
- onEndTouch(touch_pos) - Called when the Scene has stop being touched
- onMoveTouch(touch_pos) - Called when a touch is moved over the Scene
- onKeyPress(event) - Called when a key is pressed and this scene has focus
- onKeyDown(event) - Called when a key is pressed down and this scene has focus
- onKeyUp(event) - Called when a key is released and this scene has focus
Examples
Example that shows how to create a scene with optional extras
var scene = new b5.Scene();
scene.name = "my_scene"; // Name the scene
b5.app.addScene(scene); // Add the scene to the app for processing
b5.app.focus_scene = scene; // Set our scene as the focus scene
Enable scene touch panning example
scene.touch_pan_x = true;
scene.touch_pan_y = true;
Add clipper to scene example
var clipper = new b5.Shape();
clipper.type = b5.Shape.TypeCircle;
clipper.width = 300;
scene.clip_shape = clipper;
Add a scene update (onTick) handler example
scene.onTick = function(dt) {
this.x++;
};
Add touch handlers to a scene example
scene.onBeginTouch = function(touch_pos) {
console.log("Scene touch begin");
};
scene.onEndTouch = function(touch_pos) {
console.log("Scene touch end");
};
scene.onMoveTouch = function(touch_pos) {
console.log("Scene touch move");
};
Make scene camera follow a target actor example
scene.target_x = player_actor;
scene.target_y = player_actor;
Create a resource and add it to the scenes resource manager then later find it and destroy it
var material = new b5.Material("static_bounce");
material.restitution = 1;
scene.addResource(material, "Material");
// Find resource
var material = b5.Utils.findResourceFromPath("scene1.material1", "material");
material.destroy();
Add a physics world to a scene
scene.initWorld(gravity_x, gravity_y, do_sleep);
Add an actions list to a scene
// Create actions list
var actions_list = new b5.ActionsList("turn", 0);
// Add an action to actions list
actions_list.add(new b5.A_SetProps(actor, "vr", 2 / 5));
// Add actions list to the scenes actions list manager
scene.actions.add(actions_list);
Add an animation timeline to a scene
// Create a timeline that scales actor1
var timeline = new b5.Timeline(actor1, "_scale", [1, 1.5, 1], [0, 0.5, 1], 1, [b5.Ease.quartin, b5.Ease.quartout]);
// Add timeline to the scene for processing
scene.timelines.add(timeline);
For a complete overview of the Scene class see Booty5 Scene Overview
Properties:
Name | Type | Description |
---|---|---|
app |
b5.App | Parent app (internal) |
actors |
Array.<b5.Actor> | = []; - Array of top level actors (internal) |
removals |
Array.<b5.Actor> | Array of actors that should be deleted at end of frame (internal) (internal) |
world |
object | Box2D world (internal) |
timelines |
b5.TimelineManager | Actor local animation timelines (internal) |
actions |
b5.ActionsListManager | Actions list manager (internal) |
order_changed |
number | Set to true when child actors change order (internal) |
bitmaps |
Array.<b5.Bitmap> | Bitmap resources (internal) |
brushes |
Array.<object> | Brush resources (internal) |
shapes |
Array.<b5.Shape> | Shape resources (internal) |
materials |
Array.<b5.Material> | Material resources (internal) |
sounds |
Array.<b5.Sound> | Audio resources (internal) |
events |
b5.EventsManager | Events manager |
tasks |
b5.TasksManager | Tasks manager (internal) |
name |
string | Name of the scene (used to find scenes in the app) |
tag |
string | Tag (used to find groups of scenes in the app) |
active |
boolean | Active state, inactive scenes will not be updated (default true) |
visible |
boolean | Visible state, invisible scenes will not be drawn (default true) |
layer |
number | Visible layer (set via property _layers) (default 0) |
x |
number | Scene x axis position |
y |
number | Scene y axis position |
w |
number | Scene width (default 1024) |
h |
number | Scene canvas height (default 768) |
clip_children |
boolean | If set to true then actors will be clipped against extents of this scene (default true) |
clip_shape |
b5.Shape | If none null and clipping is enabled then children will be clipped against shape (clip origin is at centre of canvas), if set via _clip_shape then instance of shape or string based path to shape can be used (default is null) |
camera_x |
number | Camera x position |
camera_y |
number | Camera y position |
camera_vx |
number | Camera x velocity |
camera_vy |
number | Camera y velocity |
vx_damping |
number | Camera x velocity damping (default 1) |
vy_damping |
number | Camera y velocity damping (default 1) |
follow_speed_x |
number | Camera target follow speed x axis (default 0.3) |
follow_speed_y |
number | Camera target follow speed y axis (default 0.3) |
target_x |
b5.Actor | Camera actor target on x axis (default null) |
target_y |
b5.Actor | Camera actor target on y axis (default null) |
touch_pan_x |
boolean | If true then scene will be touch panned on x axis (default false) |
touch_pan_y |
boolean | If true then scene will be touch panned on y axis (default false) |
panning |
boolean | Set to true if scene is currently being panned |
min_panning |
number | Minimum distance that a touch moves to be classed a as a pan (squared) |
world_scale |
number | Scaling from graphical world to Box2D world (default 20) |
time_step |
number | Physics time step in seconds (use 0 for based on frame rate) (default 0) |
extents |
Array.<number> | Scene camera extents [left, top, width, height] |
opacity |
number | Scene opacity (default 1) |
frame_count |
number | Number of frames that this scene has been running |
touching |
boolean | Set to true when user touching in the scene |
touchmove |
boolean | Set to true when touch is moving in this scene |
Returns:
Type | Description |
---|---|
b5.Scene | The created Scene |
Methods
-
addActor(actor){b5.Actor}
core/scene.js, line 271 -
Adds the specified actor to this scenes child list, placing the specified actor under control of this scene
Name Type Description actor
b5.Actor An actor
Returns:
Type Description b5.Actor The supplied actor -
addResource(resource, type)
core/scene.js, line 845 -
Adds a resource to the this scenes local resource manager
Name Type Description resource
object The resource to add
type
string Type of resource to add (brush, sound, shape, material, bitmap or geometry)
-
areResourcesLoaded(){boolean}
core/scene.js, line 907 -
Checks the scenes resources to see if all preloaded resources have been loaded
Returns:
Type Description boolean true if all scene preloaded resources have been loaded, otherwise false -
baseUpdate(dt)
core/scene.js, line 648 -
Main base scene update method that is called by the main app object each logic loop. Performs many actions including:
- Calling onTick() callback
- Updating local timelines manager
- Updating local actions manager
- Update child actor hierarchy
- Update physics world
- Cleaning up destroyed child actors
- Sorting child actor layers
Name Type Description dt
number Time that has passed since this scene was last updated in seconds
-
bringToFront()
core/scene.js, line 389 -
Moves the scene to the end of the apps child list, effectively rendering it on top of all other scenes that have the same depth
-
countResourcesNeedLoading(){number}
core/scene.js, line 924 -
Returns how many resources that are marked as preloaded in this scene still need to be loaded
Returns:
Type Description number Number of resources that still need to loaded -
destroy()
core/scene.js, line 256 -
Destroys the scene and all of its contained actors and resources
-
dirty()
core/scene.js, line 829 -
Dirties all child actor transforms forcing them to be rebuilt
-
draw()
core/scene.js, line 593 -
Renders the scene and all of its contained actors
-
findActor(name, recursive){b5.Actor}
core/scene.js, line 339 -
Searches the scenes children to find the named actor
Name Type Description name
String Name of actor to find
recursive
boolean If true then this scenes entire child actor hierarchy will be searched
Returns:
Type Description b5.Actor The found actor or null if not found -
findActorById(id, recursive){b5.Actor}
core/scene.js, line 365 -
Searches the scenes children to find the actor by its id
Name Type Description id
number Id of actor to find
recursive
boolean If true then this scenes entire child actor hierarchy will be searched
Returns:
Type Description b5.Actor The found actor or null if not found -
findHitActor(position){b5.Actor}
core/scene.js, line 812 -
Traverses the actor hierarchy testing touchable actors to see if the supplied position lies within their bounds
Name Type Description position
object The x,y position to be tested
Returns:
Type Description b5.Actor The actor that was hit or null if no actor was hit -
findResource(name, type){object}
core/scene.js, line 887 -
Searches the scenes local resource manager for the named resource. If the resource is not found within the scene then the app global resource space will be searched
Name Type Description name
string Name of resource to find
type
string Type of resource to add (brush, sound, shape, material, bitmap or geometry)
Returns:
Type Description object The found resource or null if not found -
initWorld(gravity_x, gravity_y, allow_sleep)
core/scene.js, line 541 -
Creates and initialises a Box2D world and attaches it to the scene. Note that all scenes that contain Box2D physics objects must also contain a Box2D world
Name Type Description gravity_x
number X axis gravity
gravity_y
number Y axis gravity
allow_sleep
boolean If set to true then actors with physics attached will be allowed to sleep which will speed up the processing of physics considerably
-
onMoveTouchBase(e)
core/scene.js, line 503 -
Callback that is called when the user moves a touch around the screen, provided that this scene has primary or secondary focus
Name Type Description e
object Touch event object
-
release()
core/scene.js, line 245 -
Releases this scene, destroying any attached physics world and child actors. If the scene has the onDestroy callback defined then it will be called
-
removeActor(actor)
core/scene.js, line 282 -
Removes the specified actor from this scenes child list
Name Type Description actor
b5.Actor An actor
-
removeActorsWithTag(tag)
core/scene.js, line 291 -
Removes all actors from this scenes child list that match the specified tag
Name Type Description tag
String Actor tag
-
removeResource(resource, type)
core/scene.js, line 862 -
Removes a resource from this scenes local resource manager
Name Type Description resource
object The resource to remove
type
string Type of resource to add (brush, sound, shape, material, bitmap or geometry)
-
sendToBack()
core/scene.js, line 413 -
Moves the scene to the start of the apps child list, effectively rendering behind all other scenes that have the same depth
-
update(dt)
core/scene.js, line 712 -
Updates the scene and all of its contained actors
Name Type Description dt
number The amount of time that has passed since this scene was last updated in seconds
-
updateCamera(dt)
core/scene.js, line 725 -
Updates the scenes camera
Name Type Description dt
The amount of time that has passed since this scene was last updated in seconds