Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member

Simplifying code

From: Creating a Portfolio Web Site Using Flash CS4 Professional

Video: Simplifying code

Now that you have seen the basics of ActionScript, I am going to show you how you can simplify your code, which is going to cut down on errors and make your code more flexible. So I am going to go to this AS layer, select that first frame, and go to Window, and Actions. And the first thing I am going to do is pin my code by pressing that little Push-Pin icon. As you can see, as I scroll down, I am repeating many, many lines of code, this all looks the same. This looks the same, and it's pretty lengthy, and all that really changes from one function to the next is this parameter, ok, and this one, and then this one.

Simplifying code

Now that you have seen the basics of ActionScript, I am going to show you how you can simplify your code, which is going to cut down on errors and make your code more flexible. So I am going to go to this AS layer, select that first frame, and go to Window, and Actions. And the first thing I am going to do is pin my code by pressing that little Push-Pin icon. As you can see, as I scroll down, I am repeating many, many lines of code, this all looks the same. This looks the same, and it's pretty lengthy, and all that really changes from one function to the next is this parameter, ok, and this one, and then this one.

So what I want to do is create one major function that's going to accept these parameters and then move the content movie clip accordingly. So I am going to create that function right up here, above of the EventListeners. I am just going to go ahead and type in 'function,' and this function is going to be called moveContent, because the goal of this function is to move content. This function is going to accept these parameters: the x position, the y position, and then the rotation. So I need to type that right in here, theX, add a colon, and it's going to accept a number, so type in Number.

As soon as that is highlighted, you can hit Enter. It's going to accept theXPosition, and theYPosition. That's also going to be a Number, and it's going to accept theRotation, which is also going to be a Number. All right. All within those parentheses. It's not going to return anything, so as always, I am just going to type 'void,' open curly brace and then a closing curly brace.

So here is my function. Anything in here is going to execute when this function is being called. And what I want to do is I am just going to set up some trace statements. So trace is just some testing statements for me, so I can see that certain things are showing up. I can put whatever I want in here. I really want this letter X to appear, followed by the position that it receives, so theXPosition. So I am going to do Command+C, just to copy that, paste that right in there. In the Output panel, it's going to show me X and then whatever number gets passed into this function.

I am going to copy this, paste it a couple of more times, because all I need to do is modify theYPosition. Again, theYPostion is going to be displayed in the Output panel as well. And then lastly is theRotation, and again, I need to use this, theRotation. So my function is set up. It's going to accept these three parameters, and then it's just going to show the trace statements in my Output panel, just for testing purposes.

But what I need to do now is I need to call this moveContent function, and I am going to call it when I click on the homeClick. So just below this trace statement, I am just going to type in 'moveContent,' and I am going to pass three parameters into that moveContent function. Those three parameters, again, are these three. So I can take this 600, drop it right in there, add a comma, take this parameter, drop it right in there as theYPosition, and then lastly, is theRotation, so I need to take theRotation, which just happens to be 0, and I will drop that right in there.

All right, looking pretty good. It's going to pass these parameters into this function. It's going to show me in the Output window. And what I can do now is move this content, these three Tweens, and I can just select them all and move them right up into that function right there. I want to pass these parameters into this moveContent function, and I want these numbers to appear right here.

What I am going to do is I am just going to take this, theXPosition, and I am going to copy it and paste it right here. I am going to take theYPosition, because that's going to be its final YPosition, and then theRotation, I am going to take that and copy it and paste it right in there. All right. So you can see I have basically eliminated three lines of code from this function and put them up in this moveContent function. What I am going to see is everything is going to look the same, but my trace statements will appear in the Output panel.

So I am going to do a Control>Test Movie, and if I go to the About section, everything looks good. I am working with the Home button, so if I click on it, it rotates back into position, just as I expected. And the Output panel, if I can show you what that prints out, it gives me what's in that trace statement, this X, the Y, and then the Rotation. So I will open that up. Sure enough, this stuff matches up. So it's working really well, but you are really not seeing the true power of it yet.

