PDA

View Full Version : bounce



mario_hater
February 14th, 2007, 02:26 PM
i have this code, it controls the player and when the player hits the wall, they are supposed to bounce off it. However it is possible to go through the wall if you go close to the wall, turn around and go forward, it drags you through the wall. here is the code:

speed = 0;
xspeed = 0;
yspeed = 0;
onEnterFrame = function () {
player._x += xspeed;
player._y -= yspeed;
xspeed = Math.sin(Math.PI/180*player._rotation)*speed;
yspeed = Math.cos(Math.PI/180*player._rotation)*speed;
if (Key.isDown(Key.LEFT)) {
player._rotation -= 10;
}
if (Key.isDown(Key.RIGHT)) {
player._rotation += 10;
}
if (Key.isDown(Key.UP)) {
if (speed<20) {
speed += 2;
}
}
if (speed>0) {
speed -= 1;
}
if (speed<0) {
speed += 1;
}
if (wall.hitTest(player._x, player._y, true)) {
speed *= -1;
xspeed *= -1;
yspeed *= -1;
}
};
does anyone know how to fix this?

SacrificialLamb
February 14th, 2007, 11:04 PM
I think it has some thing to do with the center point of the MC. but see if this work's

onEnterFrame = function () {
xspeed = Math.sin(Math.PI/180*player._rotation)*speed;
yspeed = Math.cos(Math.PI/180*player._rotation)*speed;
if (Key.isDown(Key.LEFT)) {
player._rotation -= 10;
}
if (Key.isDown(Key.RIGHT)) {
player._rotation += 10;
}
if (Key.isDown(Key.UP)) {
if (speed<20) {
speed += 2;
}
}
if (speed>0) {
speed -= 1;
}
if (speed<0) {
speed += 1;
}
if (wall.hitTest(player._x+xspeed , player._y-yspeed, true)) {
speed *= -1;
xspeed *= -1;
yspeed *= -1;
} else {
player._x += xspeed;
player._y -= yspeed;
}
};
I added the xspeed and yspeed to the x and y of the hitTest so it test's the position that it will be at if it moves, I also added the moving

mario_hater
February 15th, 2007, 04:10 AM
thanks