*** Sorry, solved this one. A quick game of 'Orcs Must Die' and I realised what it was. It's just an error in the DrawImage command, the rotation wasn't put down correctly ***
Just bought Monkey. I'm still at the stage where every 2nd line of code generates and error and without a debugger I simply can't work out what's causing this one.
I've stripped my code down to the basics and supplied it below. The idea is, when you hit key_left or key_right, the angle of your ship changes, but all I get is 'Array index out of range' and a bunch of errors. The up and down keys work fine, so it doesn't seem to be a syntax error since they're almost identical.
Not sure why it's even mentioning an array since there are none in the program. I tried removing the brackets incase that was confusing it, but it's not that.
Import mojo
Function Main()
New Game
End Function
Class Game Extends App
Field Player:PlayerClass
Method OnCreate()
Local f:Int
Player=New PlayerClass
Player.Image=LoadImage("spaceship.png",1,Image.MidHandle)
SetUpdateRate 60
End Method
Method OnUpdate()
Player.Update(1)
End Method
Method OnRender()
Local width:Int=DeviceWidth,height:Int=DeviceHeight
Cls 0,0,0
Player.Draw(width,height)
End Method
End Class
Class Sprite
Field Image:Image
Field X:Float
Field Y:Float
Field Speed:Float
Field Angle:Float
End Class
' Player object definition...
Class PlayerClass Extends Sprite
Field Turning:Float=5
Field Thrust:Float=5
Method Update(d)
CheckKeys(d)
Move(d)
End Method
Method Move(d)
Self.X=Self.X+(Sin(Self.Angle))*d*Self.Speed
Self.Y=Self.Y+(-Cos(Self.Angle))*d*Self.Speed
End Method
Method CheckKeys(d)
If KeyDown(KEY_LEFT)
Self.Angle=Self.Angle-(Self.Turning*d)
if Self.Angle<0 Then Self.Angle=Self.Angle+360
Endif
If KeyDown(KEY_RIGHT)
Self.Angle=Self.Angle+(Self.Turning*d)
if Self.Angle>359 Then Self.Angle=Self.Angle-360
Endif
If KeyDown(KEY_UP)
Self.Speed=Self.Speed+(Self.Thrust*d)
if Self.Speed>10 Then Self.Speed=10
Endif
If KeyDown(KEY_DOWN)
Self.Speed=Self.Speed-(Self.Thrust*d)
if Self.Speed<-10 Then Self.Speed=-10
Endif
End Method
Method Draw(width:Int,height:Int)
DrawImage Self.Image,width/2,height/2,Self.Angle
*** Should have been DrawImage Self.Image,width/2,height/2,Self.Angle,1,1,0 .... although the angle is wrong but I can sort that :p
End Method
End Class
|