On this page
sprite
Functions, messages and properties used to manipulate sprite components.
Functions
sprite.set_hflip(url: string | Hash | Url, flip: boolean)
Sets horizontal flipping of the provided sprite's animations. The sprite is identified by its URL. If the currently playing animation is flipped by default, flipping it again will make it appear like the original texture.
// How to flip a sprite so it faces the horizontal movement (it is assumed that
// the sprite component has id "sprite" and the original animation faces right):
export default defineScript({
update(self, dt) {
// calculate self.velocity somehow
sprite.set_hflip("#sprite", self.velocity.x < 0);
},
});
Parameters
url:string | Hash | Url— the sprite that should flip its animationsflip:boolean—trueif the sprite should flip its animations,falseif not
sprite.set_vflip(url: string | Hash | Url, flip: boolean)
Sets vertical flipping of the provided sprite's animations. The sprite is identified by its URL. If the currently playing animation is flipped by default, flipping it again will make it appear like the original texture.
// How to flip a sprite in a game which negates gravity as a game mechanic (it is
// assumed that the sprite component has id "sprite" and the original animation
// is up-right):
export default defineScript({
update(self, dt) {
// calculate self.up_side_down somehow, then:
sprite.set_vflip("#sprite", self.up_side_down);
},
});
Parameters
url:string | Hash | Url— the sprite that should flip its animationsflip:boolean—trueif the sprite should flip its animations,falseif not
sprite.play_flipbook(url: string | Hash | Url, id: string | Hash, complete_function?: function(self, message_id, message, sender), play_properties?: Record<string | number, unknown>)
Play an animation on a sprite component from its tile set An optional completion callback function can be provided that will be called when the animation has completed playing. If no function is provided, a animation_done message is sent to the script that started the animation.
// Assuming the sprite has id "sprite": play the "jump" animation followed by the
// "run" animation:
export default defineScript({
init() {
const url = msg.url("#sprite");
sprite.play_flipbook(url, "jump", (self, message_id, message) => {
if (message_id === hash("animation_done") && message.id === hash("jump")) {
// jump animation done, chain with "run"
sprite.play_flipbook(url, "run");
}
});
},
});
Parameters
url:string | Hash | Url— the sprite that should play the animationid:string | Hash— hashed id of the animation to playcomplete_function?:function(self, message_id, message, sender)— function to call when the animation has completed.
self
object The current object.
message_id
hash The name of the completion message, "animation_done".
message
table Information about the completion:
-
number
current_tile- the current tile of the sprite. -
hash
id- id of the animation that was completed.
sender
url The invoker of the callback: the sprite component.
play_properties?:Record<string | number, unknown>— optional table with properties:
offset
number the normalized initial value of the animation cursor when the animation starts playing.
playback_rate
number the rate with which the animation will be played. Must be positive.
Properties
size: Vector3
The size of the sprite, not allowing for any additional scaling that may be applied. The type of the property is vector3. It is not possible to set the size if the size mode of the sprite is set to auto.
slice: Vector4
The slice values of the sprite. The type of the property is a vector4 that corresponds to the left, top, right, bottom values of the sprite in the editor. It is not possible to set the slice property if the size mode of the sprite is set to auto.
scale: Vector3
The non-uniform scale of the sprite. The type of the property is vector3.
image: Hash
The image used when rendering the sprite. The type of the property is hash.
material: Hash
The material used when rendering the sprite. The type of the property is hash.
cursor: number
The normalized animation cursor. The type of the property is number.
playback_rate: number
The animation playback rate. A multiplier to the animation playback rate. The type of the property is number. The playback_rate is a non-negative number, a negative value will be clamped to 0.
animation: Hash
READ ONLY The current animation id. An animation that plays currently for the sprite. The type of the property is hash.
frame_count: Hash
READ ONLY The frame count of the currently playing animation.