I mean, you are probably thinking, "Geez, it did this five minutes ago." Well, let's start eliminating some of this code, because I want to do the same thing for these other functions when these other buttons are clicked on. So for the aboutClick, I want to do the same thing. It's the same function, this moveContent, moveContent, and in there, it's going to accept these three parameters. So I am just going to drop in these numbers, just like that, click and drag them over, select them, drag them on over.

You can do it how ever you want, but I am just doing a click and drag. Make sure you have commas separating each one. So that's looking pretty good, and this is the fun part, because I get to delete those three lines of code, and I get to simplify everything. I will even delete that comment. So you can see my code getting smaller and smaller. And the nice thing is is this will alleviate any errors, because there could be some error right in here. Well, since that's not going to exist anymore, since these are all parameters that are being passed into the moveContent function, it doesn't matter that some things might be misspelled, because the less typing, the less lines of code, probably the less errors there will be.

So it's looking pretty good so far. The last one I am going to do is the Portfolio button. moveContent, these three, and lastly, this rotation, drop that right in here, and eliminate those three lines of code. All right. So definitely everything is condensed down, and if I have these parameters correct, I am going to make sure I have my commas separating each.

Notice that I have three parameters for this moveContent function, for each one, that then gets passed into here, and then these Tweens get used. So this is going to work out quite well. Let's just go ahead and test it out. Clock on the About section, works like a charm. Video section, works great. Portfolio section, works great. And then I can click back on John Doe. And again, the great thing is is this is really flexible, because if I want to go in here and change some of this, say, for instance, I want to change this, instead of having a Regular smooth transition, I can change that to something else, like Bounce.

I don't have to change it in 12 different places. I can just change it in three different places and then it will affect all the transitions, and I can see that there's this sort of jarring bounce that happens with all of these. I don't particularly like it, but again, the nice thing is I can roll it back to what it was, no problem, because I just need to change it in these three places. That's the power of making one function and then just passing in the parameter, the one item you want to change, and then using it wherever you want to use it.

So the more you can simplify your code this way, the better off you will be, the more flexible your code will be, and the less errors you will have.

Show transcript

This video is part of

Image for Creating a Portfolio Web Site Using Flash CS4 Professional
 
Expand all | Collapse all
  1. 1m 41s
    1. Welcome
      1m 7s
    2. Using the exercise files
      34s
  2. 24m 1s
    1. Understanding web design
      2m 44s
    2. Creating a Flash file
      3m 18s
    3. Importing a design from Photoshop
      7m 45s
    4. Optimizing the design
      10m 14s
  3. 13m 59s
    1. Dynamically loading an image
      3m 37s
    2. Loading random images
      10m 22s
  4. 47m 38s
    1. Creating menu buttons
      8m 18s
    2. Adding functionality to buttons
      8m 56s
    3. Adding navigation
      9m 45s
    4. Creating transitions
      12m 0s
    5. Simplifying code
      8m 39s
  5. 26m 11s
    1. Creating web site sections
      7m 41s
    2. Dynamically loading sections
      11m 7s
    3. Linking to web sites and email addresses
      7m 23s
  6. 29m 18s
    1. Setting up a portfolio
      12m 31s
    2. Adding functionality to thumbnails
      10m 10s
    3. Allowing for unlimited thumbnails
      6m 37s
  7. 41m 17s
    1. Benefits of using XML
      4m 42s
    2. Loading XML content
      4m 34s
    3. Sorting XML content
      7m 32s
    4. Displaying XML content
      7m 35s
    5. Using XML to create thumbnails
      8m 41s
    6. Displaying content with thumbnails
      8m 13s
  8. 25m 59s
    1. Creating and importing video
      9m 47s
    2. Adding a Full Screen button
      4m 15s
    3. Creating custom video controls
      4m 50s
    4. Adding functionality to video thumbnails
      7m 7s
  9. 9m 12s
    1. Adding audio to buttons
      5m 30s
    2. Adding audio to dynamically created buttons
      3m 42s
  10. 21m 19s
    1. Applying professional transitions
      9m 26s
    2. Creating a preloader
      8m 56s
    3. Publishing a web site
      2m 57s
  11. 3m 15s
    1. Reviewing the "corporate" Flash site
      1m 40s
    2. Reviewing the "artistic" Flash site
      1m 35s
  12. 12s
    1. Goodbye
      12s

