Defining and managing sprite animations?

Blitz3D Forums/Blitz3D Beginners Area/Defining and managing sprite animations?

Headdy(Posted 2011) [#1]
Long time no speak folks...

I've resumed work on a long-time project, and I'm wondering if there's an easy way of managing (2D) sprite animations.

Basically I have a player-controlled character who will perform several actions (such as walking, sliding and ducking), and each of these will have a separate animation - some looping, some playing once. The game will also have enemy characters, which in many cases will also have multiple animations.

I'm a bit stuck because, although I'm rusty with Blitz, I haven't ever seen an example of this being done with source code. I also can't think of a way to do it using Blitz's very limited Array functionality.


Yasha(Posted 2011) [#2]
Off the top of my head...


Const ANIM_PLAYING = 1, ANIM_STOPPED = 2
Const ANIM_MAXFRAMES = 100

Type Anim
    Field state
    Field doesLoop
    Field frame[ANIM_MAXFRAMES], frameCount
    Field currentFrame
End Type

Function UpdateAnimations()
    Local a.Anim
    For a = Each Anim
        UpdateAnim a
    Next
End Function

Function UpdateAnim(a.Anim)
    If a\state = ANIM_PLAYING
        a\currentFrame = a\currentFrame + 1
        If a\currentFrame = a\frameCount
            a\currentFrame = 0
            If a\doesLoop  = False Then StopAnim a
        EndIf
    EndIf
End Function

Function PlayAnim(a.Anim)
    a\state = ANIM_PLAYING
End Function

Function StopAnim(a.Anim)
    a\state = ANIM_STOPPED
End Function

Function DrawAnim(a.Anim, x, y)
    MyMagicalSpriteLibraryDrawingFunction a\frame[a\currentFrame], x, y
End Function


...I'd use something like that.


Headdy(Posted 2011) [#3]
Thank you, I will give that a try.


Kryzon(Posted 2011) [#4]
Yes, there is an easy way to handle all this.

Classify everything you will use, such as animations, enemies, NPCs and other characters.
Since Blitz3D can't have functions or "methods" within its Types, we need to use Fields storing values that represent the 'Class' or 'Kind' of your characters so that your Types can communicate with the functions that will update them:
Type TCharacter ;Generic character Type for every character in your game, ranging from the player to enemies and other NPCs.
	Field class% ;(1) Player; (2) Enemy; (3) Neutral NPC.
	Field subClass% ;Further differentiate characters of the same class (say, different types of enemies; they 
	;are all enemies (class 2) but are still different among each other).

	Field anim.TAnimation ;Instance of a TAnimation type to handle animation states and updating.
	
	[...]
End Type

Function UpdateCharacters()
	For c.TCharacter = Each TCharacter
		;Select the appropriate behavior based on the 'class' of each character:
		Select c\class
			Case 1
				UpdatePlayer(c) ;Updates player input and states.
			Case 2
				UpdateEnemy(c) ;UpdateEnemy will read the 'subclass' field and update states, steering, targets, goals etc. (generic AI).
			Case 3
				UpdateNPC(c) ;Updates states.
		End Select
		
		UpdateAnimation(c\anim) ;The TAnimation type is very generic and doesn't need to know the 
		;class or subclass of any character. It simply updates frames based on a speed and switches 
		;between 'sequences' or 'animImages'.
	Next
End Function
I didn't tell you how you should code that TAnimation type that every character will hold.
You need to code one so that you can implement an animation system similar to the way Blitz3D does its 3D animations (with different play modes, speeds, sequences, current frame etc.).
Organization and extensibility will take you a long way.


Yasha(Posted 2011) [#5]
Going slightly off the rails into something deeper, but here's a tutorial related to Kryzon's point: http://www.hpquest.com/techlord/apps/AOBPwB3D/AOBPwB3D.pdf

Perhaps useful if you're not familiar with OOP (OBP is the closest you can come in vanilla B3D).

And here's a cute hack (well, not really a hack - it's entirely intentional behaviour) for implementing polymorphic functions in B3D via Object/Handle: http://www.blitzbasic.com/Community/posts.php?topic=86449


Kryzon(Posted 2011) [#6]
Very nice links, and I had totally forgotten about Object() and Handle(); they'd be a boon to this kind of work.


Yue(Posted 2012) [#7]
Ho thanks Yasha, yes here: http://www.hpquest.com/techlord/apps/AOBPwB3D/AOBPwB3D.pdf

Oops, sorry doble post.

=)

Last edited 2012