sprites...almost got it down, just not quite
BlitzMax Forums/BlitzMax Beginners Area/sprites...almost got it down, just not quite
| ||
| I have started on a fairly-complex sidescroller with the working title "Guardian Knights." Wait, why am I telling you this? It's irrelevant to the question at hand! Anyways, due to the large amount of code I created a separate import/include file for the player setup "GKPLAYR.BMX" to put into the (currently very tiny) main source file "GKMAIN.BMX" Gosh I love typing those filenames like that it's so fun. '************************** '* GUARDIAN KNIGHTS * '* version 0.03 * '* * '* painstakingly recoded * '* in BlitzMax * '* * '* 2009 Nick Hagen * '* * '* adept25@... * '************************** '****** Init Section ****** Graphics 640,480,0 'Imported files Import "GKPLAYR.bmx" GKMAIN so far...when I run it it undoubtedly fails but it gives me this strange and scary compile error: "Unable to convert from 'Int' to 'brl.max2d.TImage' Anyways, here's GKPLAYR
'*** Include this in the main code ***
' Nick Hagen 5-19-2009
Global RunLeft: Int = False, RunRight: Int = False
Global StandLeft: Int = False, StandRight: Int = False
Global AimDir: Int 'The 8 total directions player can aim/face
'Moving (runnin) speed
Const MoveSpeed: Int = 5
'Define Sprites
spr_p1_SndL: TImage = LoadAnimImage("gfx\csa1sndl.png",64,64,0,4)
spr_p1_SndR: TImage = LoadAnimImage("gfx\csa1sndr.png",64,64,0,4)
spr_p1_RunL: TImage = LoadAnimImage("gfx\csa1runl.png",64,64,0,8)
spr_p1_RunR: TImage = LoadAnimImage("gfx\csa1runr.png",64,64,0,8)
'****** Types *************
'Define Player
Type t_player
Field x_pos:Int, y_pos:Int 'X and Y positions
Field sprite: TImage = spr_p1_SndR 'Start standing right
Field state:Int 'Player's state
'Player's methods
Method UpdateState()
'Move left
If KeyDown(KEY_LEFT) Then
AimDir = 0 '0 is left
RunLeft = True
sprite = spr_p1_RunL
'Stand left
ElseIf Not KeyDown(KEY_LEFT) And AimDir = 0 Then
RunLeft = False
sprite = spr_p1_SndL
Else
EndIf
'Move right
If KeyDown(KEY_RIGHT) Then
AimDir = 4 '4 is right
RunRight = True
sprite = spr_p1_RunR
Else
RunRight = False
sprite = spr_p1_SndR
EndIf
End Method
End Type
I am quite proud of how I wrote all this out: I think I have the concept down (different sprite animations for standing, running, jumping, shooting, etc.) but something's not right. Can you point me in the right direction and tell me what I did wrong? If you're confused with what I've said so far then I'll clarify for you. |
| ||
| I think when you define sprites, you must declare them global so they will not be auto declared as ints inside the type. I am not sure though |
| ||
| I think when you define sprites, you must declare them global so they will not be auto declared as ints inside the type. I am not sure though Tried it, didn't get any errors either :P All I need to do now is actually code the player data. Can you tell me a bit more about IMPORT? I LOVE your username btw. I spent many hours of my childhood reading those awesome books XD |
| ||
The error you are getting is because you are incorrectly storing the "players state".Field state:Int 'Player's state should be Field state:Timage 'Player's state This means when you come to draw your sprite you would just drawimage state, x,y. Dont forget to put in a variable that keeps a track of the animation frame that you are on, which gets reset everytime you change the players sprite. This method is ok for a start but you need to invest the time into creating a little sprite engine, makes life a whole lot easier..... IMPORT simply inserts your other BMX file into the file when its compiled. Ahhhh put SUPERSTRICT or STRICT as the first line of your main BMX file, makes life easier in the long run. |