imagescollide2

BlitzMax Forums/BlitzMax Beginners Area/imagescollide2

Strict 
SetMaskColor 0,0,0
Global Number_of_ships = 10
Global My_Image:Timage=LoadImage( "ship.bmp" )
MidHandleImage( My_image )

Type ship 
Field X#, Y#
Field Dir
Field Armor = 100
Field Speed# = 0.2
Field red
Field green
Field blue

Method draw()
SetColor (Self.red,self.green,self.blue)
SetRotation (Self.dir+90)
DrawImage( my_image,self.X#, self.Y#)
End Method

Method update()
self.X:+ self.Speed*Cos( self.Dir )
self.Y:+ self.Speed*Sin( self.Dir )
For Local theship:ship = EachIn collisionList
If ImagesCollide2(my_image,self.x,self.y,1,self.dir+90,1,1,my_image,theship.x,theship.y,1,theship.dir+90,1,1)
RuntimeError "collided"
Self.red=255
self.green=0
self.blue=0
EndIf 
Next
End Method

End Type


Graphics 800,600,0
Global shipList:TList = CreateList()
Global collisionlist:tlist=CreateList()
For Local N = 1 To Number_of_ships 
Local theship:ship 


theship = New ship
theship.Armor = Rand( 5 )*10 + 150
theship.X = Rand( 5, 800 ) ;
theship.Y = Rand( 5, 600 )
theship.Dir = Rand( 0, 360 )
theship.green=255
theship.red=255
theship.blue=255

ListAddLast( shipList, theship )
Next
While Not KeyDown(Key_Escape)

For Local theship:ship = EachIn shipList
theship.draw()
theship.update()
Next

SetRotation 0
DrawText "Number of ships : "+shipList.Count(), 20, 20
DrawText "Press ESC to exit", 20, 40

Flip ;Cls
Wend


cant get imagescollide2 to return positive

can anyone help?

also how would I delete the type instances of the ships that collide?

how do you check for collisions of images in the same list?
and then delete their types

just tho i'd say your checking for collisions in the wrong list. You are going through a collisionList when you should be going through shipList - have you tried putting a End after
For Local theship:ship = EachIn collisionList
if you have it wont end because the list has nothing in it for it to loop through.
When you do get it to collide you can just do somthing like:
shipList.remove(self)
shipList.remove(theship)


oh and make sure you dont do a check on the current ship by putting somthing like:
if theship <> self then
when you loop through all the ships when you call update.

thanks, I tried to loop through the ships in the update method. Using the original list. (Collision list doesnt exist anymore)

then it says 'attempt to index array element beyond array length'

but I dont think I am using any arrays
??


Strict 
SetMaskColor 0,0,0
Global Number_of_ships = 10
Global My_Image:Timage=LoadImage( "ship.bmp" )
MidHandleImage( My_image )
Global shipList:TList = CreateList()

Type ship 
Field X#, Y#
Field Dir
Field Armor = 100
Field Speed# = 0.2
Field red
Field green
Field blue

Method draw()
SetColor (Self.red,self.green,self.blue)
SetRotation (Self.dir+90)
DrawImage( my_image,self.X#, self.Y#)
End Method

Method update()
self.X:+ self.Speed*Cos( self.Dir )
self.Y:+ self.Speed*Sin( self.Dir )
For Local shipy:ship = EachIn shipList
If ImagesCollide2(my_image,self.x,self.y,1,self.dir+90,1,1,my_image,shipy.x,shipy.y,1,shipy.dir+90,1,1)
RuntimeError "collided"
Self.red=255
self.green=0
self.blue=0
EndIf 
Next
End Method

End Type


Graphics 800,600,0

For Local N = 1 To Number_of_ships 
Local theship:ship 


theship = New ship
theship.Armor = Rand( 5 )*10 + 150
theship.X = Rand( 5, 800 ) ;
theship.Y = Rand( 5, 600 )
theship.Dir = Rand( 0, 360 )
theship.green=255
theship.red=255
theship.blue=255

ListAddLast( shipList, theship )

Next
While Not KeyDown(Key_Escape)

For Local theship:ship = EachIn shipList
theship.draw()
theship.update()
Next

SetRotation 0
DrawText "Number of ships : "+shipList.Count(), 20, 20
DrawText "Press ESC to exit", 20, 40

Flip 
Cls
Wend


you are trying to check for frame 1 change it to 0
For Local shipy:ship = EachIn shipList
If shipy <> self then
if ImagesCollide2(my_image,x,y,0,dir+90,1,1,my_image,shipy.x,shipy.y,0,shipy.dir+90,1,1)
RuntimeError "collided"
Self.red=255
self.green=0
self.blue=0
EndIf 
endif
Next


ok heres what I have so far
Strict 
SetMaskColor 0,0,0
Global Number_of_ships = 10
Global My_Image:Timage=LoadImage( "ship.bmp" )
MidHandleImage( My_image )
Global shipList:TList = CreateList()

Type ship 
Field X#, Y#
Field Dir
Field Armor = 100
Field Speed# = 0.2
Field red
Field green
Field blue

Method draw()
SetColor (Self.red,self.green,self.blue)
SetRotation (Self.dir+90)
DrawImage( my_image,self.X#, self.Y#)
End Method

Method update()
self.X:+ self.Speed*Cos( self.Dir )
self.Y:+ self.Speed*Sin( self.Dir )
For Local shipy:ship = EachIn shipList
If shipy <> Self Then
If ImagesCollide2(my_image,x,y,0,dir+90,1,1,my_image,shipy.x,shipy.y,0,shipy.dir+90,1,1)
RuntimeError "collided"
Self.red=255
self.green=0
self.blue=0
EndIf 
EndIf
Next

End Method

End Type


Graphics 800,600,0

For Local N = 1 To Number_of_ships 
Local theship:ship 


theship = New ship
theship.Armor = Rand( 5 )*10 + 150
theship.X = Rand( 5, 800 ) ;
theship.Y = Rand( 5, 600 )
theship.Dir = Rand( 0, 360 )
theship.green=255
theship.red=255
theship.blue=255

ListAddLast( shipList, theship )

Next
While Not KeyDown(Key_Escape)

For Local theship:ship = EachIn shipList
theship.draw()
theship.update()
Next

SetRotation 0
DrawText "Number of ships : "+shipList.Count(), 20, 20
DrawText "Press ESC to exit", 20, 40

Flip 
Cls
Wend

still no collisions though
also my ship image isnt being masked at all

You're loading your graphics before the Graphics command which causes odd problems.
You also need to change the colour back to 255,255,255 if the collision has ended...
Strict 
Type ship 
Field X#, Y#
Field Dir
Field Armor = 100
Field Speed# = 0.2
Field red
Field green
Field blue

Method draw()
SetColor (Self.red,self.green,self.blue)
SetRotation (Self.dir+90)
DrawImage( my_image,self.X#, self.Y#)
End Method

Method update()
self.X:+ self.Speed*Cos( self.Dir )
self.Y:+ self.Speed*Sin( self.Dir )
For Local shipy:ship = EachIn shipList
	If shipy <> Self Then
		If ImagesCollide2(my_image,x,y,0,dir+90,1,1,my_image,shipy.x,shipy.y,0,shipy.dir+90,1,1)
			Self.red=255
			self.green=0
			self.blue=0
			shipy.red=255
			shipy.green=0
			shipy.blue=0			 
	      Else
	             self.red=255
	             self.green=255
	             Self.blue=255
	   	EndIf 
	EndIf
Next

End Method

End Type


Graphics 800,600,0
SetMaskColor 0,0,0
Global Number_of_ships = 10
Global My_Image:Timage=LoadImage( "ship.png" )
MidHandleImage( My_image )
Global shipList:TList = CreateList()



For Local N = 1 To Number_of_ships 
Local theship:ship 


theship = New ship
theship.Armor = Rand( 5 )*10 + 150
theship.X = Rand( 5, 800 ) ;
theship.Y = Rand( 5, 600 )
theship.Dir = Rand( 0, 360 )
theship.green=255
theship.red=255
theship.blue=255

ListAddLast( shipList, theship )

Next
While Not KeyDown(Key_Escape)

For Local theship:ship = EachIn shipList
theship.draw()
theship.update()
Next

SetRotation 0
DrawText "Number of ships : "+shipList.Count(), 20, 20
DrawText "Press ESC to exit", 20, 40

Flip 
Cls
Wend



thanks it works perfectly now,