So I'm trying to work out some code for collision detection, but there's something wrong with it. It seems to only detect a collision between the 'player' and the last "tile" type placed/loaded onto the map, rather than any tile on the map. I honestly can't see what's going wrong. Can anyone tell me why this is happening?
Many thanks to anyone who can help me with this.
Edit: should I have put that in a codebox? :S
Many thanks to anyone who can help me with this.
;tiles test ;Sean Tomlin ; ;Last updated 13th June 07 ;;;;;;;;;;;;;;;;;;;;; ; ;Space to save map ;Comma to load map ;Left mouseclick = place tile ;Delete + mouseclick = delete tiles ;Right mouseclick + move mouse = move around map ;Up/Down = change tile ; ;;;;;;;;;;;;;;;;;;;;; Global tnumber = 0 Global maprx Global mapry Global maprmode Global ox = 0 Global oy = 0 Global gravity# = 0.01 Graphics 640,480,16,1 SetBuffer BackBuffer() ClsColor 134,156,175 ;load tileset Global tiles1=LoadAnimImage("C:/a/Co/Tiles/_A_TilesTest.bmp",16,16,0,16) MaskImage tiles1,255,0,255 Global px Global py Global pxspeed Global pyspeed Global pfalling=1 Global x Global y Global count Global tilecount ;;;;;;;;;;;;; While Not KeyHit(1) Cls Origin ox,oy UpdateMouse() If KeyHit(27) And tnumber<15 Then tnumber=tnumber+1 If KeyHit(26) And tnumber>0 Then tnumber=tnumber-1 If KeyHit(57) Then SaveMap() Text 10,290,"Save" EndIf If KeyHit(51) Then LoadMap() If KeyHit(14) Then ClearMap() UpdateTiles() UpdatePlayer() Text 10,10,x/16 Text 10,30,y/16 Text 10,180,maprx Text 10,200,mapry Text 10,220,maprmode Flip Wend ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; Type tile ;position within grid Field xnum Field ynum ;type of tile: ground, water or danger Field mode End Type ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; Function UpdateMouse() ;infiinite mouse movement xs=MouseXSpeed() ys=MouseYSpeed() MoveMouse 320,240 ;right mouse button (click + drag): move around map If MouseDown(2) Then ox=ox+xs oy=oy+ys Else x=x+xs y=y+ys EndIf ;draw the currently selected tile at mouse cursor DrawImage tiles1,x,y,tnumber Color 45,200,75 Rect x,y,16,16,0 ;LMB: place tile If MouseDown(1) Then If Not KeyDown(58) Then t.tile = New tile t\xnum = x/16 t\ynum = y/16 t\mode = tnumber Else ;4x tile if CAPS pressed also t.tile = New tile t\xnum = x/16 t\ynum = y/16 t\mode = tnumber t.tile = New tile t\xnum = (x/16)+1 t\ynum = y/16 t\mode = tnumber t.tile = New tile t\xnum = x/16 t\ynum = (y/16)+1 t\mode = tnumber t.tile = New tile t\xnum = (x/16)+1 t\ynum = (y/16)+1 t\mode = tnumber EndIf EndIf End Function ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; Function UpdateTiles() For t.tile = Each tile DrawImage tiles1,t\xnum*16,t\ynum*16,t\mode If ImageRectOverlap(tiles1,t\xnum*16,t\ynum*16,x,y,14,14) Then If KeyDown(211) Then Delete t Else EndIf If ImageRectOverlap(tiles1,t\xnum*16,t\ynum*16,px,py,16,26) Then pfalling = 0 Else pfalling = 1 EndIf Next End Function ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; Function UpdatePlayer() If KeyDown(205) Then If pxspeed<1.8 Then pxspeed=pxspeed+0.1 ElseIf pxspeed>0 Then pxspeed=pxspeed-0.1 EndIf If KeyDown(203) Then If pxspeed>-1.8 Then pxspeed=pxspeed-0.1 Else If pxspeed<0 Then pxspeed=pxspeed+0.1 EndIf px = x py = y ;If pfalling = 1 Then ; If pyspeed<0.5 Then pyspeed = pyspeed + 0.01 ;Else ; pyspeed = 0 ;EndIf ;py = py + pyspeed Color 30,200,130 Rect px,py,16,24 Text px+30,py,pfalling End Function ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; Function SaveMap() Color 20,20,75 mapname$="mapdata.txt" Count = 0 For t.tile = Each tile count = count + 1 Next mapout = WriteFile(mapname$) WriteInt(mapout,count) For t.tile = Each tile WriteInt(mapout,t\xnum) WriteInt(mapout,t\ynum) WriteInt(mapout,t\mode) Next CloseFile mapout End Function ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; Function LoadMap() Color 20,20,75 whichmap$="mapdata.txt" For t.tile = Each tile Delete t Next mapin = ReadFile(whichmap$) count = ReadInt(mapin) For tile = 1 To count t.tile = New tile t\xnum=ReadInt(mapin) t\ynum=ReadInt(mapin) t\mode=ReadInt(mapin) Next CloseFile(mapin) End Function Function ClearMap() For t.tile = Each tile Delete t Next End Function
Edit: should I have put that in a codebox? :S