I am having a little trouble with my collision. My player disappears when you walk on the ground. Any help is appreciated.
Rem ***** Main Source File *****
Sync On : Sync rate 30
set display mode 640, 480, 32
gosub setup
gosub mapSetup
Do
While game = 1
gosub player_Control
sync
Endwhile
Loop
setup: ///////////////Main setup
game as Integer
jump as Integer
playerSpr as Integer
playerXPos# as Float
playerYPos# as Float
oldXPos# as Float
oldYPos# as Float
grav# as Float
chktimer as Integer
fall As integer
tileNum as Integer
Dim map(20,15)
game = 1
playerSpr = 9999
grav# = 1.2
tileNum = 1
return ///////////////End Main setup
mapSetup: ////////////Map
Box 0, 0, 32, 32, rgb(0, 100, 100), rgb(0, 100, 0), rgb(0, 100, 100), rgb(0, 100, 0)
Get Image 1, 0, 0, 32, 32
Box 33, 0, 65, 64, rgb(100,0,0), rgb(120,0,0), rgb(100, 0,0), rgb(120,0,0)
Get image 2, 33, 0, 65, 64
For y = 1 to 15
For x = 1 to 20
Read A
If A = 1
Sprite tileNum, groundX, groundY, 1
map(x, y) = 1
inc tileNum
EndIf
If A = 2
Sprite playerSpr, groundX, groundY, 2
EndIf
groundX = groundX + 32
Next x
groundX = 0
groundY = groundY + 32
Next y
return ///////////////End of map setup
player_Control: ////////////Player controller
playerXPos# = sprite x(playerSpr)
playerYPos# = sprite y(playerSpr)
oldXPos# = sprite x(playerSpr)
oldYPos# = sprite y(playerSpr)
If keystate(32) = 1
inc playerXPos#, 2.0
endIf
If keystate(30) = 1
dec playerXPos#, 1.5
endIf
playerYPos# = playerYPos# + 1
//////////////////////////////////////////////////////////////Collision
for c = 1 to tileNum - 1
fall = scanY(playerSpr, c)
If sprite collision(playerSpr, c)
playerXPos# = scanX(playerSpr, c, oldXPos#)
endIf
If fall = 0
playerYPos# = oldYPos# - 1
EndIf
Next c
////////////////////////////////////////////////////////////////////////
sprite playerSpr, playerXPos#, playerYPos#, 2
return //////////////End player control
function scanY(player, tile)//////////////////////////////// scanY
falling as integer
playerTop = sprite y(player)
playerBase = findSprBase(player)
tileTop = sprite y(tile)
tileBase = findSprBase(tile)
falling = 1
If sprite collision(player, tile)
If playerTop <= tileBase
If playerBase - 6 >= tileBase
falling = 1
endIf
EndIf
If playerBase >= tileTop
If playerTop + 6 <= tileTop
falling = 0
EndIf
EndIf
endIf
endFunction falling
function scanX(spr, tile, sprx)////////////////////////////// scanX
playerwidth = sprite x(spr) + sprite width(spr)
tilewidth = sprite x(tile) + sprite width(tile)
playerBase = findSprBase(spr)
tileBase = findSprBase(tile)
//Right Collision
If playerwidth >= sprite x(tile)
If sprite x(spr) <= sprite x(tile) - 6
newX = sprx
endIf
EndIf
//Left Collision
If sprite x(spr) <= (tilewidth)
If playerwidth >= tilewidth + 6
newX = sprx
EndIf
EndIf
endfunction newX
function findSprBase(spr)
spriteBase = sprite y(spr) + sprite height(spr)
endfunction spriteBase
//Map Setup\\
//Will load from txt files later.\\
Data 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1
Data 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1
Data 1,0,0,2,0,0,0,0,1,0,1,0,0,0,0,0,0,0,0,1
Data 1,0,0,0,0,0,0,1,0,1,0,1,0,0,0,0,0,0,0,1
Data 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1
Data 1,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,0,1
Data 1,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,1
Data 1,0,1,1,1,1,0,0,0,0,0,1,0,0,0,0,0,0,0,1
Data 1,0,0,0,0,0,1,0,0,0,1,0,0,0,0,0,0,0,0,1
Data 1,0,0,0,0,0,0,1,1,1,0,0,0,0,0,0,0,0,0,0
Data 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
Data 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1
Data 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1
Data 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1
Data 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1