Accurate collision tests among circles

Aye! Hi there.
I’ve been trying some collision tests among circles these days. And very unluckily, I encounter some problems.

I could create a function of collision test pretty easily with this method:


dx = this._x - _root.ball2._x
dy = this._y - _root.ball2._y
d = Math.sqrt((dx*dx)+(dy*dy))

dx is the difference between the x-coordinates of the two circles and… you know.

Here are the full codes:
Note that I named the two circles as “ball1” and “ball2”.


onClipEvent (enterFrame) {
 var speed = 1
 var dx:Number
 var dy:Number
 var d:Number
}
onClipEvent (enterFrame) {
 if (Key.isDown (Key.RIGHT)) {
  this._x += speed
 }
 if (Key.isDown (Key.LEFT)) {
  this._x -= speed
 }
 if (Key.isDown (Key.UP)) {
  this._y -= speed
 }
 if (Key.isDown (Key.DOWN)) {
  this._y += speed
 }
 dx = this._x - _root.ball2._x
 dy = this._y - _root.ball2._y
 d = Math.sqrt((dx*dx)+(dy*dy))
 if (d <= 40) {
  speed = 0
 } 
}

So ball1 did not stop when it hit ball2. It went through.

But when I change “speed = 0” to "trace(“Hit!”), I got the results in the output window. So flash knows that there had been a collision. However, it does not stop ball1 from going through. What’s the matter?