CollideImage getting hit object

BlitzMax Forums/BlitzMax Programming/CollideImage getting hit object

MattVonFat(Posted 2006) [#1]
Hi. I've done a search and alot of what I found on this subject has helped with this Method. But it still doesn't want to work right.



Now I have 2 Types in use. Gog and Food. Each has been added to the FoodLayer. I have 30 Food and 1 Gog.

This method is called by Food. The main problem is that the 'Collided' object array only contains the Food type that has called it and wont contain the Gog when I collide it with a Food, so I never have that '1' printed. Every Food and Gog I create will have the ID as the type (So If I craeted G:Gog the id will be G).

I've seen posts where other people have got this working but I still cant get it right myself so I guess I just havent understood how it works.

Does anyone know what I'm doing wrong?
Thanks
Matthew


klepto2(Posted 2006) [#2]
Global foodlayer = 1

Type Food
	Global List:TList = New TList
	Field x,y
		
	Function Create:Food(x,y)
		Local f:Food = New Food
		f.x=x
		f.y=y
		List.Addlast(f)
		Return f
	End Function
	
	Function UpdateAll()
		For Local f:Food = EachIn List
		f.Update()
		Next
	End Function 
	
	Method Render()
	SetColor 0,255,0
	DrawRect x,y,7,7
	SetColor 255,255,255
	
	End Method
	
	Method Update()
	
	Render()
	
	Local Collided:Object[], CollidedObj:Object, G:Gog
		
	Collided = CollideRect(X,Y,7,7,FoodLayer,0,Self)
	
	For CollidedObj = EachIn Collided
	If Gog(CollidedObj)
		Print 1
		G = Gog(CollidedObj)
		G.Energy:+10
		Food.List.Remove(Self)
		Exit
	End If
	Next
	
	End Method
	
End Type

Type Gog
	Field x,y
	Field Energy
	
	Function Create:gog(x,y)
		Local g:gog = New gog
		g.x=x
		g.y=y
		Return g
	End Function
	
	Method Update()
	
	DrawRect(x,y,7,7)
	DrawText Energy,x+10,y+10
	CollideRect(x,y,7,7,0,FoodLayer,Self)
	
	End Method
End Type

Graphics 800,600,0,-1



For Local I:Int = 0 To 30 
	Food.Create(Rnd(0,800),Rnd(0,600))
Next

Local G:Gog = Gog.Create(MouseX(),MouseY())

While Not KeyHit(key_escape)

ResetCollisions()

G.X = MouseX()
G.Y = MouseY()

G.Update()
Food.UpdateAll()



Flip
Cls
Wend


I would guess you haven't set the WriteMAsk of the gog to the Foodlayer.


MattVonFat(Posted 2006) [#3]
Thanks. I was adding them to the FoodLayer when I crerated them but when I used CollideImage in the way you have it worked perfectly!

Thanks very much!