How to Implement types in a game.

BlitzMax Forums/BlitzMax Beginners Area/How to Implement types in a game.

Im having trouble right now. as I cannot think of how this can be done. :/
All I wanna do. Is draw an animated image based on what key is being pressed. each animation has a different width,height and amount of frames, and frames per second it should be played at. I just wanna know basics so I can try and implement other things later on like fading and flashing.
Heres my code currently for my types
Type TPlayer
	Field x, y, speed
EndType

Type TBomberman Extends TPlayer
	Field bombs, health, power, hammer, gloves, punch, skates

EndType

Type TAnimation Extends TBomberman
	Field maxFrames,animationFps,width,height
	Global animationCounter:Int = 0
	Global animationCount:Int = 7
	Global animationFrame:Int = 0
	Global maxFrames:Int = 33
EndType


Global animationCounter:Int = 0
Global animationCount:Int = 7
Global animationFrame:Int = 0
Global maxFrames:Int = 33
Global bomber:TImage=LoadAnimImage("gfx/players/bomberman/waiting.png",22,24,0,33,MIPMAPPEDIMAGE)
MidHandleImage(bomber)

Function updateAnimations()
animationCounter:+1 'increments every tick
If animationCounter >= animationCount 'once ready then we'll update which frame to display
animationCounter=0
	If animationFrame < maxFrames-1 'because we want 0 - 11 not frame 12
		animationFrame:+1
	Else
		animationFrame = 0
	End If
End If
SetScale(2,2)
DrawImage(bomber,320,240,animationFrame)
'DrawImage(bomb,120,140,animationFrame)

'draw all of your animations here based off the current animationFrame
'though keep in mind that with this example every animation would be running at 12 fps
'another way you could do it is to have animationCounter count through 0 - 59 and then
'just use a divide for which ticks you want your frame to be updated on for each animation


EndFunction

Does animation need its own type?

Thanks for helping whoever.

Here's how I would implement the animation ..

Basically, I update a global 'counter' variable in the UpdateAnimations() function and then do a
IF counter MOD frameupdatedelay = 0 currentframe+1


Graphics 640,480


Type TPlayer
	Global List:TList=New TList
	Field image:TImage
	Field x%, y%, w%, h%, speed%
	Field numframes%, currentframe% , frameupdatedelay%
	' add player to list
	Method New()
		List.AddLast Self
	End Method
	' load player graphics
	Method LoadPlayer%(file$,iw%,ih%,nframes%,flags%=MIPMAPPEDIMAGE)
		image=LoadAnimImage(file$,iw,ih,0,nframes,flags)
		Assert image,"image not loaded ->  "+file$
		w=iw ; h=ih ; numframes=nframes
	End Method
	' update all of the player animations
	Function UpdateAnimations()
		Global counter%
		counter:+1 'increments every tick
		For Local p:TPlayer=EachIn TPlayer.List
			If (counter Mod p.frameupdatedelay)=0
				p.currentframe:+1
				If p.currentframe=p.numframes p.currentframe=0
			EndIf
		Next
	End Function
	' draw all of the players on screen
	Function DrawPlayers()
		SetScale 2,2
		For Local p:TPlayer=EachIn TPlayer.List
			DrawImage p.image,p.x,p.y,p.currentframe
		Next
	End Function
End Type

Type TBomberman Extends TPlayer
	Field bombs, health, power, hammer, gloves, punch, skates
EndType

' ###########################

Global bomber:TBomberman=New TBomberman
bomber.LoadPlayer "gfx/players/bomberman/waiting.png",22,24 , 33
MidHandleImage bomber.image
bomber.x=320 ; bomber.y=240
bomber.frameupdatedelay=5 ' set the delay of animation here
' set up extras
bomber.bombs=5 ; bomber.health=20 ' etc ...


' ###########################
' MAIN LOOP
' ###########################

Repeat
	Cls
	TPlayer.UpdateAnimations
	TPlayer.DrawPlayers
	Flip
Until KeyHit(KEY_ESCAPE)


wow thanks jim! :) sure beats my crappy try.
Graphics(640,480,0)

'Global bomb=LoadAnimImage("gfx/items/anim/bomb.png",16,17,0,4,MIPMAPPEDIMAGE)

Global fps:TFps = New TFps


Global animationCounter:Int = 0
Global animationCount:Int = 7
Global animationFrame:Int = 0
Global maxFrames:Int = 0
AutoMidHandle(True)
Global waiting:TImage=LoadAnimImage("gfx/players/bomberman/waiting.png",22,24,0,33,MIPMAPPEDIMAGE)
Global walk_up:TImage=LoadAnimImage("gfx/players/bomberman/walk_up.png",14,24,0,10,MIPMAPPEDIMAGE)
Global walk_down:TImage=LoadAnimImage("gfx/players/bomberman/walk_down.png",14,24,0,10,MIPMAPPEDIMAGE)
Global walk_left:TImage=LoadAnimImage("gfx/players/bomberman/walk_left.png",17,24,0,10,MIPMAPPEDIMAGE)
Global walk_right:TImage=LoadAnimImage("gfx/players/bomberman/walk_right.png",17,24,0,10,MIPMAPPEDIMAGE)




