A specific analysis of the random motion of MC with AS

Source: Internet
Author: User
Tags cos setinterval sin
Random

Let the MC do random move, it is conceivable that frame animation is impossible to achieve, which requires us to do with AS.
In general, there are 2 ways to let the MC do random movement with as:

The 1th kind:

1. You can first randomly determine a point on the stage of the coordinates, we call it "target position", with (X,y), by this "target coordinates" and MC original coordinates (X0,Y0), according to the formula {math.atan2 (y-y0,x-x0)} can be calculated a radian a, as Figure 1;


2. Again by the sine sin of this radian (a) and cosine by cos (a) a fixed value speed V, you can get the MC's vertical and horizontal coordinates will move the distance, such as figure 2,MC also toward this "target position" moved.


3. We use setinterval every once in a short time to take a "target point", because it is random, so the MC also do irregular movement.
Let's take a look at an example:

Click here to download the source file

As:

v = 10;//Speed
Function abc () {
x = random (stage.width);
y = random (stage.height);
}//function, obtain the target point randomly
SetInterval (abc,500)//Every 0.5 seconds to determine the target point,
The frequency of the "target" change is too high for the onenterframe to be used directly, causing the ball to vibrate rather than move;
Onenterframe = function () {//Continuous execution
A = Math.atan2 (y-mc._y,x-mc._x);//Calculate radians A
Mc._x + + V*math.cos (a);
Mc._y + = V*math.sin (a);//move
}

The 2nd kind, is lets the MC randomly rotate an angle a, this angle is also the MC will go forward direction.

Specific practices:

1, we want to first the MC rotation angle A into radians;

2, the same arc with the cosine sin and cos multiply the speed V, to find the MC of the vertical and horizontal coordinates will move the distance, so that the MC in this direction of rotation move. (Refer to Figure 2)

3, with Setinterval/onenterframe constantly to determine the random rotation angle, the MC also randomly movement.
This involves the conversion of radians and angles, and we have the formula:

Angle = Radian *180/3.1415 ...;
Radian = Angle *3.1415...../180;

Let's take a look at this piece of code:

v = 10;//Speed
Function abc () {
A = Mc._rotation = Random (360);
}//Gain Angle
SetInterval (abc,500)//Every 0.5 seconds to determine the angle, the principle of ditto
Onenterframe = function () {
b = a*180/math.pi;//converts angle to radians, Math.PI to Pi
Mc._x + = Math.Cos (b) *v;
Mc._y + = Math.sin (b) *v;//Mobile
}

Copy the as into the source file above to see if the effect will be the same:



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.