There is a new component, ex.GraphicsComponent
to work with these graphics with ex.Actor
's and other ex.Entity
's
The ex.GraphicsComponent
allows users to manage graphics with Actors and Entities in an easy way
For most games, you will be using the graphics component off of Actors or plain Entities.
const image = new ex.ImageSource('./path/to/my/image.png')
await game.start()
const actor = new Actor({
x: 100,
y: 100,
})
actor.graphics.use(image.toSprite())
The graphics component allows developers to save named graphics to avoid passing around graphic object references if desired. These can be used to show specific graphics.
actor.graphics.add('jump', jumpAnimation)
actor.graphics.show('jump') // display the graphic
// equivalent to
actor.graphics.show(jumpAnimation) // display the graphic
actor.graphics.hide() // hide the graphic
If no name is specified when added to the graphics component it is considered the 'default' graphic and is shown automatically.
// graphic considered 'default' and displayed automatically
actor.graphics.add(jumpAnimation)
visible: boolean
opacity: number
offset: Vector
anchor: Vector
The graphics component allows access to the underlying graphics context.
const actor = new ex.Actor({...});
actor.graphics.onPostDraw = (ctx: ex.ExcaliburGraphicsContext) => {
ctx.save();
ctx.z = 99;
ctx.drawLine(ex.vec(0, 0), ex.vec(200, 200), ex.Color.Green, 10);
ctx.restore();
}
The layer's component adds a way for multiple graphics to be on top or behind each other for a certain actor or entity.
Layers can be ordered numerically, larger negative layers behind, and positive layers in front.
actor.graphics.layers.create({ name: 'background', order: -1 })
actor.graphics.layers.create({ name: 'foreground', order: 1 })
actor.graphics.layers.get('background').show(myBackground)
actor.graphics.layers.get('foreground').show(myForeground)
// no longer display the background
actor.graphics.layers.get('background').hide()
There is always a layer named 'default'
at order: 0
actor.graphics.show(myAnimation)
// is equivalent to
actor.graphics.layers.get('default').show(myAnimation)