Main Menu

getting the tile that was hit

Started by RaverDave, December 11, 2005, 03:23:53 AM

Previous topic - Next topic

RaverDave

I can only guess its because of the size difference between the object and the target sprites?? Though really i just dont kow!

kevin

#16
Yes, that's pretty the much the problem.  The first solution didn't take into account the size of the ball.


(I hope this cut and pastes correctly this time..)




PlayBASIC Code: [Select]
; OpenScreen 640,480,32,2
;SetFPS 0
;Mouse Off
;ScreenVsync On

PlayerX=50
PlayerY=430
PlayerWidth =10
PlayerHeight =10
Speed=4
MyMap = GetFreeMap()
CreateMap MyMap, 1

TileWidth=32
TileHeight=16
Number_of_tiles = 5

; Loop through and draw a series of randomly coloured Boxes to the screen
For lp=1 To Number_of_tiles
xpos=lp*tileWidth
BoxC Xpos,0,Xpos+TileWidth,TileHeight,1,RndRGB()
Next

; Grab the box graphics that we've just drawn as image.
TempImage=GetFreeImage()
GetImage TempImage,0,0,xpos+TIleWidth,Tileheight



RemStart
So far, we've created our Map and Manually Created an image containing some
coloured blocks. Now it's time to import this image into our Map for use.

When we have map block graphics stored in an image, we import them using
the MakeMapGFX command. This command requires the Map you wish to store these
blocks in, the source Image to import, the Tile Width, the Tile Height, number
of tiles to import and finally the transparent colour (if any) to use for
these tiles.
RemEnd


; Import this image into our previously defined Map.
MakeMapGFX MyMAP,TempImage,TileWidth,TileHeight,Number_Of_Tiles,RGB(0,0,0)



RemStart
Now we have a Map that has a set of tiles ready for use in it, so the next
thing we need to is create a level. This is done with the CREATELEVEL command.
Which requires two things, it needs the know within what MAP this level should
be created, and what Level Index it should for this level.


RemEnd


; Create level 1 in MyMAP, 100 tile across and 200 tiles down.
CreateLevel MyMap,1, 100,100



For Ylp=0 To 12
For Xlp=0 To 19
t=ReadData()
If t>0
PokeLevelTile MyMap,1,Xlp,ylp,t
EndIf
Next xlp
Next ylp

Data 1,1,1,0,1,0,0,0,1,1,1,0,1,0,1,0,0,0,0,0
Data 1,0,1,0,1,0,0,0,1,0,1,0,1,0,1,0,0,0,0,0
Data 1,1,1,0,1,0,0,0,1,1,1,0,0,1,0,0,0,0,0,0
Data 1,0,0,0,1,0,0,0,1,0,1,0,0,1,0,0,0,0,0,1
Data 1,0,0,0,1,1,1,0,1,0,1,0,0,1,0,0,0,0,0,1
Data 0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,1
Data 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
Data 1,1,0,0,1,1,1,0,1,1,1,0,1,1,1,0,1,1,1,0
Data 1,0,1,0,1,0,1,0,1,0,0,0,0,1,0,0,1,0,0,0
Data 1,1,0,0,1,1,1,0,1,1,1,0,0,1,0,0,1,0,0,0
Data 1,0,1,0,1,0,1,0,0,0,1,0,0,1,0,0,1,0,0,0
Data 1,0,1,0,1,0,1,0,0,0,1,0,0,1,0,0,1,0,0,0
Data 1,1,0,0,1,0,1,0,1,1,1,0,1,1,1,0,1,1,1,0
; Draw the Level to the screen at Xpos 0 and Ypos 50


; Show the user te screen and wait for a key to be pressed
Repeat

; cls rgb(0,100,200)
setcursor 0,0

DrawMap MyMap,1,0,0

PLayerX,PLayerY=Handle_player(PLayerX,PLayerY,PLayerWIdth,PlayerHeight,Speed,MyMap,1)

X2=playerX+PlayerWidth
Y2=playerY+PlayerHeight
Box playerX,playerY,x2,y2,1


Rem Circle playerX,playerY,10,1
v=ScanCode()
If v=16
quit=1
endif

Sync
Until quit=1





Function Handle_player(PLayerX,PLayerY,WIdth,Height,Speed,ThisMap,ThisLevel)

; Store the players possible new position
NewX=PlayerX
NewY=PlayerY

; Handle the players Movement
If UpKey() Then NewY=PLayerY-Speed
If DownKey() Then NewY=PLayerY+Speed
If LeftKey() Then NewX=PLayerX-Speed
If RightKey() Then NewX=PLayerX+Speed

; ==========================
; Handle Collision
; ===========================
If CheckMapImpact(thisMap,ThisLevel,PlayerX,playerY,NewX,NewY,Width,Height)

; If there was impact with the map, then read our
; new position back.
PlayerX=GetMapImpactX()
PlayerY=GetMapImpactY()

TileWidth=GetMapBlockWidth(ThisMap)
TileHeight=GetMapBlockHeight(ThisMap)

TileX=PlayerX/TileWidth
TileY=PlayerY/Tileheight

; Check what side the impact occured upon
; and display a message

If GetMapImpactLeft()
Print "Impact occured on the LEFT side"

; Kill Tile to the left of our position
Login required to view complete source code