hitTest - Bounce Direction

I’ll begin by posting a link to the swf in question. Steer using WASD keys, and don’t mind the bouncing at the start, just steer out of the box into the open.

http://img144.imageshack.us/my.php?image=bouncehelpar6.swf

You will notice that if you hit any of the walls of the flash, the ball will bounce off the wall. (unnaturaly, but I’m very content with this way).

You will also notice that you can bounce off the top and the bottom of the flash, as well as the black rectangle’s sides.

Now to the problem: Try bouncing off the bottom of the black rectangle. The ball will go “inside” the rectangle and be thrown off to one of the sides instead of bouncing down like it should.

If you try standing completely still (no left or right movement) under the rectangle and go upwards it will bounce like it should.



onClipEvent (load) {
    touching = false;
}
onClipEvent (enterFrame) {
    if (this.hitTest(_root.ball) && touching == false) {
        trace(touching);
        if (_root.ball.xdest>_root.ball._x && touching == false) {
            _root.ball.xdest -= 150;
        } else if (_root.ball.xdest<_root.ball._x && touching == false) {
            _root.ball.xdest += 150;
        } else if (_root.ball.ydest<_root.ball._y && touching == false) {
            _root.ball.ydest += 150;
        } else if (_root.ball.ydest>_root.ball._y && touching == false) {
            _root.ball.ydest -= 150;
        }
        touching = true;
        trace(touching);
    }
    if (!this.hitTest(_root.ball)) {
        touching = false;
    }
}

(yes, I’m a n00b at flash and actionscript as you can see by the very bad coding)

I know why this problem occurs, or at least I think I do. The left and right directions take “priority” over the up and down directions thanks to the way the ‘else ifs’ are positioned. Flash basicly says “aha the ball is moving to the left or right, bounce it in the oppisite direction” and ignores if it is going up or down.

So I know what causes the problem, but I got no clue how to redeem it. Help?

I don’t know if this helps you, but you’ll find a video tutorial about bouncing balls on http://codervods.com Cheers!

Thanks for the link, didn’t help a bit though unfortunatley.