Join Paul Trani for an in-depth discussion in this video Adding button functionality, part of Creating a First Web Site with Flash Professional CS5.
Essential to any site is the need to add button functionality, which will really enable that user to navigate the site and control contents. So this site is no different, because I do have all these sections set up. So what I want to do is I want to control the Timeline. And I want it to, when you click on any one of these buttons, it will jump to the appropriate section. So the first thing I need to do is I need to stop the Timeline at about Frame 29, because currently it's going to play through all over this content.
So about at Frame 29, this is where I want the Timeline to stop. And this is going to take ActionScript, okay. Luckily, what's available in Flash CS5 is code snippets. So under Window > Code Snippets, I am able to open up this Code Snippets panel. Very powerful, just really helpful in adding interactivity. But I'm going to go ahead and just put this on the side. This is actually loaded, full of ActionScript code.
And I encourage you to check out all of these different folders, because you have general actions. You can control the Timeline. You have animation controls. You can load and unload content, you know SWF files, JPEGs, audio and video as well and even some generic event handlers. But what I'm going to do is I'm just going to open up this Actions panel, just to kind of give you a quick preview. A popular one on this folder is clicking to Go to a web page, but Timeline Navigation is what I want to focus on, because again, at Frame 29, I want to stop at that frame.
So all I need to do is go to the frame I want to stop the Timeline at and double-click on that code snippet. It automatically adds that code snippet. Here it is. This is actual code that does although work, but it also gives you a comment right here, which tells you what is going on. So this is perfect. I can learn and then also see the code and even modify it. I know this is really just a really simple example, but you'd be surprised at how often you'll end up using this stop action right here.
Okay, well, where did I put this stop action? Well, down here it tells me it actually creates this Actions layer and in Frame 29 is where this code exists. So let's take a look at that. I'm going to close my Actions panel. And I'll scroll up to the top. And look at that. There it is, the Actions layer, Frame 29, right there. See that little A right there. That little A says that there are some ActionScript associated to that frame. So I can go to Window > Actions.
And there's my code. Okay, so I have actually stopped the Timeline at this point, but what I want to do next is I want to add functionality to these buttons. Now I'm not going to do that at Frame 29. What I'm going to do is I'm going to do that at Frame 1, okay. So right here at this very first frame, in case the user wants to jump directly to the Contact section, they don't have to wait for this animation to play. So it's all about usability here. One key thing to note is to make sure your graphics exist at the point in time that you apply your actions.
Okay, so again, I want to go ahead and apply some code to each one of these four buttons. And if I go to my Properties panel, and with this button selected, well, let's take a look at it. It's just an instance of the generic button. And in fact there are four of them. So I need to give each one a unique name in order to apply some code to it. I first need to give it a unique instance name. So right up here in my Properties panel, I'm going to give this an instance name of about_btn, just like that.
Now the ActionScript code I apply to the button is going to recognize this as that unique button, but I'm going to go through for each one of these and give each one an instance name, a unique name that clearly defines it. portfolio_btn. Don't use any spaces. Don't start with any special characters or numbers, just to keep it simple. contact_btn, just like that.
So each one of these buttons has an instance name. Now I can apply code to it. All right, so with this button selected, I can go to my Code Snippets panel. And in my Timeline Navigation folder, I can Click to Go to Frame and Play. And that's what I want to do. I actually want to, if I expand up this Timeline more, I'm just going to close this up actually. I want to go to Frame 30, right here. And I want to play it.
And it's going to play through and then right there. But I don't want it play through into the other sections. So again, what I need to do is add some other stop actions. So I'm going to stop at this frame here. It adds the code at Frame 49, as it shows me there. Close that, kind of move on down, right here also for the Portfolio section. Double-click and stop at this frame. And then lastly for the Contact section, I want to double-click on that code snippet to stop it at that frame.
All right, so my stop actions are in place. And again, if I go back here to my very first frame and select that button, I can select Click to Go to Frame and Play. So I'm going to ahead and apply it to that selected button, just by double-clicking on it. And it adds that code to it. Remember, it adds at this very first frame. That's what that little A is. So at the very beginning of this web site is where all my major code is going to live.
So again, here is my code, and I can start to modify it or whatever I need to do. First thing I'm going to do is I'm going to remove this panel off to the side just by clicking that left arrow. There we go. And now I can read this clear, because this comment not only tells me what it does; it gives me instructions on how to modify it. So I need to replace the number 5 with the frame number you want to jump to. Okay, in this case, it's going to be Frame 30, because Frame 30 is where that About content starts to roll in.
All right, and each one's spaced out about 20 frames. So I have just to change that to gotoAndPlay(30). And let's go ahead and test out this button. It's really simple. I still need to apply this, but again, it stops at this point in time. And if I click on this button, it goes to Frame 30, plays it, and then it reaches that second stop action right there and stops. So that's perfect.
You know the next step is to make these other buttons to work, but it follows the same process. Again, I'm going to make sure I'm in Frame 1. I'm going to select the Portfolio button. And keep in mind that I'm not selecting the text. There's text on top. I'm actually selecting the button back there. So I will select Click to Go to Frame and Play. Apply that. And it applies that same code. And still it tells me to modify that frame number. And each one's spaced out about 20 frames.
So this is on Frame 50 is where-- well, actually, let's just double-check that. Yup! There is Frame 50, perfect. So it goes there and it adds this comment. These comments, if I could be honest, are getting kind of annoying because it's just taking up a lot of room. Well, you know what? They're not needed. So I can even delete that right there. Now I can read this better. And if I take a closer look at this code, you can start to see the difference and what's going on here, because it's going to give me my instance name and it adds an event listener to this button that listens for the CLICK.
And on CLICK, it's going to fire off this function right here. So it's great that I list out to this code here. I can modify it further if I wanted to, but in general that's the structure for a common button. All right, let's move on along. Contact button, select it, double-click, Click to Go to Frame and Play. It's about a frame what? 70, right there. That's for the Contact button again. And let's close this, because lastly, I'm just going to select this Home button and do the same thing, which is apply that code.
All right, and I'll delete that comment. Now, this I want to go to about Frame 10 and play. Now you can also move this code around. Since they are going in order from left -to-right, I can always take this Home button code and drag it up near the top like that. So now it's sort of more in order as they're laid out on this page of the nave bar, so Home, About, Portfolio and Contact. It's looking good.
I will do a Test Movie. Stops where it needs to stop, About section, Portfolio section, Contact section and then Home. So everything works exactly as expected. It's perfect. Code snippets not only provide you the code; it gives you direction on modifying and within minutes, you can have a fully functional web site.
- Understanding web design
- Creating buttons and web graphics in Photoshop
- Formatting text with the Text Layout Framework
- Using the Timeline
- Creating a custom looping animation
- Loading images in a gallery
- Creating invisible buttons
- Adding music and video to a site
- Customizing video playback controls
- Optimizing and publishing a web site