Learn a little flash (50) Anti-cotangent application every day

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.