Ok so I've been looking at 2d threads, saying to draw the sprite while a collision check is happening, and to store old locations then moving it back to the old location if a collision has happened, and to do this before the drawing of a sprite. I need to be able to slide around the wall too.
Why doesn't the below code work?
Rem Project: SpriteCollision
Rem Created: Wednesday, November 30, 2011
Rem ***** Main Source File *****
Type Graphics
id as Integer
img as Integer
xpos as Integer
ypos as Integer
oldxpos as Integer
oldypos as Integer
EndType
// Define Variables
Global Player as Graphics
Global Wall as Graphics
Global NotCollided = 0
// Variables Initialized
Player.id = 2 : Player.img = 2
Player.xpos = Screen Width() / 2 : Player.ypos = Screen Height() / 2
Wall.id = 1 : Wall.img = 1
Wall.xpos = 400 : Wall.ypos = 200
Wall.oldxpos = 400 : Wall.oldypos = 200
// Load Data
Load Image "player.png", Player.img
Load Image "wall.png", Wall.img
Draw Sprites First
// Sprite Settings - Offset to Center
Sprite Player.id, Player.xpos, Player.ypos, Player.img
Offset Sprite Player.id, Sprite Width(Player.id) / 2, Sprite Height(Player.id) / 2
Sprite Wall.id, Wall.xpos, Wall.ypos, Wall.img
Offset Sprite Wall.id, Sprite Width(Wall.id) / 2, Sprite Height(Wall.id) / 2
// Setup Code
Sync On : Sync Rate 60 : Autocam Off : Backdrop Off
Do
UpdateWorld()
CollisionWorld()
Text 20, 20, "OldX: " + Str$(wall.oldxpos) + " OldY: " + Str$(wall.oldypos)
Text 20, 40, "X: " + Str$(wall.xpos) + " Y: " + Str$(wall.ypos)
Sync
Loop
Function UpdateWorld()
// move the entire world instead of the player
// store old sprite locations before moving - used for collision
If Upkey()
wall.oldypos = Sprite Y(wall.id)
Wall.ypos = Sprite Y(Wall.id) + 1
endif
If Downkey()
wall.oldypos = Sprite Y(wall.id)
Wall.ypos = Sprite Y(Wall.id) - 1
endif
If Leftkey()
wall.oldxpos = Sprite X(wall.id)
Wall.xpos = Sprite X(Wall.id) + 1
Endif
If Rightkey()
wall.oldxpos = Sprite X(wall.id)
Wall.xpos = Sprite X(Wall.id) - 1
endif
EndFunction
Function CollisionWorld()
// If I haven't collided, draw the sprite normally.
If Sprite Collision( player.id , wall.id) = 0
// Draw Sprite
Sprite Wall.id, Wall.xpos, Wall.ypos, Wall.img
Else
// If I have collided, move it back to the old locations before drawing the sprite
If Sprite Collision( player.id , wall.id) = 1
// Draw Sprite
Wall.xpos = Wall.oldxpos
Wall.ypos = Wall.oldypos
Sprite Wall.id, Wall.xpos, Wall.ypos, Wall.img
EndIf
EndIf
EndFunction
Thanks for the help