Skip to main content
GameDev.net gamedev.net

PRO Tired of ads? Read GameDev.net ad-free and help keep the community independent with GameDev Pro — $3/month.

Animotion babeh!

Animotion babeh!

_the_phantom_
Not dead... · · 2 min read
449 0
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 informationspr = eng:createSprite("explosion.png",8)-- using the baseimg table from an earlier post as a basespr = baseimg:new({x = 10, y = 10, sprite = spr, currentFrame = 0})-- we need a table of sprites to render as the rendering routine takes batchessprites = {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 = falsetext= ""lastupdate = 0lastexplodetime = 0while(quit == false)do	events = bonsai.getEvents()	for i = 1, #events 	do 		if events.message == bonsai.Event.types.quit 		then 			quit = true 		end 		if events.message == bonsai.Event.types.char		then			 char = bonsai.Event.charTranslate[events.param1]			 if char ~= nil then text = text .. char end		end 	end		eng:startFrame()	eng:renderQuads(imgs)	eng:renderSprites(sprites)	eng:renderText(0,100,text,font,{r=0,g=255,b=255})	currentTime = bonsai.currentTimeMillis()	if lastupdate + 20 < currentTime	then		for k, v in pairs(imgs) do v:update() end		if lastexplodetime + 80 < currentTime 		then 			spr.currentFrame = spr.currentFrame + 1			if spr.currentFrame == spr.sprite.frames then spr.currentFrame = 0 end			lastexplodetime = currentTime		end		lastupdate = currentTime	end	eng:endFrame()end


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...

Discussion

Loading comments...