AS: Main
FlashTroops
This tutorial will explain a code that allows you to determine the distance that a movieclip is from any surface, according to the angle you happen to be facing. It involves some trig functions, a “for” loop, and shapeflags. Here is the full code:
onClipEvent (enterFrame) {
xDist = _x-_root._xmouse;
yDist = _y-_root._ymouse;
angle = Math.atan2(yDist, xDist)/(Math.PI/180)-180;
xDiff = Math.cos(angle*(Math.PI/180))*1;
yDiff = Math.sin(angle*(Math.PI/180))*1;
for (i=0; i<600; i++) {
xComp = Math.cos(angle*(Math.PI/180))*i+_x;
yComp = Math.sin(angle*(Math.PI/180))*i+_y;
if (_root.ground.hitTest(xComp, yComp, true)) {
if (_root.ground.hitTest(xComp-xDiff, yComp-yDiff, true) == false) {
if (_root.ground.hitTest(xComp+xDiff, yComp+yDiff, true)) {
_root.distance = i;
}
}
}
}
}
Working File
Explaination File
The code will work when placed on a movieclip, and will calculate your distance from the “ground” movieclip. Now for the explainations.
xDist = _x-_root._xmouse;
yDist = _y-_root._ymouse;
angle = Math.atan2(yDist, xDist)/(Math.PI/180)-180;
These first three lines are what calculate the angle between the movieclip and the mouse. The distance we are looking for from the ground movieclip is along this angle.
xDiff = Math.cos(angle*(Math.PI/180))*1;
yDiff = Math.sin(angle*(Math.PI/180))*1;
This is the number that we will later use within the shapeflag to help us determine the exact distance. The reason for this code will be further explained later in the tutorial.
for (i=0; i<600; i++) {
This is the loop we will be using for the calculations. 600 for a loop is a little much, but for this tutorial, it is what I will be using to gain the correct distance, to the pixel. If you want to change the ++ to anything other then +=1 (eg. +=2, +=3, +=4) you will have to change the “1” at the end of the x and y Diff codes.
xDiff = Math.cos(angle*(Math.PI/180))*1 ;
yDiff = Math.sin(angle*(Math.PI/180))*1 ;
Change the 1 to whatever number you want to loop to go up by. Raising the number will create less lag, but the calculations will not be as accurate.
xComp = Math.cos(angle*(Math.PI/180))*i+_x;
yComp = Math.sin(angle*(Math.PI/180))*i+_y;
These calculate the X and Y components of the angle. The distance of these are controlled by the loop.
if (_root.ground.hitTest(xComp, yComp, true)) {
if (_root.ground.hitTest(xComp-xDiff, yComp-yDiff, true) == false) {
if (_root.ground.hitTest(xComp+xDiff, yComp+yDiff, true)) {
This is the actual calculations. These three lines will calculate the distance between the ground movieclip and the movieclip with this code on it. If one of the components is touching the ground, and that component-1 (x and y Diff is equal to 1) is not touching, and component +1 is touching, then calculate the distance.
This code can be used in a number of situations. If you want a sidescroller or an overhead shooter, and did not want to use duplicated movieclips as bullets, this code could create instantaneous shooting, like a real gun would. Maybe for dynamic lighting on a bumpy surface, and plenty of other things im sure. Hope somebody learned something.