Hi Everybody,
I'm sorry that I wasn't in class today. I have been a bit ill all weekend since Friday, so I thought it better that I not go in to class today. However, it's nearing the end of the term, so we haven't any time to waste. Below you will see the steps that I want you to complete and bring to class next week ALONG WITH THE HOMEWORK FOR THIS WEEK, so don't forget. BOTH HOMEWORKS (last week's and this weeks) WILL BE DUE NEXT WEEK!!!
- Syllabus
- LINK class syllabus
- Classwork
- LINK No class files this week;
- Homework
- exercise 1:
- Create a drop-down menu as we did last week in class. This time, however, put only 3 movie-clip buttons in the part that appears and disappears.
- The instance names for the 4 movieclips which function as buttons should be btn_01, btn_02, btn_03, andbtn_04.
- The instance name of the movieclips which contains the 3 options should be options .
- This drop-down menu should function exactly the way they did in class last week. Also, make sure that, as per last week's homework, when you mouse off the menu, the menu options should disappear.
- In addition, use an array to make the text show in the 4 movie-clips as instructed in last week's homework.
- Create another array named urlArray:
- var urlArray:Array = new Array();
- Next, as indicated in last week's blog entry, below the previous code, we need to put data into the array. We call this process populating the array. An array is similar to a variable in that it contains data; however, it is much more powerful in that, unlike a variable, it DOES NOT hold only ONE element of data. Instead, it may hold many, MANY DATA ELEMENTS. Each of these data elements is indicated by a number, and the counting starts at 0:
- urlArray[0] = new URLRequest("http://dma-205.blogspot.com");
- urlArray[0] = new URLRequest("http://www.actionscript.org");
- urlArray[0] = new URLRequest("http://www.kirupa.com");
- Now, we have to modify the code that we created last week. As you may have noticed last week, the code is very, very long to do simple things. We have 2 functions for each of our buttons just to make the colors chane when we mouse-over them. It can be greatly simplified. In fact, all we really need is one function for that activity. What we are going to do is create 4 functions in total instead of 2 functions per button situation that we have now. The first two we'll work with are for the opening and closing of the menu:
- Comment out all the functions which pertain to the changing of the colors and the opening and closing of the menu. You should also comment out the listeners. Do not delete these functions & listeners, however, until you get the replacement functions working properly.
- Create a new function and name it menuOpen.
- function menuOpen (evt:MouseEvent):void {
- }
- Next, create another function namedmenuClose.
- function menuClose (evt:MouseEvent):void {
- }
- Eventually, in a future class, we will combine these two functions using what's known as an if/else statement, but for now we'll keep them as 2 separate function. Right now, however, they have nothing inside of them, so let's put some actions in the first one:
- function menuOpen (evt:MouseEvent):void {
- options.visible = true;
- }
- Now, modify the listener for the first button to read as follows:
- btn_01.addEventListener(MouseEvent.MOUSE_OVER, menuOpen);
- Save and test your movie and it should open up.
- Next, we will put some actions in the second function:
- function menuClose (evt:MouseEvent):void {
- options.visible = false;
- }
- And modify the mouse-over listener for the first button to read as follows:
- btn_01.addEventListener(MouseEvent.MOUSE_OUT, menuClose);
- For now, we are not quite ready, however, for the above listener, so let's comment it out for the time being. We will come back to this later. This means that the menu will not yet close:
- //btn_01.addEventListener(MouseEvent.MOUSE_OUT, menuClose);
- Next, we'll modify the code to make the buttons change colors again, but with much less code:
- Create a new function and name it colorFun_01. With this function we must do TWO things: (1) determine which button the user is mousing over, and (2) make that button change its colors.
- function colorFun_01 (evt:MouseEvent):void {
- btn_01.stroke.transform.colorTransform = sColor_new;
- btn_01.fill.transform.colorTransform = fColor_new;
- btn_01.btn_txt.transform.colorTransform = sColor_new;
- }
- The problem with this function, of course, as some of you will probably have noticed is that it will only change one button, the one that is named using its instance name, btn_01. Therefore, we have to figure out a way to select whichever button we happen to mouse-over
- The answer to this is contained within evt. I mentioned it once before, but that little statement that is in between the parenthesis, is actually a variable and the type of information that will be contained within the variable: (evt:MouseEvent), where evt is the variable name, and MouseEvent is the type of information held within it.
- If you look in your listeners, you will see that term again, MouseEvent. When the listener calls on the function, the information of WHICH particular button the user happens to be interacting with is being stored by that variable evt. That means, that evt knows which button is currently being moused-over or clicked on or whatever at any given moment.
- This means that we do NOT have to be specific by naming a particular button's instance name, all we have to do is invoke evt.
- Now, because evt contains MouseEvent information, it also possesses a new property. That property is known as currentTarget, in which the current target is whatever is the movie-clip that the user happens to be interacting with at any given moment.
- Let us test this, so first, temporarily comment out the code in the function:
- function colorFun_01 (evt:MouseEvent):void {
- //btn_01.stroke.transform.colorTransform = s_new;
- //btn_01.fill.transform.colorTransform = f_new;
- //btn_01.btn_txt.transform.colorTransform = s_new;
- }
- Now, add the following line of code:
- function colorFun_01 (evt:MouseEvent):void {
- trace(evt);
- //btn_01.stroke.transform.colorTransform = s_new;
- //btn_01.fill.transform.colorTransform = f_new;
- //btn_01.btn_txt.transform.colorTransform = s_new;
- }
- Next, modify your listener for the first movieclip button to call this function instead of the old color change function.
- btn_01.addEventListener(MouseEvent.MOUSE_OVER, colorFun_01);
- Save and test your movie.
- Mouse-over on your button and look at your output window. If you don't have any errors, what it will do is tell you something about a movieclip.
- Now, let's add a name to that movieclip.
- function colorFun_01 (evt:MouseEvent):void {
- trace(evt.currentTarget.name);
- //btn_01.stroke.transform.colorTransform = s_new;
- //btn_01.fill.transform.colorTransform = f_new;
- //btn_01.btn_txt.transform.colorTransform = s_new;
- }
- Save and test the movie again. Now, when you click on the button, you should see the instance name of it inside the output window.
- Modify all the listeners that pertain to the old color change functions for the MOUSE_OVER actions. When you mouse over each of them, they the output should indicate which one you have moused-over by its instance name.
- This tells us for certain that, instead of typing a particular instance name, we can just use the generic term, currentTarget after the variable name, evt. Let us now do that. First, comment out the trace and remove the comments from the other lines of code, modifying the instance name to add evt.currentTarget instead.
- function colorFun_01 (evt:MouseEvent):void {
- evt.currentTarget.stroke.transform.colorTransform = s_new;
- evt.currentTarget.fill.transform.colorTransform = f_new;
- evt.currentTarget.btn_txt.transform.colorTransform = s_new;
- }
- Good, now that that works, let's see if we can create the function to make the color change back:
- function colorFun_02 (evt:MouseEvent):void {
- evt.currentTarget.stroke.transform.colorTransform = s_old;
- evt.currentTarget.fill.transform.colorTransform = f_old;
- evt.currentTarget.btn_txt.transform.colorTransform = s_old;
- }
- Do not forget to modify the listeners for all the movieclips so that they call this function when the user mouses-off of them.
- exercise 2:
- Next, let's make the links in the drop-down menu active:
- To start, then, we'll put to use that new array that we created in the beginning of this homework:
- var urlArray:Array = new Array();
- urlArray[0] = new URLRequest("http://dma-205.blogspot.com");
- urlArray[0] = new URLRequest("http://www.actionscript.org");
- urlArray[0] = new URLRequest("http://www.kirupa.com");
- Let's start putting these URLs to work for us by creating a new function called linkfun:
- function linkFun(evt:MouseEvent):void {
- }
- Next, inside that function, first, I'd like to create a variable to store the name of the particular movieclip that is moused-over or mouse-out of at any given moment:
- function linkFun(evt:MouseEvent):void {
- var mcName:String = evt.currentTarget.name;
- }
- We are going to use this string inside what's called a switch statement. This statement allows a script to select from several possibilities, or cases. Think of it this way. We have 3 buttons, and therefore there are 3 possible outcomes. The user could click on one of the buttons, or the user could click on one of the others. Depending on which button the user actually clicks on, 3 different things could happen. THOSE ARE OUR 3 CASES. The way we are going to tell Flash to determine if it's one movieclip or another is by its instance name. Recall, the instance name is going to be stored in our new variable, whichever movieclip button the user happens to have moused-over:
- function linkFun(evt:MouseEvent):void {
- var mcName:String = evt.currentTarget.name;
- switch(mcName) {
- case "btn_02" :
- do something;
- case "btn_03" :
- do something;
- case "btn_04" :
- do something;
- }
- }
- The way the above statement works is this: recall, mcName contains the name of the particular movieclip that the user mouses-over. Actionscript is then going to look at each of the 3 cases. It will then compare the name that is inside mcName with the strings that you see there in each case, "btn_02", "btn_03", or "btn_04". Whichever one matches, the script then does what the next line tells it to do.
- What we want it to do is to open up a new web-page:
- function linkFun(evt:MouseEvent):void {
- var mcName:String = evt.currentTarget.name;
- switch(mcName) {
- case "btn_02" :
- navigateToURL(new web address);
- case "btn_03" :
- navigateToURL(new web address);
- case "btn_04" :
- navigateToURL("new web address");
- }
- }
- Obviously, we couldn't just type do something, so we replaced it with the action navigateToURL();
- What we didn't do is type in a particular web address. We could do that, but it'd have to be a string like so: "http://www.google.com".
- However, we do not have to do that. We have already created the array above, and inside each numbered element of the array, is a particular URL address. Therefore, all we really have to do is type the NAME OF THE ARRAY followed by its INDEX NUMBER:
- function linkFun(evt:MouseEvent):void {
- var mcName:String = evt.currentTarget.name;
- switch(mcName) {
- case "btn_02" :
- navigateToURL(urlArray[0]);
- case "btn_03" :
- navigateToURL(urlArray[1]);
- case "btn_04" :
- navigateToURL(urlArray[2]);
- }
- }
- The last step is to type the three listeners for the three movieclips, each which corresponds to a different case:
- options.btn_02.addEventListener(MouseEvent.CLICK, linkFun);
- options.btn_03.addEventListener(MouseEvent.CLICK, linkFun);
- options.btn_04.addEventListener(MouseEvent.CLICK, linkFun);
- Next, go to the FILE menu, and select Publish Settings.
- Make certain that both .swf and .html are selected.
- Then at the bottom, click publish and okay.
- Open up your folder, and notice that you have an .html file there. Open it up in your browser and try it out.
- The links may not function as you may get a notice in your browser saying that it is a potentially unsafe action. That is okay. We will deal with that another time. Just click okay as that tells us that it is at least trying to open up an outside web-site.
- exercise 3: EXTRA CREDIT
- Instead of linking to other web-sites, try to get other .swf files to load into the page when you click onto the buttons in the pull-down menu.
No comments:
Post a Comment