Actions
Actions
Actions can be chained together and can be set to repeat, or can be interrupted to change.
Actor actions are available off of Actor.actions.
Chaining Actions
You can chain actions to create a script because the action methods return the context, allowing you to build a queue of actions that get executed as part of an ActionQueue.
tsclass Enemy extends ex.Actor {public patrol() {// clear existing queuethis.actions.clearActions()// guard a choke point// move to 100, 100 and take 1.2s// wait for 3s// move back to 0, 100 and take 1.2s// wait for 3s// repeatthis.actions.repeatForever((ctx) => {ctx.moveTo(100, 100, 1200).delay(3000).moveTo(0, 100, 1200).delay(3000)})}}
tsclass Enemy extends ex.Actor {public patrol() {// clear existing queuethis.actions.clearActions()// guard a choke point// move to 100, 100 and take 1.2s// wait for 3s// move back to 0, 100 and take 1.2s// wait for 3s// repeatthis.actions.repeatForever((ctx) => {ctx.moveTo(100, 100, 1200).delay(3000).moveTo(0, 100, 1200).delay(3000)})}}
Example: Follow a Path
You can use Actor.actions.moveTo to move to a specific point, allowing you to chain together actions to form a path.
This example has a Ship follow a path that it guards by
spawning at the start point, moving to the end, then reversing
itself and repeating that forever.
tspublic class Ship extends ex.Actor {public onInitialize() {const path = [new ex.Vector(20, 20),new ex.Vector(50, 40),new ex.Vector(25, 30),new ex.Vector(75, 80)];// spawn at start pointthis.actions.repeatForever(ctx => {this.x = path[0].x;this.y = path[0].y;// create action queue// forward path (skip first spawn point)for (let i = 1; i < path.length; i++) {ctx.moveTo(path[i].x, path[i].y, 300);}// reverse path (skip last point)for (let i = path.length - 2; i >= 0; i--) {ctx.moveTo(path[i].x, path[i].y, 300);}});}}
tspublic class Ship extends ex.Actor {public onInitialize() {const path = [new ex.Vector(20, 20),new ex.Vector(50, 40),new ex.Vector(25, 30),new ex.Vector(75, 80)];// spawn at start pointthis.actions.repeatForever(ctx => {this.x = path[0].x;this.y = path[0].y;// create action queue// forward path (skip first spawn point)for (let i = 1; i < path.length; i++) {ctx.moveTo(path[i].x, path[i].y, 300);}// reverse path (skip last point)for (let i = path.length - 2; i >= 0; i--) {ctx.moveTo(path[i].x, path[i].y, 300);}});}}
While this is a trivial example, the Action API allows complex routines to be programmed for Actors. For example, using the Tiled Map Editor you can create a map that uses polylines to create paths, load in the JSON using a Generic Resource, create a TileMap, and spawn ships programmatically while utilizing the polylines to automatically generate the actions needed to do pathing.
Custom Actions
You can write your own actions by implementing the Action interface and
queueing them with ActionContext.runAction. An action gets update(elapsed)
every frame until isComplete(entity) returns true, reset() when a repeat
starts it over, and stop() when the queue is cleared.
typescriptclass Shake implements ex.Action {id = ex.nextActionId();private _elapsed = 0;constructor(private actor: ex.Actor, private duration: number, private magnitude: number) {}update(elapsed: number) {this._elapsed += elapsed;this.actor.pos = this.actor.pos.add(ex.vec(Math.random() - 0.5, Math.random() - 0.5).scale(this.magnitude));}isComplete() {return this._elapsed >= this.duration;}reset() {this._elapsed = 0;}stop() {this._elapsed = this.duration;}}actor.actions.runAction(new Shake(actor, 500, 4)).fade(0, 200).die();
typescriptclass Shake implements ex.Action {id = ex.nextActionId();private _elapsed = 0;constructor(private actor: ex.Actor, private duration: number, private magnitude: number) {}update(elapsed: number) {this._elapsed += elapsed;this.actor.pos = this.actor.pos.add(ex.vec(Math.random() - 0.5, Math.random() - 0.5).scale(this.magnitude));}isComplete() {return this._elapsed >= this.duration;}reset() {this._elapsed = 0;}stop() {this._elapsed = this.duration;}}actor.actions.runAction(new Shake(actor, 500, 4)).fade(0, 200).die();
You can also manipulate an Actor's ActionQueue directly through Actor.actionQueue, for example with ActionQueue.add.