'modification of code by Fredborg 'to illustrate increase speed with not having to check all objects for 'collision one object at a time ' Strict Graphics 640,480 'don't forget to set midhandle otherwise collision will 'not work properly with rotation AutoMidHandle True 'List holds the objects to be displayed Global ObjectList:TList=CreateList() Const COLLISION_LAYER=1 Type img Field image:timage Field x:Float,y:Float Field sx:Float,sy:Float Field r:Float,sr:Float Field Collided:Int=False Field CollisionList:String Field ID:Int Function Create:img(Image:TImage,j:Int) Local i:img = New img i.image = Image i.x = Rnd(20,620) i.y = Rnd(20,460) i.sx = Rnd(-3,3) i.sy = Rnd(-3,3) i.r = Rnd(0,360) i.sr = Rnd(-5,5) i.ID = j ObjectList.Addlast i Return i End Function Method UpdateState() x :+ sx y :+ sy r :+ sr If x>620 And sx>0 sx = -sx ElseIf x<20 And sx<0 sx = -sx EndIf If y>460 And sy>0 sy = -sy ElseIf y<20 And sy<0 sy = -sy EndIf SetBlend ALPHABLEND SetRotation r CollideImage(image,x,y,0,0,COLLISION_LAYER,Self) CheckCollision() End Method Method DrawSelf() SetRotation r If Collided=True Then SetColor 255,0,0 Else SetColor 255,255,255 DrawImage Image,x,y DrawText ID,x-3,y End Method Method CheckCollision() If Collided=False 'check only those not yet colliding Local p:Object[]=CollideImage(Image,x,y,0,COLLISION_LAYER,0) 'as everything including self is written to the collision layer 'we need to skip over a collision of 1 object ie colliding w self If p.length>1 Then For Local k:img=EachIn p k.collided=True Next EndIf EndIf EndMethod End Type Local URL:String="http::www.2dgamecreators.com/tutorials/gameprogramming/basic/" Global Image:TImage=LoadImage(LoadBank(URL+"blobship_1-1.png")) For Local j = 0 To 20 img.create(Image,j) Next Repeat Cls 'update all objects first For Local i:img = EachIn ObjectList i.UpdateState() Next 'then its time to draw them For Local i:img = EachIn ObjectList i.DrawSelf() Next If CollideRect(MouseX(),MouseY(),1,1,1,0) And MouseDown(1) Print "Collided" EndIf DrawRect MouseX(),MouseY(),2,2 Flip 'reset collision both in collision layer and also flag ResetCollisions(COLLISION_LAYER) For Local i:img = EachIn ObjectList i.Collided=False Next If KeyHit(KEY_A) Then 'pause to see whats happening Repeat Until KeyHit(KEY_S) EndIf Until KeyHit(KEY_ESCAPE) Or AppTerminate()