Am making a simple flash game and am trying to keep the avatar from hitting walls in a given level, here is the code I wrote, its simple enough
var hitWall:Boolean = checkWallHitlvl1();
if ( downKeyIsBeingPressed )
{
avatar.moveABit( 0, 1 );
if(hitWall)
{
avatar.moveABit( 0, -5);
}
}
else if ( upKeyIsBeingPressed )
{
avatar.moveABit( 0, -1 );
if(hitWall)
{
avatar.moveABit( 0, 5 );
}
}
else if ( leftKeyIsBeingPressed )
{
avatar.moveABit( -1, 0 );
if(hitWall)
{
avatar.moveABit( 5, 0 );
}
}
else if ( rightKeyIsBeingPressed )
{
avatar.moveABit( 1, 0 );
if(hitWall)
{
avatar.moveABit( -5, 0 );
}
}
hitWall checks if the avatar hits the level’s walls, the problem with this code is that even if for say I hit a wall from the right, and am trying to move left, I cant since hitWall doesn’t check from where the wall is hit, my question is is there a way to make this work without actually needing to figure out from which direction the player hits a given wall, i tried moving the avatar automatically to the left if he hits it from the right, but it didn’t quite work out so well…
any suggestions please?
thanx =D
EDIT1: i edited the code, it works now, but it looks kinda jerky since i have to step backwards 5 pixels, I cannot step backwards 1 pixel unfortunately since, since flash’s hitTestObject still returns true (checkWallHitLvl1() basically uses hitTestObject to check if the player hits the wall), any suggestions?
EDIT2: i found a way to fix it, just need to run some more tests and make sure it works fine, if they all pass i will post the code up, basically I just used 4 boolean variables to check which direction i hit the wall, and went from there…=D
What I do is keep the position of when it is not hit.