I’m certain there are better, more concise ways of doing this, but this is how I’ve done it in the past. Until someone comes along and shows you a more concise way, this will handle things for you.
essentially, you are handling which direction to reverse by deciding whether it is moving more vertically or horizontally at the time of the crash. what you need to do instead is figure out whether you hit a vertical or horizontal wall.
if (collide("deflector", x, y))
{
//first, store a reference to the entity we collided with
var deflector:Entity = collide("deflector", x, y);
/**
* next reset our position back to where it was before we collided
* this helps to fix a lot of bugs with fast moving bullets
* let me know if this doesn't make sense
*/
x = lastGoodX;
y = lastGoodY;
/**
* the case with squares is very simple,
* if the wall you strike is vertical, reverse the
* x movement, if it is horizontal,
* then reverse the y movement.
*/
if (collideWithVertical(x, v.x, deflector)){//if we hit a vertical wall
v.x *= -1; //reverse x direction
} else {//else we must have hit a horizontal wall
v.y *= -1;//so reverse the y direction
}
}
private function collideWithVertical(xPos:Number, xDir:Number, deflector:Entity)
{
if (xDir > 0) {//if we are moving to the right..
//then if we haven't yet passed the left edge,
//we must have hit a vertical wall
return x < deflector.left;
} else if (xDir < 0) {//if we are moving to the left...
//then if we havent yet passed the right edge,
//we must have hit a vertical wall.
return x > deflector.right;
}
//if we aren't moving left or right,
//then we must have hit a horizontal wall.
else return false;
}