Search Results

Friday, May 20, 2011

2D Sprites V1.8

Hey. So here's another release of 2D Sprites in the BGE. Note that there's a little bit different this time around.

For one thing, I've changed the format of the function - there aren't arguments to the Sprite (UV animation) and SpriteMesh (Replacing mesh animation) anymore except for what object to perform the animation on - if you leave it blank, it will use the object that the function is running on. So, the new format for animations would look like this:

from Sprites import *

cont = logic.getCurrentController()
obj = cont.owner

obj['sprite'] = obj.children[0] # The sprite object in this example is the child of the object running this script (the sprite is parented to this object, a collision box)

Sprite(obj['sprite']) # Automatically creates the necessary variables in, and then animates the sprite object


The Sprite functions will automatically create the variables necessary for them to run. If you want to alter the values (like which animation is playing, or how fast they are playing), make sure of two things:

1) I've changed the variable names around so that they begin with 'spr' - spritefps has become sprfps, anim has become spranim, and so forth, to avoid naming conflicts.

2) The variables don't exist in the sprite object until you call one of the Sprite functions - if you try to access them before you run the functions (not set them, but read them), then there will be an error. To account for this, I've also made a SpriteInit() function that will create the variables necessary for the sprite functions to run - the sprite functions already call this function, though, so you don't need to run the function for the sprites to work, but it avoids errors. An example of this would be:


SpriteInit(obj['sprite'])

if obj['sprite']['sprfps'] == 0:
     obj['sprite']['sprfps'] = 1    # Notice that checking the value of 'sprfps' would throw an error if SpriteInit() wasn't above, as the variables don't exist before this

SpriteMesh(obj['sprite'])

Anyway, that's just about it. Download the Sprites V1.8 module here.

No comments:

Post a Comment