Loading JPGs Dynamically
Now that you've learned how to load an external movie into a project, the process of dynamically loading a JPG should be easyit's almost identical to that of loading an external SWF. As demonstrated in the preceding exercise, the loadMovie() action loads an external SWF into a target. You'll use the same action to load an external JPGbut with a twist. Look at the following syntax:
This loadMovie() action identifies a JPG as the external asset to load. Here, myBitmap.jpg is loaded into the myClip_mc target. After a JPG is loaded into a movie in this fashion, Flash sees it as a movie clip instance, which means that you can control it via ActionScriptrotating, resizing, or making it transparent as with any other clip.In this exercise, we'll build slideshow functionality into our project to demonstrate how we can load JPGs into a target on an as-needed basis.
loadMovie("myBitmap.jpg", "myClip_mc");
Open virtualaquarium2.fla.We'll continue working with this file from the preceding exercise.Note the empty movie clip instance on the Placeholder layer (the small white circle in the panel graphic's upper-left corner). Called placeholder_mc, this is the movie clip instance into which our external JPGs will be loaded. Also note the text field with the name title_txt, at the top of the panel; we'll use that in our exercise as well.


NOTEOnly standard JPG files are supported. Progressive JPG files are not supported.Return to Flash. With the Actions panel open, select Frame 1 of the Actions layer and add the following line of script just below the backgrounds array from the preceding exercise:
var slides:Array = new Array(["Shark", "image0.jpg"], ["Jellyfish",)"image1.jpg"],This step creates a new two-dimensional array named slides. As you learned in Lesson 6, "Creating and Manipulating Data," this is an array in which each element contains more than one item. The first part of each element is simply a string description of the image; the second parameter represents the directory path to that image.Now let's create a function to load these images into our project, with the functionality of a slideshow.Add the following script just below the randomBackground() function call:["Seahorse", "image2.jpg"]);
var currentSlide:Number;
function changeSlide(number:Number){
if (number >= 0 && number < slides.length){
currentSlide = number;
title_txt.text = slides[number][0];
loadMovie (slides[number][1], "placeholder_mc");
}
}

Because the last two actions in the function reference elements in the slides array, you can further break down those actions:
currentSlide = 1;
title_txt.text = slides[1][0];
loadMovie (slides[1][1], "placeholder_mc");
The result is that image1.jpg is loaded into the instance named placeholder_mc and the text string "Jellyfish" appears above the panel in the title_txt text field.
currentSlide = 1;
title_txt.text = "Jellyfish";
loadMovie ("image1.jpg", "placeholder_mc");

This line of script calls the changeSlide() function, passes that function a value of 0, and as a result displays the initial image when the movie is initially played. Note that this initial function call sets the value of currentSlide to 0, as defined in the changeSlide() function definitionan important point to keep in mind as you progress through the next couple of steps.Add the following event handler at the end of the current script:
changeSlide(0);
When the prev_btn instance is clicked, this script calls the changeSlide() function defined in Step 4. The parameter value passed to the function depends on how currentSlide - 1 is evaluated. When the movie initially plays, the changeSlide() function is called and passed a value of 0, as scripted in Step 5. Consequently, currentSlide is initially set to 0. Clicking the prev_btn instance just after the movie begins to play results in calling the changeSlide() function and passing a value of -1 (the result of subtracting 1 from the value of currentSlide). The if statement in the changeSlide() function then evaluates that value (-1) and prevents the function from executing (Step 4 explains why). As a result, this button does nothing until currentSlide has a value greater than 0. In other words, this button cannot navigate the slideshow beyond the initial image. The other arrow button (next_btn) increases the value, as shown in the next step.Add the following event handler after the script added in Step 6:
prev_btn.onRelease = function(){
changeSlide(currentSlide - 1);
}
This script adds an onRelease event handler to the next_btn instance, similar to the one in Step 6 but with one exception: the parameter value passed to the changeSlide() function depends on how currentSlide + 1 is evaluated. If currentSlide has a value of 0 when this button is clicked (after the movie begins playing), the changeSlide() function receives 1 (the result of adding 1 to the value of currentSlide). Remember that this value is evaluated by the if statement in the function. In this case, because the number is between 0 and 2, the if statement allows the continued execution of the function, which loads the appropriate JPG as described in Step 4. As the image is loading, the changeSlide() function updates the value of currentSlide based on the value passed to the function (1 in this case) so that the prev_btn and next_btn buttons we just scripted can act based on this value when subsequently clicked.The next_btn button instance cannot navigate the slideshow beyond the last image, because after currentSlide has the value 2, clicking this button again would send the changeSlide() function a value of 3 (which the if statement in that function would analyze, and then would prevent the function from executing).In tandem, these two buttons advance and rewind the slideshowthough only within the limits described.Choose Control > Test Movie.As soon as the movie begins playing, the image of a shark appears and the title_txt text field above the panel displays the text string "Shark". The upper-left corner of the image is placed at the registration point of the empty movie clip into which it was loaded.Click the prev_btn instance and nothing happens. Click the next_btn instance once, and the next JPG image loads; click it again and another JPG loads. However, if you click this button a third time, nothing happens because you've reached the upper limit of how far this button can advance. The prev_btn button instance has a similar restriction in the opposite direction.To add slides to this project, you simply add the appropriate data to the slides array and place the JPG files in the directory. The movie would then accommodate the new images automatically. This means that you can add hundreds of images without affecting the file size of the main movie. Because the images are external, you can easily bring them into a photo editor, update or change them, and then resave them, and your changes will be reflected the next time the project is played.
next_btn.onRelease = function(){
changeSlide(currentSlide + 1);
}
TIPTo remove a movie that has been loaded into an instance, so that the instance becomes empty, use the following syntax:
Close the test movie and save the file as virtualaquarium3.fla.This step completes the exercise. We'll build on this file in the following exercises.
unloadMovie("nameOfInstance")