Compare Method for z-sorting

BlitzMax Forums/BlitzMax Programming/Compare Method for z-sorting

Hi, I'm using an overriding Compare Method which someone on here used quite a while ago, I can get it to sort objects created by the type but I cant get it to sort different types, here I'm creating a Player and Enemy Types, the player z-sorts with the other player, but not with the enemy?
Strict

Graphics 640,480,0

Global entity_list:TList = CreateList()  'Create a list for our player and enemy

' Player Class --------------------------------
Type Player
	Field x:Float,y:Float
	Field image:TImage

	Function Create:Player(x,y,anim_file:String)
		Local ent:Player = New Player
		ent.x = x
		ent.y = y
		ent.image = LoadAnimImage(anim_file,128,128,0,1)

		entity_list.AddLast(ent)	'Add the player to the list
		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 )
		
		If Player(other)
			Return Sgn(y - Player(other).y)
		EndIf
		Return -1

	End Method

End Type

' Enemy Class ------------------------------------------
Type Enemy
	Field x:Float,y:Float
	Field image:TImage
	
	Function Create:Enemy(x,y,anim_file:String)
		Local ent:Enemy = New Enemy
		ent.x = x
		ent.y = y
		ent.image = LoadAnimImage(anim_file,256,256,0,1)
		
		entity_list.AddLast(ent)	'Add the enemy to the list
		Return ent
	End Function
		
	Method Update()
		
		DrawImage image,x,y,0
		
	End Method
	
	Method Compare( other:Object )
		
		If Enemy(other)
			Return Sgn(y - Enemy(other).y)
		EndIf
		Return -1

	End Method

	
End Type

'Create our Player instance
Local player1:Player = Player.Create(50,80,"Media/warrior.bmp")
Local player2:Player = Player.Create(100,50,"Media/warrior.bmp")
'Create our Enemy instances
Local forest_monster:Enemy = Enemy.Create(100,100,"Media/forest_monster.bmp")


'MAIN LOOP ----------------
While Not KeyHit(KEY_ESCAPE)
	Cls 
	
	SortList(entity_list)
	
	For Local p:Player = EachIn entity_list  'Update our player
		p.Update()
	Next
	For Local e:Enemy = EachIn entity_list	 'Update our enemy
		e.Update()
	Next

' IGNORE THIS, this is to make one player move slower than the other.
	player1.x :+ (KeyDown(KEY_RIGHT) - KeyDown(KEY_LEFT))*2
	player1.y :+ (KeyDown(KEY_DOWN) - KeyDown(KEY_UP))*2

	Flip
Wend


Thanks for any help.

Hi
Can't run code but this should help you:
Strict

Graphics 640,480,0

Global entity_list:TList = CreateList()  'Create a list for our player and enemy

' Player Class --------------------------------
Type Player
	Field x:Float,y:Float
	Field image:TImage

	Function Create:Player(x,y,anim_file:String)
		Local ent:Player = New Player
		ent.x = x
		ent.y = y
		ent.image = LoadAnimImage(anim_file,128,128,0,1)

		entity_list.AddLast(ent)	'Add the player to the list
		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 )
		
		If Player(other)
			Return Sgn(y - Player(other).y)
		ElseIf Enemy(other)
			Return Sgn(y - Enemy(other).y)
		EndIf
		
		Return -1

	End Method

End Type

' Enemy Class ------------------------------------------
Type Enemy
	Field x:Float,y:Float
	Field image:TImage
	
	Function Create:Enemy(x,y,anim_file:String)
		Local ent:Enemy = New Enemy
		ent.x = x
		ent.y = y
		ent.image = LoadAnimImage(anim_file,256,256,0,1)
		
		entity_list.AddLast(ent)	'Add the enemy to the list
		Return ent
	End Function
		
	Method Update()
		
		DrawImage image,x,y,0
		
	End Method
	
	Method Compare( other:Object )
		
		If Enemy(other)
			Return Sgn(y - Enemy(other).y)
		ElseIf Player(other)
			Return Sgn(y - Player(other).y)
		EndIf
		Return -1

	End Method

	
End Type

'Create our Player instance
Local player1:Player = Player.Create(50,80,"Media/warrior.bmp")
Local player2:Player = Player.Create(100,50,"Media/warrior.bmp")
'Create our Enemy instances
Local forest_monster:Enemy = Enemy.Create(100,100,"Media/forest_monster.bmp")


'MAIN LOOP ----------------
While Not KeyHit(KEY_ESCAPE)
	Cls 
	
	SortList(entity_list)
	
	For Local p:Player = EachIn entity_list  'Update our player
		p.Update()
	Next
	For Local e:Enemy = EachIn entity_list	 'Update our enemy
		e.Update()
	Next

' IGNORE THIS, this is to make one player move slower than the other.
	player1.x :+ (KeyDown(KEY_RIGHT) - KeyDown(KEY_LEFT))*2
	player1.y :+ (KeyDown(KEY_DOWN) - KeyDown(KEY_UP))*2

	Flip
Wend


I think you are on the wrong way. Something like this is more logical:

Strict 


Type TEntity
	Field Image:TImage
	Field X:Float, Y:Float
	Field Link:TLink
	
	Method Compare(OtherObj:Object)
		Local En:TEntity 
		
		En = TEntity(OtherObj)
		
		If Not En Return
		
		Return Y-En.Y
	End Method
	
	Method AddLast(List:TList)
		Link = List.AddLast(Self)
	End Method
	
	Method Remove()
		If Link
			Link.Remove()
			Link = Null
		EndIf
	End Method
	
	Method Update() Abstract 

End Type



Type TPlayer Extends TEntity
	'Player data
	
	Function Create:TPlayer()
	
	End Function
	
	Method Update()
		'Do something
	End Method
End Type


Type TEnemy Extends TEntity
	'Enemy data
	
	Function Create:TEnemy()
	
	End Function
	
	
	Method Update()
		'Do something
	End Method

End Type






Mfg Suco

Edit: Whoops, looks like I was a day late and a dollar short. :)

You probably need to make the types both extend a common base type.

Type Base
End Type

Type Player Extends Base
End Type

Type Enemy Extends Base
End Type

Then put the common stuff like x,y,image in the base and put the compare function in the base type.

Thanks guys, it was the base type which was causing issues, I needed to put the method in the Entity and change the updates in the loop to
Entity.Update().