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

Loading random images

From: Creating a Portfolio Web Site Using Flash CS4 Professional

Video: Loading random images

One thing you will always want to consider is how to keep your site fresh and visitors returning. One easy way to do this is by loading in different content for each visit. And that's what I want to do, in this case. I want to load in a different background each time the page is refreshed. Okay, and I really want to pick from a pool of about 10 backgrounds I already have set up. So let's go ahead and take a look at how this is currently set up. The background is being loaded in, using this UILoader component.

Loading random images

One thing you will always want to consider is how to keep your site fresh and visitors returning. One easy way to do this is by loading in different content for each visit. And that's what I want to do, in this case. I want to load in a different background each time the page is refreshed. Okay, and I really want to pick from a pool of about 10 backgrounds I already have set up. So let's go ahead and take a look at how this is currently set up. The background is being loaded in, using this UILoader component.

So if I go to Window down to Component Inspector, you can see that the source says that it's pulling from images/Bkgd1.jpg. Well, it's not realistic from you to come in here and change this every 15 minutes and then republish the file. But I could, I guess, do that, but what you're probably thinking, "There should be an easier way," and there is. And this way is done in ActionScript. Okay, so I'm going to use Flash's programming language to change this number here.

I'm going to pick a random number from 1-10, and then replace that number. Okay, so just close that panel. First thing you need to do, if you're going to write ActionScript, is put ActionScript in a layer, and create a new layer just by clicking this Page icon, and call AS. And all of your ActionScript should really go on one frame in your Timeline, preferably in your main Timeline, just right here. That's where I want to keep all of my code. So with that frame selected, I'm going to Window, down to Actions, and that will open up the Actions panel. All right.

First off, notice that there is this panel off to the side. I could use some of that content, but I'm going to do is I'm going to type in, right in this area, I'm going to write my ActionScript to right in here. And what I want you to get used to doing is start just typing in, in Plain English what you want to do. And in this case, what I want to do is I want to LOAD A RANDOM BACKGROUND (FROM 1 TO 10). That's basically what I want to do. So just in Plain English, type in what you want to do.

And if you highlight that line, what you can do is tell Flash to ignore this line by selecting Apply line comment. That will enable Flash to just ignore that line, and then move on down to what you wanted to render out. So in this case, what I want to do is I need to create a Random number. And this Random number is really going to go in a container called a variable. So I'm going to create this var randomNumber, and what's going in this container is a number, so I need to tell Flash to expect a Number.

So again, think of a variable as just an empty box that you can put anything into, and in this case, I am telling Flash that it's going to contain a number, and sure enough, that's going to be equal to whatever number you want it to contain, but I want it to be a Random Number. So first thing you might want to consider is just typing in 'random.' And notice that it's a keyword, and if it changes colors, it means that it's a keyword, and you really explore what it really does. So I'm going to go ahead and View Help, just by right clicking on that word, and it will open up Flash's Help, and it tells me what random is.

Now if I scroll up near the top, notice that it's part of the Math Class. And the Math Class has many methods, one of them being the random method, but you can do so many different things. So again, if I select that, it's just going to tell me that it's going to pick a random number between 0 and 1. It's not going to be a whole number. It's going to be like 0.3258, something like that. But it does generate a random number, and that's what I want. So I want to use Math.random.

So let me just minimize that, go back in Flash and type in Math, because again, it's part of the Math Class. It's this random method, and then just like it showed me in help, I need to have open and close parentheses. Now, this will return a random number, and it puts it in this container. All right! Now for testing purposes, what you're going to want to do is use a trace statement. And all this does is helps me check what's inside of random number.

Just for my purposes, the end-user will never see any sort of trace statement, but it really helps me figure out sort of how my code is working. So I'm going to go to Control and Test Movie, and we're going to see what happens with the trace statement when I add that in. What happens with the trace statement is it prints out what was in that trace statement, and this is what that random number is .63 yada, yada. So let's just do a Test Movie, and I'll show you that it does generate a different number.

So it is picking your random number each time. It's working great. But this is really random, and isn't really useful for my specific case. So I need to do some more things with it. In fact, I need it to pick from a number, say, between 1 and 10, Okay? So what I can do is say, "Hey, Math.random? I need to multiply you by 10." Now if I run it, it's going to pick a number between 1 and 10. So I'm just going to hit Command+Enter, or Ctrl+Enter if you're on a PC, and you can see, in the Output Panel, it's showing me that it is actually picking a random number between 1 and 10.

But I still have an issue, because I don't have my image named this. I just have one background, two background, three background, or background three, but I don't have their whole numbers, so I need to round this to a whole number. And again, this - if I check out the Math Class again, I can see what else I can do. For instance, I can round down, maybe using floor, or I can even, Returns the ceiling, I can round up using ceil, so that's what I want to do. I want to round up.

So 1.5 will actually become 2. 2.9 will actually become 3, and so on. So Math.ceil, and then within those parentheses, it will go ahead and round up anything that's in there. Let's check this out now, Test Movie. Finally, a whole number, and I'll just hit Command+Enter a couple more times, and you can see it's generating a whole number. Finally, something pretty useful.

So now that it's returning a whole number between 1 and 10, let's make sure I have my images in place. Because here is my images, Background 1 through 10. Great, they are all set up. I need to be aware of how this is named, because now I need to use this random number. So another line and what I'm going to do now is I'm going to pin my code, and what pinning your code does is it just always keeps it up. So if I actually click off in the Timeline anywhere, in some other location, my code disappears, But if you're always writing code in one place, what you want to do is bring up that code and hit this little push-pin, Pin active script, because I always want this to be up, regardless of what I am clicking on.

In this case, I want to click on the UILoader component, because I want to kind of compare what that component is doing with my code. So UILoader component selected. I'm going to go to Window>Component Inspector. Here is my Component Inspector panel, source, images/Bkgd1.jpg. All right. I need to write this in ActionScript, and the first thing I need to do is I need to give that UILoader an instance name. I need to give it a specific name, so I can reference it in my ActionScript.

Just close that, select my UILoader component. Right up in here, I want to type 'bkgdImage.' This is known as an instance name. It's just a specific name. And what I encourage you to do is just do Command+C, just to copy it, because I'm going to use that in my ActionScript, Command+V, bkgdImage. So now that UILoader has a specific name. I can reference it. I can change its source to "images/Bkgd1.jpg," just like that.

All right. So it's doing an exactly the same thing as this Component Inspector panel is doing. What I want to do is I want to replace this number with a random number. So in order to do that, I am just going to make sure this is text, and then I want to add this random number, so I'll, again, Copy, Paste. I encourage you to copy and paste, just to eliminate any sort of mistypes or typos. But now what's going on is it's going to look in the images folder for a background, whatever random number we pick .jpg. All right.

Everything is looking good, and one thing you can do is, in your Actions panel, you can check the syntax. So I can go ahead and hit that checkbox. And say this script contains no errors. That's great, but I really won't know if it's working until I do a Test Movie. Sure enough, there it is, looks like a different background to me, and it's bkgd6.jpg. And I can do a Command+Enter, or Ctrl+Enter if you're on a PC, and you can see a different background 8, background 5 loading in, so each time the user visits the site, it's going to go ahead and pull up a different image, which works out great, a great way to keep your site fresh, and hopefully, visitors returning.

It really does a good job of just really appealing to your portfolio as well, seeing as you can have sort of an unlimited, virtually unlimited number of images as your background that you can pull from, and again, the fact that your site is fresh just really enhances the experience for the user.

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.