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:
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 GameObjectA name is resolved in this order:
- A prefab with that name in
settingsData.json. - An image in the initial assets (for example the texture atlas).
- 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:
this.AddGameObject('ball')
.SetPosition(100, 200)
.SetScale(2)
.SetRotation(45)
.CenterPivot();| Property | Description |
|---|---|
x, y, position | Position (position is a Vec2). |
scale, scaleX, scaleY | Scale. |
rotation | Rotation in degrees. |
pivot, pivotX, pivotY | Anchor point, in texture pixels from the top left. See Pivot. |
alpha, color, visible | Appearance. |
flipped | Mirror horizontally. |
width, height | Size (read-only). |
active | Inactive objects are not updated. |
tags, customData | Your 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).
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:
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
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
const health = player.AddComponent(HealthComponent);
player.GetComponent(HealthComponent);
player.HasComponent(HealthComponent);
player.RemoveComponent(HealthComponent);See Components.
Pointer input
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
obj.Remove();Pooled objects are returned to their pool, so they can be reused without new allocations.
Shaders and masks
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.