Elegant system for doing sprite animations?

BlitzMax Forums/BlitzMax Programming/Elegant system for doing sprite animations?

Bukky(Posted 2006) [#1]
Hey all,

My first post on this forum...so here goes nothing!

I was wondering if there was an elegant way of doing sprite based animations using BlitzMax? I have a series of animated strips and was wondering if anyone has implemented a way to display them in blitz without using messy loops?

Lets say that, for instance, I have an animation loaded as such:

Global Test_Animation:Timage=LoadAnimImage("images/testanim.png",190,187,0,4)

Is there a way to invoke the animation by telling Blitz to play frames in the order of 0, 1, 0, 2, 0, 3, 0, 4 over a period of 2 seconds? Or would I have to code a methd to do this on my own?

Thanks!


Tom Darby(Posted 2006) [#2]
That's gonna be a decidedly roll-yer-own type of thing...


tonyg(Posted 2006) [#3]
You would have to code your own method.


Bukky(Posted 2006) [#4]
Hey guys,

Thanks for the speedy replies.

Not really what I wanted to hear, but my thoughts have been confirmed. Looks like I have a long afternoon ahead of me! :)


Scott Shaver(Posted 2006) [#5]
something like this should do it.

type foo
field image:TImage =...
field animspeed:long = 60
field lasttime:long = 0
field curframe:int = 0
field frameorder:int[] = [0,1,0,2,2,3,4,5,0,2];

method update(curtime:long)
 if curtime-lasttime>=animspeed then curframe:+1
 if curframe>frameorder.length then curframe=0
 lasttime = curtime
endmethod

method display()
 drawimage image,x,y,frameorder[curframe]
endmethod
end type



Bukky(Posted 2006) [#6]
Thanks Scott. I'll give it a try.


Bukky(Posted 2006) [#7]
Hmmm...I've been working on my animation class this morning, and so far so good. The display method that Scott suggested doesnt seem to work properly though...

I tried debugging it by printing out the values of curtime - lasttime, but the values that I get are never really higher than 20. The animation that I'm testing needs a 250 millisecond gap between each frame. Any suggestions?


Helios(Posted 2006) [#8]
Hope this works, wrote it off the top of my head.

type TAnimation
Field Image:TImage, X:Int, Y:Int
Field Frames:Int[] = [0,1,2,2,1,0]
Field CurrFrame:int
Field AnimTimer:Int = Millisecs()
Field AnimSpeed:Int = 250

method Update()
if Millisecs() - AnimTimer > AnimSpeed then AnimTimer = Millisecs() ; CurrFrame:+1
If CurrFrame > Frames.Length - 1 then CurrFrame = 0
end method

method Render()
DrawImage Image,X,Y,Frames[CurrFrame]
end method
end type



Bukky(Posted 2006) [#9]
Hey Helios!

Thanks for the quick reply!

Your Update() method seems to work as desired, but the Render() method that I have gives me an array out of bounds error :(

Here is the code that I have:

Method Display(xCoord:Int, yCoord:Int)
		DrawImage strip, xCoord, yCoord,frameorder[curframe]
End Method 



Helios(Posted 2006) [#10]
Hum, thats odd. Are you sure your loading your image correctly? This works fine on my computer....

Test.png should be a 32x32 image with 3 frames.

Type TAnimation
	Field Image:TImage, X:Int, Y:Int
	Field Frames:Int[] = [0,1,2,2,1,0]
	Field CurrFrame:Int
	Field AnimTimer:Int = MilliSecs()
	Field AnimSpeed:Int = 250

	Method Update()
		If MilliSecs() - AnimTimer > AnimSpeed Then AnimTimer = MilliSecs() ; CurrFrame:+1
		If CurrFrame > Frames.Length - 1 Then CurrFrame = 0
	End Method

	Method Render()
		DrawImage Image,X,Y,Frames[CurrFrame]
	End Method
End Type

Graphics 800,600,0,60

Local Anim:TAnimation = New TAnimation
Anim.Image = LoadAnimImage("Test.png",32,32,0,3)

Repeat
	Anim.Update()
	Anim.Render()
	Flip;Cls
Forever



Bukky(Posted 2006) [#11]
Hey!

I got it working. You were right, my array out of bounds problem had to do with the way I loaded my image.

thanks everyone for the help!


Booticus(Posted 2006) [#12]
Hye thanks everybody for this. This is indeed more elegant!