Skip to main content
Inspiring
June 12, 2007
Answered

Finding closest target

  • June 12, 2007
  • 2 replies
  • 227 views
Hello there, i've got a ball movie clip named ball, and 4 targets in each corner of the screen. What I want to do is dynamically find the closest target and then get the ball to move towards that target. I've come close to getting it working, but for occasionally when I run it, the ball will travel towards a target that is definitely not closest to it. I've got a sneaking suspicion that it has to do with my distance formula, but I figured i'd post here and maybe someone might be able to shine a light on what's wrong or point out an easier/more efficient way of performing this task. Heres my code. The only thing that isn't in the code is my ball movie clip which is just a red circle in the library with the linkage identifier "ball". Everything else is simply code. I know this is quite a bit of code to sift through, but i'd appreciate any help/tips/suggestions. Thanks.

This topic has been closed for replies.
Correct answer kglad
^2 isn't actionscript code for squaring. fix that.

p.s. it's amazing that comes close to doing what you want. ^ is the bitwise or operator.

2 replies

kglad
Community Expert
Community Expert
June 12, 2007
you're welcome.
kglad
Community Expert
kgladCommunity ExpertCorrect answer
Community Expert
June 12, 2007
^2 isn't actionscript code for squaring. fix that.

p.s. it's amazing that comes close to doing what you want. ^ is the bitwise or operator.
Gooms9Author
Inspiring
June 12, 2007
ahhh... i just tried it and it came very close to doing what I want so I assumed it was the correct way to square a value. I fixed that though and tidied up the function a bit as well. All works great now. It seems I didn't need nearly as many lines of code to achieve the same results. Thanks for the help.