• Advertisement
Sign in to follow this  
  • entries
  • comments
  • views

Animotion babeh!

Sign in to follow this  


So, I've been putting off doing animated sprites as I didn't know how I was going to handle the loading and frame selection aspect of them.

I was going to use a tool I was told about after I posted a thread asking for a program to generate sprite sheets and a definition file which gives details, however in the end I decided against that, in part because I wasn't getting on with the method it uses to define where sprites are.

Then I considered rolling my own tool, but didn't fancy working out how to get a GTL loaded image to display in C#, well at least sanely, so decided against that plan for now. However, the tool will get written at a later date.

In the end I settled for just telling it that there are 8 frames in the image, because right now that's all I need to do.

Some code writing and binding later and we have animated explosions going on.. yay!

Loading and defining in Lua is pretty simple;

-- spr is a table which holds all the sprite related information
spr = eng:createSprite("explosion.png",8)

-- using the baseimg table from an earlier post as a base
spr = baseimg:new({x = 10, y = 10, sprite = spr, currentFrame = 0})

-- we need a table of sprites to render as the rendering routine takes batches
sprites = {spr}

The use of 'sprite' to hold the reference to the sprite data is manatory so that the drawing function knows where to look for the data.

I also took this moment to stick some timebased updates in, namely a 30 updates per second limit. This was a good test of the small timeGetTime() wrapper function, which until point hadn't been tested.

So, the final(?) test loop looks like this;

quit = false
text= ""
lastupdate = 0
lastexplodetime = 0
while(quit == false)
events = bonsai.getEvents()
for i = 1, #events
if events.message == bonsai.Event.types.quit
quit = true
if events.message == bonsai.Event.types.char
char = bonsai.Event.charTranslate[events.param1]
if char ~= nil then text = text .. char end


currentTime = bonsai.currentTimeMillis()
if lastupdate + 20 < currentTime
for k, v in pairs(imgs) do v:update() end
if lastexplodetime + 80 < currentTime
spr.currentFrame = spr.currentFrame + 1
if spr.currentFrame == spr.sprite.frames then spr.currentFrame = 0 end
lastexplodetime = currentTime
lastupdate = currentTime


I believe, pending the creation of a few 'constant' colours I have enough code now to port the shoot 'em up from Java based to Bonsai based \o/

So, I guess I should get on it...
Sign in to follow this  


Recommended Comments

There are no comments to display.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Advertisement