Ball to bounce of an object

Hello!

I need help with a ball colliding correctly with a square on stage. This is the file: https://app.box.com/s/ui4q15juyp9iqszmm95g
It’s basically a ball bouncing of four walls and hitting 2 object incorrectly. Because there is a quite a bit of code relevant to the ball hitting the walls I am only posting the code for the object on the main timeline.

var ballXSpeed:Number = 8;//X Speed of the Ball
var ballYSpeed:Number = 8;//Y Speed of the Ball
mcBall.addEventListener(Event.ENTER_FRAME, moveBall);
function moveBall(event:Event):void {
mcBall.x += ballXSpeed;//Move the ball horizontally
mcBall.y += ballYSpeed;//Move the ball vertically

and inside the object (brick .as)

if (this.hitTestObject(_main.mcBall))
{
_main.ballYSpeed *= -1;

What code do I need to change inside brick .as to fix this?
Thanks!

If you’re struggling with writing your own collision physics, it might be a good idea to look into a library or framework that has that stuff written for you already, like Box2D. I usually find it difficult to wade into someone else’s hand-rolled physics code, because there are a lot of simplifying assumptions that people make, which usually aren’t expressed well in one-time-use code.

Edit: For example, your expression _main.ballYSpeed *= -1 is, I think, a simplification of a vector cross product, since you’re kind of special-casing the physics of the situation. But that makes it harder to read through the logic of your code, since it’s depending on contextual details like the static position and orientation of the bricks.

I understand… Thank you for answering me!