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