Learn how to write ActionScript codes using user defined functions to avoid retyping codes over and over again.


More DIY videos at 5min.com

Video Transcription

Okay, so we have got the basic of setting Variables on rebuilt now. But let us look at how we can put this into practice with actually architecting a well define application. So let us close the emulator down and let us save and close this file. In building a well-architected application, we want to control the application flow. Now, we do this through Action Scripts and one of the best ways of reusing Action Script code is through using define functions. We seen some built in Flash functions like the Trace and the String functions, but we can actually write our own Action Script. This is a great way of reusing code. It just means that we do not have to retype code over and over again and creating opportunities for areas to getting into our application. After we have written the function of our own, all we have to do later on is just call its Function name from anywhere in the application and the same piece of codes executed. All right, so let us start running some functions. We are going to open up a new file from the same location as before, called scavengerhunt_2_raw. And before we get started, let us save that. Save As, _2, will take at the _raw and save. So let us just select the first primary action script layout, so you can see now actions panel. We have got the same action script from that previous example and some comments that it will help us write some new functions. Under the decoration comic book, we are going to declare a new Variable, just like we did before. So let us add some space and declare a new Variable called Current State. Now, with good name in conventions, we know that this Variable is going to have something to do with the state of the application that we are currently looking at. And we are going to set the data type of this Variable to a String. So remember, current state is always going to be expected to be a String or a list of characters. So let us get in the bottom of our actions panel where we can see the update divisibility of the all view state and sets the visibility of the new state comic block. We are going to start writing a first use of define function. We do this by declaring it as a function just by simply typing the word Function. The next thing a Function need is a name. And again, good name in convention name is that is going to be a doing word. It is going to describe exactly what it is that this function does. In this case, it is going to be update view. Now, we can post variables or attributes into a function to work with. These are called parameters and they come between the parentheses after the function name. So this function is going to expect the parameter which is a string describing the current state of our application. The variable name is going to be p_newView and as we had done before with data objects, we are going to set it a data type, this time a String. Okay. Now, the third thing that is used to define function made is return type. Sometimes we want to have functions to actually return something to the action script that called it. Other times, we just wanted to execute a block of code once and that is it, and that is what this function is going to do. So the return type of this function is just simply going to be void. And a close at the Syntax has brought their function, we need to add some open and close curly braces. The action script that appears between the curly braces is what gets executed when the functions called or invalid. So, all we want this function to do for the time being, is just trace out the value of the argument that get sent to it. So we use the trace function between the curly braces and we are going to trace at the value of p_newView, close bracket and that it is. Now, that we got our function, we simply just need to call our function name update view. So just below, where we have declared the function, let us call it. I am going to add some space down below, and we simply just type a name update view. Now, the function that we have created is expecting a String to pass to it. So let us pass that String now when we call this function. We are just going to send it the string test. So anything between the brackets is what gets sent to the function when we call it and we close up with a semicolon, as this always good practice when we are writing action script. So let us save this application and see what we get now. And I am going to test my move. Now, an output window is showing the two true traces from our previous example, as well as the word test. So we have called the update function, passed it the string test, and when the function was called, we had that trace statement which trace at the value of the argument it got sent. In this case test, so it worked. Let us close down our emulator and keep going.