AS: Main
FlashTroops
In this tutorial, I will explain a code that will allow your to calculate which movieclip happens to be the closest in descending named order (ball1, ball2, ball3, etc…) either duplicated or otherwise. This can extremely useful for AI purposes in a variety of games. I will not be introducing any new concepts that have not already been in tutorial in AS: Main, so I will link you to tut’s needed to understand the code and only explain things already covered lightly.
Full Code:
onClipEvent (load) {
distance=0
searchRadius =0
xDist=0
yDist=0
}
onClipEvent (enterFrame) {
searchRadius = 1000;
for (i=0; i<10; i++) {
xDist = _x-_root["ball"+i]._x;
yDist = _y-_root["ball"+i]._y;
distance = Math.sqrt(Math.pow(xDist, 2)+Math.pow(yDist, 2));
if (distance< searchRadius) {
searchRadius = distance;
trace(i)
}
}
}
Working File
Now for the breakdown.
onClipEvent (load) {
totalDist = 0;
distance=0
xDist=0
yDist=0
}
This is just setting up our variables. I don’t think any explaining is needed there.
searchRadius = 1000;
This is exactly what it sounds like. It is your search radius. I will explain its further importance a little later.
for (i=0; i<10; i++) {
This is our loop that we will be using for checking the movie clips. I will pink to a tut explaining how they work in the end.
xDist = _x-_root["ball"+i]._x;
yDist = _y-_root["ball"+i]._y;
These distances are what we will be using with the distance formula to find out exactly how far each movieclip is away from your main MC.
distance = Math.sqrt(Math.pow(xDist, 2)+Math.pow(yDist, 2));
This what calculates the distance of each MC. Some of you may recognize if from a math class, it is the basic distance formula used in all math.
Math.sqrt() = Square Root.
Math.pow() = To the power of.
The 2 I have in the Math.pow() function is the power, whether it be squared (2), or cubed (3), and any higher.
if (distance< searchRadius) {
searchRadius = distance;
}
Our search radius is, at the moment, 1000. What this does is checks if the total distance of the movie clip is less then the current radius. If it is, then it must be closer. I then set the radius to how close that paticular movie clip may be. Now for the logic behind all of this.
The first radius is 1000. The loop checks to see if ball1’s distance is less then that radius. Ball1 will always be under the initial radius. Now that it is under the radius, we check to see if ball2 is under that radius. Now remember, that radius is no longer 1000, it is however close ball1 is to you. So, if ball2 happens to be closer then ball1, then the radius is changed back to the distance of ball2. If it is not closer, then the radius remains what it was (distance of ball1). This cycle goes through until the closest ball is found.
The one flaw with this code is that it traces more then just the closest ball. It will first trace ball1, and if ball2 is closer, then ball2 is traced, and if ball2 happens to be the closest of all of the MC’s, then ball2 is traced for the rest of the script. But ball1 is traced for the one split second, which may cause problems in some scripts. It works fine for my working file, as we moves toward the MC, which is the purpose of many AI, which I is why I still find this script useful.
Hopefully someone understood this and hopefully someone finds it useful. I for one will be using it in my next small project, which probably wont get done like all my other games, but I will get the engine done at least. Here is the script I used on my working file:
onClipEvent (load) {
xSpeed = 0;
ySpeed = 0;
playerDist = 0;
speed = 5;
}
onClipEvent (enterFrame) {
_x -= xSpeed;
_y -= ySpeed;
playerDist = 1000;
for (i=0; i<10; i++) {
xDist = _x-_root["ball"+i]._x;
yDist = _y-_root["ball"+i]._y;
distance = Math.sqrt(Math.pow(xDist, 2)+Math.pow(yDist, 2));
if (distance<playerDist) {
_root["ball"+i].thing = true;
playerDist = distance;
xDist = _x-_root["ball"+i]._x;
yDist = _y-_root["ball"+i]._y;
angle = Math.atan2(yDist, xDist)/(Math.PI/180);
xSpeed = Math.cos(angle*(Math.PI/180))*speed;
ySpeed = Math.sin(angle*(Math.PI/180))*speed;
if (this.hitTest(_root["ball"+i])) {
xSpeed = 0;
ySpeed = 0;
_root["ball"+i].swapDepths(9999);
_root["ball"+i].removeMovieClip();
}
}
}
}
So add my code to a movie clip, then make other movie clips with the name ball1, ball2, ball3, and so on. If you want more then 10 balls, change the 10 in the loop to a higher number. Here are some other tuts that might help explaining other parts of my code:
Loops and Conditions by BleeBlap
AI by Vengence
Collision with duplicated MC's by SpamBurger