Skip to content

Sprite Guide

Peter Robinson edited this page Jul 5, 2020 · 2 revisions

Introduction

All scene objects are known engine types that allow instances to be created at runtime. The "Sprite" is slightly different from other game type classes. It is derived from a parent type called "SpriteBase", which itself is derived from "SceneObject". That means it includes all the fields from those types as well as adding its own fields specific to itself.

A Sprite is simply a two dimensional image or animation that can be placed within a scene. Since the actual image rendering properties are inherited from SpriteBase and all collision and physics properties from SceneObject, there are actually only a couple fields and methods that belong to the Sprite itself. Nevertheless, a Sprite is one of the most commonly used objects within a scene.

TorqueScript Bindings

Exposed Fields

The Sprite type exposes the following fields in addition to those it inherits. Shown are the equivalent methods available that perform the same action in setting and getting the respective field:

  • FlipX
  • setFlipX(bool)
  • getFlipX()
  • FlipY
  • setFlipY(bool)
  • getFlipY()

The following is a complete description of each of these fields.

FlipX (bool)

Sets whether the image texture is flipped horizontally or not. The default is false.

%object = new Sprite();
%object.FlipX = true;

FlipY (bool)

This field sets whether the image texture is flipped vertically or not. The default is false.

TAML Format

Using TorqueScript and the exposed fields or methods described in the previous section, you can programmatically create and configure a Sprite. You can then export this type to a TAML file or even create a TAML file manually and read it into your game at an appropriate time.

Here is an example Sprite TAML file in XML format:

<Sprite
    SceneLayer="31"
    Size="100 75"
    BodyType="Static"
    BlendColor="SlateGray"
    Image="@asset=ToyAssets:highlightBackground"
    Frame="0" />

The same example in JSON format:

{
    "Sprite": {
        "SceneLayer": "31",
        "Size": "100 75",
        "BodyType": "Static",
        "BlendColor": "SlateGray",
        "Image": "@asset=ToyAssets:highlightBackground",
        "Frame": "0"
    }
}

Additional API

The following methods are available to further configure and control a Sprite.

setFlip(bool flipX, bool flipY)

Sets the sprite texture flipping status for each axis.

getFlip()

Gets the flip status for each axis.

Clone this wiki locally