Skip to content

GameObjects ​

A GameObject is anything in your scene. It holds position, scale, rotation, pivot, alpha and color, and supports children, components, shaders, masks, events and pointer input.

Adding a GameObject ​

AddGameObject accepts several kinds of input:

ts
this.AddGameObject('player');                     // a prefab name or image name
this.AddGameObject('/player');                    // force the image, skip the prefab
this.AddGameObject('enemies/saw');                // an image path
this.AddGameObject({ extends: 'player', x: 50 }); // an entity
this.AddGameObject(Asset.Image.EMOJI_GEAR);       // from generated assets
this.AddGameObject(MyGameObjectClass);            // a class
this.AddGameObject();                             // an empty GameObject

A name is resolved in this order:

  1. A prefab with that name in settingsData.json.
  2. An image in the initial assets (for example the texture atlas).
  3. An image in public/images/, which is streamed in.

The second argument is the parent: a layer name, a layer index or another GameObject.

Example: the Showcase's basics demo adds and rotates a single GameObject.

Transform ​

Most setters return the object, so you can chain them:

ts
this.AddGameObject('ball')
    .SetPosition(100, 200)
    .SetScale(2)
    .SetRotation(45)
    .CenterPivot();
PropertyDescription
x, y, positionPosition (position is a Vec2).
scale, scaleX, scaleYScale.
rotationRotation in degrees.
pivot, pivotX, pivotYAnchor point, in texture pixels from the top left. See Pivot.
alpha, color, visibleAppearance.
flippedMirror horizontally.
width, heightSize (read-only).
activeInactive objects are not updated.
tags, customDataYour own data, saved with the object.

Pivot ​

The pivot is the point of the image that sits at the object's position, and that it rotates and scales around. SetPivot(x, y) takes fractions of the texture size: values above 0 and up to 1 are a fraction, larger values are pixels. CenterPivot() is SetPivot(0.5, 0.5).

ts
obj.SetPivot(0.5, 1);   // bottom center
obj.CenterPivot();

The fraction is of the texture's own size, not the scaled size. Reading pivot, pivotX or pivotY always gives pixels, and assigning to them directly sets pixels too: on a 64×64 image, obj.SetPivot(0.5, 0.5) gives a pivot of { x: 32, y: 32 }, and obj.pivotX = 0.5 means half a pixel. A prefab's pivot works like SetPivot.

Inner transform ​

Unique to Bare, every GameObject also has an inner transform: innerPosition, innerScale, innerRotation, innerPivot and innerFlipped. It moves the texture without moving the object itself. This makes many effects possible without nesting objects, for example squashing a character while its position stays the same:

ts
import { TweenTo } from 'bare-engine';

TweenTo(player, 0.1, { innerScaleY: 0.8 });

Children follow the inner transform by default. Set childrenInheritInnerTransform = false to change that.

Examples: Platformer coins bob up and down with innerY, and RPG enemies wiggle with innerRotation while they walk.

Children ​

ts
const ship = this.AddGameObject('ship');
const gun = ship.AddChild('gun').SetPosition(20, 0);

ship.GetChildByName('gun');
gun.RemoveFromParent();

Use ToGlobal(point) and ToLocal(point) to convert between a child's space and world space.

Components ​

ts
const health = player.AddComponent(HealthComponent);
player.GetComponent(HealthComponent);
player.HasComponent(HealthComponent);
player.RemoveComponent(HealthComponent);

See Components.

Pointer input ​

ts
button.OnPointerDown(() => console.log('pressed'));
button.OnPointerUp(...);
button.OnPointerEnter(...);
button.OnPointerLeave(...);
button.OffPointers();

See Input.

Ordering ​

BringToFront() and BringToBack() change draw order within the object's layer.

Removing ​

ts
obj.Remove();

Pooled objects are returned to their pool, so they can be reused without new allocations.

Shaders and masks ​

ts
obj.AddShader(FlashShader);
obj.RemoveShader(FlashShader);
obj.SetMask(maskObject);

Custom GameObject classes ​

Extend GameObject and override its lifecycle methods (OnAwake, OnStart, OnUpdate, OnFixedUpdate, OnRender, OnRemoved, …). See Lifecycle & Cleanup for how object hooks differ from scene hooks. For most behavior a component is the better choice, because it can be attached from data.

Example: the Trust game's Peep extends GameObject and overrides OnUpdate.

Bare Engine is open source under the MIT license.