Function updateAnimations()

SetScale(2,2)
Select True
				Case KeyDown(KEY_UP)
					maxFrames = 10
					DrawImage(walk_up,320,240,animationFrame)

				Case KeyDown(KEY_DOWN)
					maxFrames = 10
					DrawImage(walk_down,320,240,animationFrame)

				Case KeyDown(KEY_LEFT)
					maxFrames = 10
					DrawImage(walk_left,320,240,animationFrame)

				Case KeyDown(KEY_RIGHT)
					maxFrames = 10
					DrawImage(walk_right,320,240,animationFrame)

				Default
					maxFrames = 33	
					DrawImage(waiting,320,240,animationFrame)

EndSelect

animationCounter:+1 'increments every tick
If animationCounter >= animationCount 'once ready then we'll update which frame to display
animationCounter=0
	If animationFrame < maxFrames-1 'because we want 0 - 11 not frame 12
		animationFrame:+1
	Else
		animationFrame = 0
	End If
End If

'DrawImage(bomb,120,140,animationFrame)

'draw all of your animations here based off the current animationFrame
'though keep in mind that with this example every animation would be running at 12 fps
'another way you could do it is to have animationCounter count through 0 - 59 and then
'just use a divide for which ticks you want your frame to be updated on for each animation


End Function

'setup your timer here
Global refreshTimer:TTimer = CreateTimer(60)
fps.show = True
SetClsColor(255,255,255)
Repeat 
WaitEvent()
Select EventID()
Case EVENT_TIMERTICK 'gets called every 60 ticks

Cls

updateAnimations()
Flip
EndSelect
Until KeyDown(KEY_ESCAPE) Or AppTerminate()
End
Global x=0


err can i ask one more question? how would i create new players for different animations? or do i call the loadanimation method when a key is pressed?
many many thanks

For players which require separate animations I would use this method which separates the animation control from the TPlayer type:

Graphics 640,480

'  handles animated images (loading, setting update delay)
Type TAnimImage
	Field image:TImage , iwidth% , iheight%
	Field numframes% , currentframe% , framedelay%
	' load animation set
	Function Load:TAnimImage(file$, iw%, ih%, nframes%, fdelay%, flags%=MIPMAPPEDIMAGE)
		Local ta:TAnimImage=New TAnimImage
		ta.image=LoadAnimImage(file$,iw,ih,0,nframes,flags)
		Assert ta.image,"image not loaded ->  "+file$
		MidHandleImage ta.image
		ta.iwidth=iw ; ta.iheight=ih ; ta.numframes=nframes
		ta.framedelay=fdelay
		Return ta
	End Function
End Type


Type TPlayer
	Global List:TList=New TList
	Field anim:TAnimImage
	Field x%, y%, w%, h%, speed%
	' add player to list
	Method New()
		List.AddLast Self
	End Method
	' update all of the player animations
	Function UpdateAnimations()
		Global counter%
		counter:+1 'increments every tick
		For Local p:TPlayer=EachIn TPlayer.List
			If (counter Mod p.anim.framedelay)=0
				p.anim.currentframe:+1
				If p.anim.currentframe=p.anim.numframes p.anim.currentframe=0
			EndIf
		Next
	End Function
	' draw all of the players on screen
	Function DrawPlayers()
		SetScale 2,2
		For Local p:TPlayer=EachIn TPlayer.List
			DrawImage p.anim.image, p.x, p.y, p.anim.currentframe
		Next
	End Function
End Type

Type TBomberman Extends TPlayer
	Field bombs, health, power, hammer, gloves, punch, skates
EndType


' ###########################

' load a 17x24 animation with 10 frames and a delay of 8
Global bomber_walk_right:TAnimImage=TAnimImage.Load("gfx/players/bomberman/walk_right.png", 17,24, 10, 8)

' create a new player
Global bomber:TBomberman=New TBomberman
' set the player to use the 'walk right' animation
bomber.anim=bomber_walk_right
' position the player
bomber.x=320 ; bomber.y=240
' set up extras for bomberman
bomber.bombs=5 ; bomber.health=20 ' etc ...


' ###########################
' MAIN LOOP
' ###########################

Repeat
	Cls
	TPlayer.UpdateAnimations
	TPlayer.DrawPlayers
	Flip
Until KeyHit(KEY_ESCAPE)


Then, to set the player to a different animation set ..
bomber.anim=bomber_walk_up ' etc ...


seriously jim cant thank you enough. :)
also in you icon guide for blitzmax. it doesnt work for me. :/
but i found a way round it. seems that that the graphics function wasnt loading its own icon.