This wiki has been moved to https://github.com/SuperTux/wiki into the mediawiki branch.
Difference between revisions of "Sprite"
(→Introduction: minor grammar fix)
|Line 63:||Line 63:|
Revision as of 00:56, 28 June 2006
Most animated images you see in supertux are sprites. Sprite definitions are defined in individual files that end in ".sprite", e.g. "data/images/creatures/snowball.sprite", and follow S-expr syntax.
(supertux-sprite (action (name "left") (hitbox 2 4 31.8 31.8) (images "left-0.png" "left-1.png" "left-2.png") ) (action (name "right") (hitbox 2 4 31.8 31.8) (mirror-action "left") ) (action (name "squished-left") (hitbox 1 -19 31.8 31.8) (images "squished-left.png") ) (action (name "squished-right") (hitbox 1 -19 31.8 31.8) (mirror-action "squished-left") ) )
Let's look at the sprite "snowball":
A sprite consists of a set of actions. An action is simply an animation composed from several images. The engine can render a sprite and change it's currently displayed action. The snowball sprite, for example, contains 4 actions named "left", "right", "squished-left" and "squished-right".
Additionally you can define animation speed (fps <n>) and a hitbox (hitbox <x> <y> <w> <h>).
The hitbox offset indicates where the "origin" of the sprite is: If the engine draws a sprite at position 50,50 then <x> will be substracted from the x coordinate and <y> will be substraced from the y-coordinate. The hitbox size will get read by most sprites' gamecode to set the width and height of an imaginary rectangle that defines what parts of a sprite are solid.
The sprite description file is s-expr based. A sprite description starts with a (sprite) list, It should then contain a name entry (name "myname") and then several (action) entries (at least 1).
The action block contains then the name of an action (name "myaction") can optionally contain an (hitbox <x> <y> <w> <h>) block. It's also possible to add an (fps number) block to define the number of frames played per second. Finnaly comes a list of (images) or the '(mirror-action "actionname") keyword which will take an already defined action and flip all it's images vertically.
Adding Own Sprites / Testing
You can easily add your own sprites by creating a custom .sprite file and placing it somewhere appropriate, e.g. "data/creatures/<name>/<name>.sprite" for badguys or "data/objects/<name>/<name>.sprite" for game objects. Now you probably want to test your sprite. This is easily possible by adding a scriptedobject to an existing level. If you have define a new sprite called "wormy" then you can test it by adding this object to an existing level file (the coordinates should be adjusted to something sensible):
(scriptedobject (name "TESTSPRITE") (x 160) (y 100) (visible #t) (sprite "data/<path>/<name>/<name>.sprite") )
And supertux will display the action called "default" in the sprite. You can test other actions by adding some commands to the (init-script) of the level like this (this will play the "slurp" animation):
(init-script " TESTSPRITE.set_animation(\"slurp\"); ")