Flash as learning: definition and invocation of functions

Source: Internet
Author: User
Tags define definition copy setinterval
Function

Demo Effect:

Click here to download the source file

Some people liken a function to a machine that is machined, a rice mill is a paddy, and the output is a random (5) function, the input is 5, the output is any one of the 0~4; random () is built into the flash itself, and we can define one of these machines by ourselves, so that What we enter, what we output, we are all our own masters. So let's start by defining a function that, as long as we enter a number, prints 3 times times the number.

Function cont (n:number) {
Trace (3*n)
}

The above defines a function, which is like a machine parked here that is not used. Let's see what the machine is for. function is the rule in as, which tells us that we are going to define a function, Cont is the name of the function, just like the name of the machine being processed; n is a parameter, just like something to be entered into the machine. Trace (3*n) is what the function is going to do, like what the machine is going to do with the input. Now we can start the machine. Follow the writing:
Cont (5) This means the execution function, enter a number 5 for the function.
This defines a simple function. In fact, the main purpose of defining a function is to make it more convenient to call, such as a lot of buttons to call this function, and the input parameters are different, then the usefulness is reflected. For example, call the above function cont:

On (Press) {
Cont (6)//output 18
}

On (Press) {
Cont (7)//Output 21
}

On (Press) {
Cont (8)//Output 24
}

The advantage of using a function is that you can set the interval of time to invoke it. For example, we want to call the following Cont function at intervals of 1 seconds, which we can write.
SetInterval (cont,1000,5)//cont is the function name, 1000 is the interval, the unit is milliseconds, and 5 is the parameter. So the CONT function is called once every 1 seconds, but what if we want the function to stop only 10 times? Full as follows :(please copy this as for testing)
Function cont (n:number) {
Trace (3*n);
if (m==undefined) {//Because there is no definition of m,m here only the first reading meets the criteria, the second time, it is directly followed by + +
M=0
}
m++
if (m>=10) {
Clearinterval (AA);//Clear Call to SetInterval
}
}
Aa=setinterval (cont,1000,5) The use of the Onenterfram in the previous section, if it is handled with a function interval call, writes:
var i:number = 0;
function dup (max:number) {
Duplicatemovieclip ("_root.bug", "Bug" +i, i);
The new ID for the copy bug,bug is bug0 bug1 bug2 ...
_root["bug" +i]._x = random (400);
The coordinates of the new bug are any number within 400, respectively.
_root["bug" +i]._y = random (400);
_root["bug" +i]._rotation = random (180);
The new Bug's rotation is any number within 180, making it easier for the worm's head to move in all directions.
i++;
Because the new ID of the bug cannot be the same, it requires i++
if (I>=max) {
If the number of copies is greater than or equal to 50
Clearinterval (AA);
Delete This.onenterframe;
Delete this event
}
Trace (i);
Tracking I, and testing the case of I without the delete
}
AA = SetInterval (DUP, 500, 50); In this way, the speed of replication by our own arbitrary control.

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.