| Function CollideImage:Object[](image:TImage,x,y,frame,collidemask%,writemask%,id:Object=Null) |
| Graphics 640,480 Local URL:String="http::www.2dgamecreators.com/tutorials/gameprogramming/basic/" Local Player:TImage=LoadImage(LoadBank(URL+"blobship_1-1.png")) Local Alien:TImage=LoadAnimImage(LoadBank(URL+"exp1.png"),64,64,0,16) Local Frame:Int=0 Local AnimDelay:int=10 Local PlayerSize:Int=1 Local AlienSize:Int=2 While Not KeyHit(key_escape) Or AppTerminate() Cls ResetCollisions() SetScale AlienSize,AlienSize DrawImage Alien,150,100,Frame CollideImage(Alien,150,100,Frame,0,2) SetScale PlayerSize,PlayerSize DrawImage Player,MouseX(),MouseY() If CollideImage(Player,MouseX(),MouseY(),0,2,0) SetClsColor 255,0,0 Else SetClsColor 0,0,0 EndIf Flip If AnimDelay<0 Then Frame :+ 1 If Frame>15 Then Frame=0 AnimDelay=10 EndIf AnimDelay :- 1 Wend End |
| ResetCollisions() |
| CollideImage(Alien,150,100,Frame,0,2) |
| If CollideImage(Player,MouseX(),MouseY(),0,2,0) |
| The basic idea is that the collidemask and writemask parameters are
bitmasks (eg: 1,2,4,8,16 etc) giving you up to 32 'layers' to write/collide
to/with. For example, you could use the following
layers: background=0, monsters=1, pickups=2, player=3. The bitmasks for these would be 1,2,4,8 respectively (bitmask=1 Shl layer). Using a bitmask instead of just a layer number allows you to specify multiple layers, eg: bitmask 3 would mean both layer 0 and layer 1. CollideImage simultaneously writes an image to 0-32 layers (depending on the writemask) and checks for collisions with 0-32 layers (dependingon the collidemask). By 'writes an image', all it really does internally is store the image, and current rot/scale etc. However, you can think of it as physically 'writing' the image. |
| Function CollideImage:Object[](image:TImage,x,y,frame,collidemask%,writemask%,id:Object=Null) |
| CollideImage(Alien,150,100,Frame,0,1) |
| CollideImage(Alien,150,100,Frame,0,3) |
| If CollideImage(Player,MouseX(),MouseY(),0,1,0) |
| If CollideImage(Player,MouseX(),MouseY(),0,3,0) |
| Function CollideImage:Object[](image:TImage,x,y,frame,collidemask%,writemask%,id:Object=Null) |
| Strict Graphics 640,480 AutoMidHandle True Local URL:String="http::www.2dgamecreators.com/tutorials/gameprogramming/basic/" Local SpaceShip:TImage=LoadImage(LoadBank(URL+"blobship_1-1.png")) Local AlienShip1:TImage=LoadImage(LoadBank(URL+"cartoonufo_1-1.png")) Local AlienShip2:TImage=LoadImage(LoadBank(URL+"cartoonufo_1-1.png")) Repeat Cls Local R:Int=0 Local G:Int=0 Local B:Int=255 ResetCollisions() SetColor 255,255,255 DrawImage AlienShip1, 250,100 CollideImage(AlienShip1,250,100,0,0,1,AlienShip1) DrawImage AlienShip2, 400,100 CollideImage(AlienShip2,400,100,0,0,1,AlienShip2) Local p:Object[]=CollideImage(Spaceship,MouseX(),MouseY(),0,1,0) For Local i:TImage=EachIn p Select i Case AlienShip1 R=255 Case AlienShip2 G=255 End Select Next SetColor R,G,B DrawImage SpaceShip,MouseX(),MouseY() Flip Until KeyDown(KEY_ESCAPE) Or AppTerminate() |
![]() |
![]() |
| DrawImage AlienShip1, 250,100 CollideImage(AlienShip1,250,100,0,0,1,Null) |
| Strict Graphics 640,480 AutoMidHandle True Local URL:String="http::www.2dgamecreators.com/tutorials/gameprogramming/basic/" Local SpaceShip:TImage=LoadImage(LoadBank(URL+"blobship_1-1.png")) Local AlienShip1:TImage=LoadImage(LoadBank(URL+"cartoonufo_1-1.png")) Local AlienShip2:TImage=LoadImage(LoadBank(URL+"cartoonufo_1-1.png")) Repeat Cls Local R:Int=0 Local G:Int=0 Local B:Int=255 ResetCollisions(3) SetColor 255,255,255 DrawImage AlienShip1, 250,100 CollideImage(AlienShip1,250,100,0,0,1,AlienShip1) DrawImage AlienShip2, 400,100 CollideImage(AlienShip2,400,100,0,0,2,AlienShip2) Local Collision_Layer:int=2 Local p:Object[]=CollideImage(Spaceship,MouseX(),MouseY(),0,Collision_Layer,0) For Local i:TImage=EachIn p Select i Case AlienShip1 R=255 Case AlienShip2 G=255 End Select Next SetColor R,G,B DrawImage SpaceShip,MouseX(),MouseY() Flip Until KeyDown(KEY_ESCAPE) Or AppTerminate() |