Q: In a scenario, there is a triangle with angles A, B, and C respectively. How can we make the angle of angle a change with the coordinates of the mouse?
Solution: use math. atan2 in the math class to find the angle value between the triangle registration point and the mouse coordinate, plus a 90 degree angle, then the triangle will rotate.
CodeImplementation:
VaR fmove: Boolean; // determines whether the mouse has been clicked.
VaR mousex: number;
VaR Mousey: number;
_ Root. MC. onenterframe = function (){
VaR dx = _ xmouse-MC. _ x; // the x-axis distance between the mouse and MC
VaR DY = _ ymouse-MC. _ y; // y-axis distance between the mouse and MC
VaR radians = math. atan2 (dy, dx); // obtain the angle
MC. _ rotation = radians * 180/Math. PI + 90; // rotate the triangle object following the mouse
};
// If this 90-angle is not added, triangle angle A will not follow the correct rotation of the mouse. You can change the value of 90 to 270 degrees. You need to test it.
VaR OBJ: Object = new object ();
OBJ. onmousedown = function (){
Mousex = _ root. _ xmouse; // assign a value every time you press the mouse.
Mousey = _ root. _ ymouse;
Fmove = true;
Move ();
};
Mouse. addlistener (OBJ );
Function move (): void {
If (fmove ){
Onenterframe = function (){
MC. _ x + = (mousex-MC. _ x)/5;
MC. _ y + = (mousey-MC. _ y)/5;
}
} Else {
Fmove = false; // if you do not press the mouse, it turns to false, so it will not appear
}
}
As long as you press the mouse, the triangle will go to the point where you press the mouse.
Application: when making RPG games, we click the mouse to let our roles rotate and so on.