Collisions between types?

BlitzMax Forums/BlitzMax Beginners Area/Collisions between types?

Azan(Posted 2006) [#1]
Hi, just bought Blitzmax and I love it... however I am having some trouble getting used to how Blitz3d did types... I am at a point where I want to see if an enemy ship is hit by a bullet, in blitz3d you could do something like this...

for s.ship = each ship
   for b.bullet = each bullet
      ;check for collisions
   next
next


But I am having trouble doing that in blitzmax, here is my header file with the enemy/bullet types...

Type bullet
	Field x#,y#
	Field xDir#, yDir#

	Field life

	Global bulletList:TList 'list that holds the emitters
	
	Method free()
		bulletList.remove(Self)
	End Method
	
	Method update()
		Local r = Rand(1,1)
		If r = 1 And life > 0 Then
			particle.create(x,y,0,155,255,0.1,60,0,0, 0, 5, -5)
			'particle.create(x,y,0,155,255,0.1,70,0,0, 0, 5, -5)
			'particle.create(x,y,155,0,255,0.01,70,0,0, 5, 0, -5)
		EndIf
		If Not life = 0 Then
			x# = x# +xDir#*9
			y# = y# +yDir#*-9
			life = life - 1
		EndIf
		If life = 0 Then
			free()
		EndIf
		'DrawOval x - playerX,y - playerY,10,10
	End Method
	
	Function updateAll()
		If Not bulletList Then 'exit function if emitterList does not exist
			Return
		EndIf
		For Local b:bullet = EachIn bulletList
			b.update()
			'If e.name$ = "blueRed" Then
				'e.x = MouseX()
				'e.y = MouseY()
			'EndIf
		Next
	End Function
	
	Function create(x,y,xDir#,yDir#)
		Local b:bullet
		b = New bullet
			b.x = x
			b.y = y
			b.xDir# = xDir#
			b.yDir# = yDir#
			b.life = 600
	

			
			If Not bulletList Then 'if the list isnt created then make it
				bulletList = CreateList()
			EndIf
			ListAddLast(bulletList,b) 'add it to the list 
	End Function 
End Type

Type enemy
	Field x,y
	Field life
	Global enemyList:TList
	
	Method update()
		SetBlend MASKBLEND
		SetScale .6,.6
		DrawImage ship02,x- playerX,y- playerY
	
	End Method
	
	Method free()
		enemyList.remove(Self)
	End Method

	Function isHit()
		If Not bulletList Then 'exit function if emitterList does not exist
			Return
		EndIf

'		For Local b:bullet = EachIn bulletList
			'If ImagesCollide(spark:TImage,b.x,b.y,0,ship:TImage,e.x,e.y,0) Then
			'	enemy.free()
			'EndIf
'		Next
	End Function

	Function updateAll()
		If Not enemyList Then 'exit function if emitterList does not exist
			Return
		EndIf
		For Local e:enemy = EachIn enemyList
			e.update()
	
		Next
	End Function
	
	Function create(x,y)
		Local e:enemy
		e = New enemy
			e.x = x
			e.y = y
			e.life = 6
	

			
			If Not enemyList Then 'if the list isnt created then make it
				enemyList = CreateList()
			EndIf
			ListAddLast(enemyList,e) 'add it to the list 
	End Function 
End Type


As you can see in the enemy type I have a function called isHit, and I attempted to do what I would normally do in blitz, but when I uncomment that bullet eachIn loop I get an error "Foreach must be used with a string, array, or appropriate object" being a noob at OOP I was curious what the best way to implement this is, also is there anything in my code that can be change to make it more effecient?

Thanks, Brian.


H&K(Posted 2006) [#2]
Put the "Createlist ()" into the declareation of each list.
i.e.
	Field x,y
	Field life
	Global enemyList:TList = createList ()

And get rid of the if not list then bits.

(cannot test ATM, did that work?)Oh And put SuperStrict as first line

You need to change the name of each list to "list", and reffer to bulletlist as bullet.list (Or bullet.bulletlist if you dont change the names)


REDi(Posted 2006) [#3]
and...
For Local b:bullet = EachIn bullet.bulletList



H&K(Posted 2006) [#4]
I knew I should have posted that bit first ;)


Azan(Posted 2006) [#5]
Alright, thanks everyone. H&K, what you told me to do works just fine :) and what REDi posted allows me to check for the collisions. So thanks everyone.