So I wasn't sure how to word this question - if anyone has any better ideas, feel free :)
But essentially, my sidescroller's collisions are behaving oddly. I have a 'border' box (_boundaries
) to the left of the start position, to prevent characters from going left on start. Before, it was working fine (however other things were broke). After fixing the many other collision issues (IE falling through the floor, getting stuck in the floor, gravity behaving oddly) the left collisions has stopped working most of the time.
If you run into the wall from anymore than say 5-10 pixels, you will, rather than being blocked/prevented from moving, you will 'teleport' to the top of the wall and continue going. If you are closer than 5-10 pixels, and start moving, you will be blocked. I've supplied an image (via link) to what the layout is.
But how do I fix my sidescroller code so that the wall always blocks (without having a ridiculously huge collision box/zone). Alternatively, is there a better way in general to work all my collisions?
https://i.stack.imgur.com/lDU9M.png
Collision Code:
private function processCollisions():void
{
//Respawn if fell off stage
if(_player.y > stage.stageHeight)
{
_player.x = _startMarker.x;
_player.y = _startMarker.y;
_boundaries.x = 0;
_boundaries.y = 0;
_vy = 0;
}
//Otherwise process collisions with boundaries
else
{
var testX = _player.x
var testY = _player.y - (_player.height / 2);
var vCollision:Boolean = false;
if(moveLeft){
testX -= horizMove;
if(_boundaries.hitTestPoint((testX - _player.width / 2), _player.y-5, true))
{
moveLeft = false;
trace("PING");
}
}
if(moveRight){
testX += horizMove;
if(_boundaries.hitTestPoint((testX + _player.width / 2), _player.y-5, true))
{
moveRight = false;
trace("PONG");
}
}
if(jump){
testY += vertMove;
if(_boundaries.hitTestPoint(_player.x, (testY - _player.height / 2), true))
{
jump = false;
trace("PANG");
}
}
//if(_vy > 0){
testY += _vy;
if(_boundaries.hitTestPoint(_player.x, (testY + _player.height / 2), true))
{
vCollision = true;
}
if(vCollision)
{
while(vCollision)
{
_player.y -= 0.1;
vCollision = false;
if(_boundaries.hitTestPoint(_player.x, _player.y, true))
{
vCollision = true;
}
}
_vy = 0;
}
}
movement();
}
Movement Code:
private function movement():void
{
if(moveLeft)
{
_vx = -5;
faceLeft = true;
faceRight = false;
}
else if(moveRight)
{
_vx = 5;
faceLeft = false;
faceRight = true;
}
else if(jump)
{
//_player.y += vertMove;
_vy = -15;
}
}
Where it gets used:
private function enterFrameHandler(e:Event):void
{
//Upon entering a frame;
//Gravitate Player
_vy += 1.75;
//Move Player
_player.x += _vx;
_player.y += _vy;
//Process Collisions for Player
processCollisions();
//Scroll map
scrollStage();
}