Function

Sometiems it is required that a bunch of code should be rewritten. In other words, we want samething to happen at different times and different situations. For that we write up the same code again. If we look at our example lets stop();, we can see that

first_btn.onRelease =function():Void{

_root.holder_mc.loadMovie("one.swf");

}

the above code is repeated almost in all the buttons. Though the value inside loadMovie(""), changes. Similar situationscan come almost anytim, when we start to write actionscript. For all these situations, actionscript provides a handy tool called "Function". Function is nothing but a bunch of code grouped together. Lets try an example. Open a new flash document, created 2 layers, and name them

actions—————-

Graphics————–

In graphics layer create a movieclip and name it "myClip_mc" and make another clip with name "move_mc" Now comeup to the actions layer.Select the first frame and open actions pannel. Type in the following script

move_mc.onRelease=function(){

trace(myClip_mc._x);

}

Now test your movie, you will see that once you click "move_mc" you get x co-ordinate of "myClip_mc" on the output panel. Do not confuse with the "function" written here. For the time being you can assume that this is how a script is written, if it has to be written in a frame. Now we will add some more code to it like below;

move_mc.onRelease=function(){

trace(myClip_mc._x);

trace(myClip_mc._y);

}

this time, if you test your movie you will see both the values of x, and y are displayed in output pannel. lets add something more inour script

move_mc.onRelease=function(){

trace(myClip_mc._x);

trace(myClip_mc._y);

myClip_mc._x=myClip_mc._x+5

}

If wetest our movie it is outputting the x,y values and making our clip move in positive x direction. At this movement, lets create another clip and name it "another_mc". We want the same code to be placed for another clip too.So the code will be like

move_mc.onRelease=function(){

trace(myClip_mc._x);

trace(myClip_mc._y);

myClip_mc._x=myClip_mc._x+5

}

another_mc.onRelease=function(){

trace(myClip_mc._x);

trace(myClip_mc._y);

myClip_mc._x=myClip_mc._x+5

}

Its time to write a function. OUr code is repeating itself, so lets write a function for it. Our new code will be

function myFunction() {

trace(myClip_mc._x);

trace(myClip_mc._y);

myClip_mc._x = myClip_mc._x+5;

}

move_mc.onRelease = function() {

myFunction();

};

another_mc.onRelease = function() {

myFunction();

};

See, I have moved all the code from both the buttons to one place, thats called a function. Now instead of all the codes inside all the buttons, we simply call the function from anywhere we want the same code.If we test our movie, we can see the same thing is still happening. Writing a function is simple, we start up with the key word "function" followed by the name of the function. In our case we name it "myFunction".So in a bare form a function will look like

function yourFunction(){

//your code

}

now from anywhere in your script if you call your function by "yourCode", all the codes inside your function will be executed. A function is called by the following syntax

yourFunction();

Function name should be followed by "()".that to tell flash that this is a function. These braces are also a lot to do with our function. We will see there use in coming examples.But keep in mind that we need to call a function with its name follwed by ().

function gotoAndTest(){

trace("try your own functions")

}

;)

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s