We have reduced support for legacy browsers.

What does this mean for me? You will always be able to play your favorite games on Kongregate. However, certain site features may suddenly stop working and leave you with a severely degraded experience.

What should I do? We strongly urge all our users to upgrade to modern browsers for a better experience and improved security.

[AS2] Keeping MovieClip within Bounds

So, yeah. I have 4 walls and currently have the following code:
`
for (i = 1; i < 5; i++) {
var currWall = this["wall" + i];
if (player.hitTest(currWall)) {
xSpeed *= -1
ySpeed *= -1
}
}
`
The thing is, this causes two problems.
1. I want it to bounce at a 90 degree to the wall, currently it bounces at 180.
2. When it hits the joint of 2 walls, it goes straight through, this is obviously because -1 \* -1 = 1, So it’ll continue going in the original direction.
So, how do I make it work? I know i’m missing something obvious, but oh well.
Thanks in Advance.

If the game is really simple and you only plan on having 4 walls, you could take the easy way and just hard code them by calling them different things. For example, “hWall1” and “hWall2” only bounce ySpeed, and “vWall1” an “vWall2” only bounce xSpeed.
But if that’s the case you may as well just hard code in x and y values for the boundaries and use basic checks to see if they’ve gone past a boundary and need to be bounced.
For a more general setup with different wall arrangements, the best way that comes to mind would be to record the previous position the player was in and use that to work out which way he should bounce. You could just use x/y coordinates, but using the edges of the bounding rectangles will be more accurate.
To avoid the wall joints double-bouncing, you can explicitly say which direction they go based on whether they were left/right, or above/below each wall.
`
function move() {
var playerBounds = player.getBounds(this);
player._x += xSpeed;
player._y += ySpeed;
for (i = 1; i < 5; i++) {
var currWall = this["wall" + i];
if (player.hitTest(currWall)) {
var wallBounds = currWall.getBounds(this);
if (playerBounds.xMax < wallBounds.xMin) {
xSpeed = -Math.abs(xSpeed);
} else if (playerBounds.xMin > wallBounds.xMax) {
xSpeed = Math.abs(xSpeed);
}
if (playerBounds.yMax < wallBounds.yMin) {
ySpeed = -Math.abs(ySpeed);
} else if (playerBounds.yMin > wallBounds.yMax) {
ySpeed = Math.abs(ySpeed);
}
}
}
}
`