Array index out of range

Monkey Forums/Monkey Programming/Array index out of range

Farflame(Posted 2012) [#1]
*** 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



Samah(Posted 2012) [#2]
From graphics.monkey:
Function DrawImage( image:Image,x#,y#,frame=0 )

You're trying to pass the angle as a frame index. Here's what you should be using:
Function DrawImage( image:Image,x#,y#,rotation#,scaleX#,scaleY#,frame=0 )


Edit: Glad you fixed it. :)


Farflame(Posted 2012) [#3]
Hehe, replied just as I'd worked it out. Thanks though. I take it the angles aren't the same as in BlitzMax, because my ship is pointing the wrong way, although moving correctly.


Samah(Posted 2012) [#4]
I take it the angles aren't the same as in BlitzMax, because my ship is pointing the wrong way, although moving correctly.

Angles in Monkey are "correct" (watch me get flamed) in that rendering is rotated counter-clockwise from right. In BlitzMax, rotation is clockwise from top.


Farflame(Posted 2012) [#5]
I get it, so 10 degrees will rotate my ship 10 degrees to the left, rather than 10 degrees to the right? That seems counter-intuitive to me, maybe because I've been using Blitz and other languages.

Just spotted your Diddy framework, looks useful, gonna have a play around with it:)


Samah(Posted 2012) [#6]
It's because the rendering uses matrix math. Think back to your trigonometry days and remember that 0 degrees points in the positive x-axis such that sin(t) = 0 and cos(t) = 1. 90 degrees points in the positive y-axis such that sin(t) = 1 and cos(t) = 0.


Farflame(Posted 2012) [#7]
Hehe, I can't remember that far back :p

But yeah, it's just a case of changing Sin to -Sin in the movement code, so it's no big deal. Will take a while to adjust my thinking, but no big deal I'm sure :)