Problem using Method Compare

BlitzMax Forums/BlitzMax Programming/Problem using Method Compare

verfum(Posted 2007) [#1]
I'm trying to use the Compare Method to sort out z-ordering, now I can get it to work with a Player Type, but if I have a PlayerType Type the function returns an Unhandled Exception error. Here is the code which works:
Strict

Type Entity
	Field x:Float,y:Float,speed:Float,dir:Float,maxspeed:Float, range:Float
	Field currentFrame:Int,startFrame:Int,lastFrame:Int,timer:Float,frameRate:Int
	Field image:TImage
	Field file
	Field font:TImageFont
	Field name:String
	Field health:Float

End Type 

Type Player Extends Entity
	Field health:Float, mana:Float
	Field score:Int = 0
	
	Function Create:Player(name:String,x,y)
		Local ent:Player = New Player
		ent.name = name
		ent.x = x
		ent.y = y
		ent.health = 50
		ent.image = LoadAnimImage("Media/warrior.bmp",128,128,0,1)

		entity_list.AddLast(ent)
		Return ent
	End Function
	
	Method Update()
	
		If KeyDown(KEY_RIGHT)
			x = x +1
		EndIf
		If KeyDown(KEY_LEFT)
			x = x -1
		EndIf
		If KeyDown(KEY_DOWN)
			y = y +1
		EndIf
		If KeyDown(KEY_UP)
			y = y -1
		EndIf
					
		DrawImage image,x,y,0

	End Method

	Method Compare( other:Object )
		Return y -Player(other).y
	End Method
EndType

Graphics 640,480,0

Global entity_list:TList = CreateList()

Local player1:Player = Player.Create("Mikey",50,50)

While Not KeyHit(KEY_ESCAPE)
	Cls 
SortList (entity_list)
For Local p:Player = EachIn entity_list
		p.Update()
	Next

player1.x :+ (KeyDown(KEY_RIGHT) - KeyDown(KEY_LEFT))*2
	player1.y :+ (KeyDown(KEY_DOWN) - KeyDown(KEY_UP))*2

Flip
Wend

So the above works, but if we introduce a PlayerType it returns an error at Method Compare.
Strict

Type Entity
	Field x:Float,y:Float,speed:Float,dir:Float,maxspeed:Float, range:Float
	Field currentFrame:Int,startFrame:Int,lastFrame:Int,timer:Float,frameRate:Int
	Field image:TImage
	Field file
	Field font:TImageFont
	Field name:String
	Field health:Float

End Type

'--------------------------------
Type PlayerType
	
	Function Create:PlayerType(name:String,anim_file:String)
		Local ent:PlayerType = New PlayerType
		ent.image = anim_file
		ent.name = name

		entity_list.AddLast(ent)
		Return ent
	End Function
		
End Type

'--------------------------------
Type Player Extends Entity
	Field playerType:PlayerType
	Field health:Float, mana:Float
	Field score:Int = 0
	
	Function Create:Player(name:String,x,y,playerType:PlayerType)
		Local ent:Player = New Player
		ent.name = name
		ent.x = x
		ent.y = y
		ent.health = 50
		ent.playerType = playerType
		ent.image = LoadAnimImage(playerType.image,128,128,0,1)

		entity_list.AddLast(ent)
		Return ent
	End Function
	
	Method Update()
		
		If KeyDown(KEY_RIGHT)
			x = x +1
		EndIf
		If KeyDown(KEY_LEFT)
			x = x -1
		EndIf
		If KeyDown(KEY_DOWN)
			y = y +1
		EndIf
		If KeyDown(KEY_UP)
			y = y -1
		EndIf
			
		DrawImage image,x,y,0
				
	End Method


	Method Compare( other:Object )
		Return y -Player(other).y  'ERROR HERE
	End Method
EndType

Graphics 640,480,0

Global entity_list:TList = CreateList()

Global warrior:PlayerType=PlayerType.Create("Warrior")',"Media/warrior.bmp"

'Player instance
Local player1:Player = Player.Create("Mikey",50,50,warrior)


While Not KeyHit(KEY_ESCAPE)
	Cls 

	SortList (entity_list)

	For Local p:Player = EachIn entity_list
		p.Update()
	Next

	player1.x :+ (KeyDown(KEY_RIGHT) - KeyDown(KEY_LEFT))*2
	player1.y :+ (KeyDown(KEY_DOWN) - KeyDown(KEY_UP))*2

	Flip
Wend

So I need to adjust the Method Compare code to allow for the PlayerType, but I'm not sure how?


fredborg(Posted 2007) [#2]
	Method Compare( other:Object )
		If Entity(other)
			Return Sgn(y-Entity(other).y)  'ERROR HERE
		EndIf
		Return -1
	End Method



verfum(Posted 2007) [#3]
Oooh he's good! hehe thanks that worked a treat :)


verfum(Posted 2007) [#4]
Strange, I would of thought this Method using Entity isntead of Player would use all entities in the list, it doesn't seem to include objects created by my Enemy type which is also extended from Entity?