Using trigonometric functions to achieve wonderful results

Source: Internet
Author: User
Tags continue eval sin
Function

First, the preface

The use of trigonometric functions to complete the flash effect has already been a master, and the tutorial, see Zjs35 eldest brother of the elliptic equation, I decided to write something, because the trigonometric function is in the flash effect has a magical effect, just a few lines of code can accomplish a lot of dazzle effect.

This article will analyze how to use trigonometric functions, and finally give two effects. One of them I've seen in a "20-line code contest," but I've never seen a source file. I give this can be said to be original.

Second, function analysis

Trigonometric functions we have learned in junior high school, especially Sin,cos, can be said to be the most common cycle function. We use trigonometric functions to effect, mainly is the two function.

The complete form of the sin function is:

Y=a*sin (Ψ+Ω*X);

Ψ is the phase, ω is the angular velocity, and the ω=2π/t,t is the cycle. According to the sine function of the image can be known, the x axis of a point is periodically in the equilibrium position of simple harmonic vibration, amplitude of a, based on this knowledge we can make a simple elastic ball:

_x+=centerx+a*math.sin ((i++) *math.pi/180);

We further think that if it is a wild ball (or vertical, the ball can also be something else), we all with a certain sine formula movement, this periodic movement is not the source of beauty? The difference between the balls is just the phase. Further thought, if not just the position of change, but transparency, size, color and other attributes have changed, this "large-scale" cyclical change is not a number of magical effects? "There is the law is the United States", trigonometric functions bring it is the regularity, periodicity, as long as we have mastered its most basic part, we can play the imagination, create more results!
Let's take a look at an example first.

Third, the example production step

This example is not a small ball to talk about, because not only small balls to use trigonometric functions, others can, such as fonts. The following font effect is to fully use the sine function formula, the effect of the preview

As you can see, the code inside is very simple, and the key is only one sentence:

_alpha=_xscale=_yscale= (100-_root. A) +_root. A*math.sin (N+I*PI);


Let's start from the beginning and believe that after you've finished, you'll have a better understanding of how to use trigonometric functions.

1. Create a new dynamic text in the home view, named text box "Lab", of course, I also put the string to display "Welcome to the Semitic Empire, I am Xfykzz" as an embedded font. Embedded fonts are also used as a key point to complete the font effect, rough talk: The Font property to the right side of a "character ... button, click, and then select "Specified range", in the bottom of the input box to "Welcome to the Semitic Empire, I am xfykzz" embedded, OK. As to what you intend to use the font does not matter, I use here is "Fangzheng water column body simplified."

2. Select the text, press F8, turn it into an MC, name I use "T" here, and then write the link id as "T" in the library panel. The way to do this is to right-click the MC in the library panel, select "Link ...", and select the first and fourth items in the pop-up dialog box to make sure.

3. Return to main scene first frame write code:

Tstring= "Welcome to the Semitic Empire, I am xfykzz";//Determine the string to display. len=tstring.length;//Specifies the variable len load string length. pi=2*math.pi/len;//Here is the round (2*math.pi ), so that each word occupies one copy (used below), and/or all the words occupy the whole circle. The circle here is not a circle, but a cycle of trigonometric functions. a=30;//Amplitude cenx=50;ceny=100;//Center coordinate n=0;//phase change factor, through its change to achieve the goal of changing the whole curve. m=2500;//variables to determine the factors that change the speed of trigonometric functions.

  
These lines are definitions of variables that are more convenient for writing below and for later modification. As you can see, we are not changing the position, but the transparency and size, in the note explained above, borrowed the use of the location of the expression method. Continue:

for (Var i=0;i
     
     < pre="">

I don't need to explain these words. Continue:

_root.onenterframe=function () {for  (var i=0;i
     
     <>

I believe that the front is not difficult to understand, this paragraph is the key, of course, it is very simple ~
Onenterframe executes the code at the frame rate, for the loop is executed for each MC:

_alpha=_xscale=_yscale= (100-_root. A) +_root. A*math.sin (N+I*PI);

This means that the phase of the first MC is (N+I*PI), that is, in the first part of a cycle, all strings are one cycle. N Changes below: n+= (_xmouse-cenx)/m; Change the phase of the entire string, thus showing a continuous feeling. m above mentioned is the sensitivity factor, the smaller the M value, the same mouse position n changes more, the character movement is more intense. Its balance position is: 100-_root. A, can be seen, its vibration position in (100-2*_root. a) to 100. With 100 as the upper limit, because we change the transparency and size.

4. Now the test to see, than using frame animation to make the same effect is not a lot simpler?

Using trigonometric functions as a font effect may be the most time-saving and easiest way to change. You can try to change the color of something again, there will be a new discovery!

  Instance One source file download

Iv. Example Two

Preview:

The second example I do not intend to say more, the structure is not the same as the one above, the difference is a change in the phase of the factor:

q=math.pi/(2*len);

This is the cause of the drift, in front of a large pile of variables you do not have to fully understand, I wrote that is to be more easily modified. I have added a note, may wish to see.

The triangle function here is just the tip of the iceberg, you just dig it, there will be surprises, not necessarily sine function, tangent? What about the cosine? May as well give it a try.

  Instance two source file download



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.