Start learning today

Get unlimited access to all courses for just $25/month.

Become a member
Sometimes @lynda teaches me how to use a program and sometimes Lynda.com changes my life forever. @JosefShutter
@lynda lynda.com is an absolute life saver when it comes to learning todays software. Definitely recommend it! #higherlearning @Michael_Caraway
@lynda The best thing online! Your database of courses is great! To the mark and very helpful. Thanks! @ru22more
Got to create something yesterday I never thought I could do. #thanks @lynda @Ngventurella
I really do love @lynda as a learning platform. Never stop learning and developing, it’s probably our greatest gift as a species! @soundslikedavid
@lynda just subscribed to lynda.com all I can say its brilliant join now trust me @ButchSamurai
@lynda is an awesome resource. The membership is priceless if you take advantage of it. @diabetic_techie
One of the best decision I made this year. Buy a 1yr subscription to @lynda @cybercaptive
guys lynda.com (@lynda) is the best. So far I’ve learned Java, principles of OO programming, and now learning about MS project @lucasmitchell
Signed back up to @lynda dot com. I’ve missed it!! Proper geeking out right now! #timetolearn #geek @JayGodbold
Share a link to this course

What are exercise files?

Exercise files are the same files the author uses in the course. Save time by downloading the author's files instead of setting up your own files, and learn by following along with the instructor.

Can I take this course without the exercise files?

Yes! If you decide you would like the exercise files later, you can upgrade to a premium account any time.

Become a member Download sample files See plans and pricing

Please wait... please wait ...
Upgrade to get access to exercise files.

Exercise files video

How to use exercise files.

Learn by watching, listening, and doing, Exercise files are the same files the author uses in the course, so you can download them and follow along Premium memberships include access to all exercise files in the library.


Exercise files

Exercise files video

How to use exercise files.

For additional information on downloading and using exercise files, watch our instructional video or read the instructions in the FAQ.

This course includes free exercise files, so you can practice while you watch the course. To access all the exercise files in our library, become a Premium Member.

Join now "Already a member? Log in

Are you sure you want to mark all the videos in this course as unwatched?

This will not affect your course history, your reports, or your certificates of completion for this course.


Mark all as unwatched Cancel

Congratulations

You have completed Creating a Portfolio Web Site Using Flash CS4 Professional.

Return to your organization's learning portal to continue training, or close this page.


OK
Become a member to add this course to a playlist

Join today and get unlimited access to the entire library of video courses—and create as many playlists as you like.

Get started

Already a member?

Become a member to like this course.

Join today and get unlimited access to the entire library of video courses.

Get started

Already a member?

Exercise files

Learn by watching, listening, and doing! Exercise files are the same files the author uses in the course, so you can download them and follow along. Exercise files are available with all Premium memberships. Learn more

Get started

Already a Premium member?

Exercise files video

How to use exercise files.

Ask a question

Thanks for contacting us.
You’ll hear from our Customer Service team within 24 hours.

Please enter the text shown below:

The classic layout automatically defaults to the latest Flash Player.

To choose a different player, hold the cursor over your name at the top right of any lynda.com page and choose Site preferencesfrom the dropdown menu.

Continue to classic layout Stay on new layout
Exercise files

Access exercise files from a button right under the course name.

Mark videos as unwatched

Remove icons showing you already watched videos if you want to start over.

Control your viewing experience

Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.

Interactive transcripts

Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.

Are you sure you want to delete this note?

No

Your file was successfully uploaded.

Thanks for signing up.

We’ll send you a confirmation email shortly.


Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

Keep up with news, tips, and latest courses with emails from lynda.com.

Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

   
submit Lightbox submit clicked
Terms and conditions of use

We've updated our terms and conditions (now called terms of service).Go
Review and accept our updated terms of service.