Walking Player

BlitzMax Forums/BlitzMax Beginners Area/Walking Player

foosh(Posted 2007) [#1]
Hello,

I just recently bought BlitzMax, and after playing around quite a bit, I've decided to make a side-scrolling shooter. I'm trying to animate my character to look like he's walking upon KeyDown(KEY_RIGHT), and this is what I got.

Type THero
Field X:Int
Field Y:Int
Field Speed:Int=3
Field Frame:Int
Field Image:TImage

Function Create:THero(File:String,xstart:Int,ystart:Int)
Local WalkImage:THero=New THero
WalkImage.X=xstart
WalkImage.Y=ystart
WalkImage.Frame=animframe
WalkImage.image=LoadAnimImage(LoadBank(File),64,64,0,2)

If WalkImage.image=Null
Print "Image file not found. Quitting..."
End
End If
Return WalkImage
End Function

Method DrawSelf()
DrawImage Image,X,Y,Frame
End Method

Method UpdateState()
If KeyDown(KEY_LEFT)
X:- Speed
EndIf
If KeyDown(KEY_RIGHT)
X:+ Speed
If Frame<1 Then
Frame :+ 1
EndIf
If Frame>0 Then
Frame :- 1
EndIf
EndIf


When I run my app and DrawSelf() and UpdateState(), I get the following error:

Unhandled Exception:Attempt to index array element beyond array length

Any suggestions?


degac(Posted 2007) [#2]
frame must be in the range 0..total frame of image-1


mothmanbr(Posted 2007) [#3]
If Frame<1 Then
Frame :+ 1
EndIf
If Frame>0 Then
Frame :- 1
EndIf

If Frame is 0, then the first if will change it to 1, and the second one will change it back to 0. Since you only have two frames, I would suggest using something like:

If Frame = 1 then Frame = 0 else Frame = 1


And by the way, you should use a timer for everything in the game, otherwise it'll depend on the FPS. Very simple to make, add this before the loop:

CurrentTime = MilliSecs()


And this inside the loop to update it:

TimeDif = MilliSecs() - CurrentTime
CurrentTime = MilliSecs()


TimeDif should be a parameter in the "UpdateState" method.


foosh(Posted 2007) [#4]
Thank you so much! The last post was the most helpful, because with my complex code I was getting it to frame 1 but then it was getting stuck on that frame. I implemented the timer, and it all works beautifully now. Thank you!


mothmanbr(Posted 2007) [#5]
Glad I could help :)