all groups > flash actionscript > october 2004 >
You're in the

flash actionscript

group:

atan and other such trig


atan and other such trig badmonkey91
10/21/2004 11:12:22 PM
flash actionscript:
Im having some problems with actionscript.

I have two points, and from that I want to create a triangle, to determine an
angle. The problem is, I cant get the code to work in actionscript. If someone
could give er' a once over, It'd be welcomed!

xm=_xmouse
ym=_ymouse
ox=276
oy=200

a=(Math.atan2(ym-oy,xm-ox));
setProperty("man", _rotation, a)
Re: atan and other such trig kglad
10/22/2004 3:20:39 AM
Math.atan2 is in radians and _rotation is in degrees. so you probably want:

xm=_xmouse
ym=_ymouse
ox=276
oy=200

a=(Math.atan2(ym-oy,xm-ox));
Re: atan and other such trig badmonkey91
10/22/2004 3:49:26 AM
that doesnt seem to work either. I have used basically the same code in basic,
and it works, is there something I need to do the variable to make it real
maybe?

I know in some langauges you use a pund sign (#) so that the math will work
with a real number.
Re: atan and other such trig kglad
10/22/2004 4:01:23 AM
that could will cause a movieclip with instance name "man" to rotate to the
specified angle. i'm not sure what's causing that code to execute and that's a
potential source of problem: if that code is attached to a frame, it's not
going to do what you want.

generally, that code would be triggered by some user action because it's
dependent upon the mouse position at the time the code executes. if that code
is attached to a movieclip that does not contain the child movieclip "man",
you'll have a path reference problem.

if you're still stumped, zip and upload your file and i'll correct it.
Re: atan and other such trig badmonkey91
10/22/2004 7:03:06 PM
I have it in two looping frames, which acts as a syncing command would.

there is nothing wrong with my theory, I think Im going at it from the wrong
direction.
I found a good tut, thanks to
http://www.actionscripts.org/tutorials/advanced/Mouse_Angle_Detection_II/index.s
html.

here's his code, put into the object's script.

onClipEvent (mouseMove) {
x = this._xmouse;
y = this._ymouse*-1;
angle = Math.atan(y/x)/(Math.PI/180);
if (x<0) {
angle += 180;
}
if (x>=0 && y<0) {
angle += 360;
}
_root.angletext = angle;
_root.arrow._rotation = angle*-1;
updateAfterEvent();
}

it works, I'll just have to translate it.
Re: atan and other such trig kglad
10/23/2004 2:19:01 AM
if you like that code and just want it to be part of a frame-loop instead of an
enterFrame loop, you can use:


x = this._xmouse;
y = -this._ymouse;
angle = 180*Math.atan(y/x)/Math.PI;
if (x<0) {
angle += 180;
}
if (x>=0 && y<0) {
angle += 360;
}
_root.angletext = angle;
_root.arrow._rotation = -angle;

AddThis Social Bookmark Button