Video Transcript
So, we started to add action script to the few places within our application. We have got some element in the main timeline. We have also got some action script starting to appear within the join screen. Flash allows us the flexibility of adding action script in various places but this is in not great best practice. In theory, we can add action script to the timeline or frame within the movie clip or we can add it directly to a button. But, what we are really trying to achieve is all that action script being stored into one place. This is generally on the first frame of the main timeline. This just allows us to group all that action script together and that is really important especially when we are sharing an application file amongst various developers. We do not want different developers looking in different places for your action script. We want them to know exactly where it all should be.
And just tonight, we are going to look later on at some other process of how to really tighten up this process using class based development. Let us go back within our join screen movie clip and start to edit that a little bit further. We will select it from our Library and double click on it which allows us just to edit that movie clip. So, let us select the first frame of our action script layer within the Join Movie Clip and underneath where we have declared our user name text field, the join button, we are going to add some action script to handle the event that is filed when the Join Button is pressed. Let us just add some space and add on release function to our Join Button object. So, when we are adding functions directly to an object, we type the object name first, the function name and then declare it as a function afterwards. And of course, we are going to say the date type as well for the function two. This is going to void because we do not want this function to return anything and then use the color braces to define the executable code for this function. So, this is now an event hand loop for the Join Button. Every time the Join Button object is released, this function is going to execute. And, we want this to execute a function called Save Username so that we can send the value of the input text field to a function which will then work with it further.
This Save Username function actually lives at of our main timeline which is different to the one that we are in the moment. That is why we have got the _parent indicator before the function name. The parent indicator refers to the object that contains the current object. So, in this case, the main timeline contains the Join Movie Clip. So, to get back to the main timeline, we need to reference it using _parent. There is another indicator that we could also which is _root. This always refers to the main timeline of the Flash Application. But for this reason, you need to be very careful how you use it. If you create a Flash Movie referencing _root and then load that Flash Movie inside another one, then again you are referencing to the wrong timeline and something is going to get broken. This does the touch of the topic of using absolute and relative paths which is going to come up more as we go through. What we will need to do now though is to go back up to the main timeline and create the Save Username function.
So let us return to the main timeline and select the first action script frame. Let us go to our actions panel and write this function. Remember when we write a user defined function, we first declare it as a function then the function name which is in this case is Save Username. Add any arguments that we are expecting to be passed into the function. In this case, we are looking for a string that we are going to identify using p_username and also the return type of the function. In this case, it is going to be void. We are going to declare a new variable inside this function to be the value of the p_username parameter that gets sent in. We have the variable name equal to the value of the parameter and this of course will a String. We just want to try and see how this value from the time being just to make sure that it is working. And because we have already set this new username variable, we can just trace that rather than the name of the parameter that gets passed in. Let us save our application and see how this works out of the mobile emulator. As always, whenever we get something wrong in action scripts, it gets traced up to the output window. It is very descriptive as to where we have gone wrong and I can see that we have left out one important gradient from malfunction. Let us go back and fix that up.
So the area was happening within our Join Movie Clip and then is joined by an unreleased function. Even though we scripted this function in a little bit of a different way to what we have done before, it still needs the basic ingredients. We need to declare it as a function, give it a name, set any parameters or arguments that we are going to use and then it will turn data type. And, I can already see which one we have left out there. It is the open and close brackets that would normally hold the arguments. This is a good opportunity to also show you another little trick inside of Flash. Whenever we write action script in the actions panel, before going and testing our movie, we can actually test to see if the syntax of that code is correct. The shortcut for this is Control T. And now, I can see. Yes! I have got all of my action script right, it contains no errors. Now, let us go inside our movie and test it in the emulator to see it working hopefully with no more action script errors.
Now, this gives us a change to actually interact with that content via the emulator. I am going to select the input text field via the directional key on the device. You can see when I click down we get this yellow focus rectangle around the input text field. Next, we are going to discuss how to control that focus rectangle a little bit more. If I was looking at this on an actual device, I will press the Select key so now we are be able to enter that text into that field. In the mobile emulator, we get this little dialog box opening up which allows us to type in the content that we want to appear in that text field. Normally, we will be doing it via the number case on the keypad. I am just going to type in Test, click okay and you can see that Test now appears within our input text field. If I continue to scroll down, I will bring the Join Button into focus. We get the obvious state of the button as well as the focus rectangle. And again, clicking select, we now trigger off the event for the button which is going to be calling the Save Username functions back on the main timeline. And remember, we set that up to be able to trace out what we put into the input text field. And as you can see, we have done a successful test.
So, we started to add action script to the few places within our application. We have got some element in the main timeline. We have also got some action script starting to appear within the join screen. Flash allows us the flexibility of adding action script in various places but...
click to read more