Photo Gallery
Using XML and Flash
        by kirupa  |  2 September 2004

This is page six of this tutorial, so if you stumbled here without having completed the previous page, click here to catch up on all the exciting stuff that you missed in the previous page. If you  haven't even started this tutorial, then head on over to the first page. I'll be waiting here until you finish.


Keyboard Input
The following code is used to help you cycle through the pictures by simply using your left and right arrow keys on your keyboard:

listen = new Object();
listen.onKeyDown = function() {
if (Key.getCode() == Key.LEFT) {
prevImage();
} else if (Key.getCode() == Key.RIGHT) {
nextImage();
}
};
Key.addListener(listen);

The line-by-line explanation:


listen = new Object();

I create a new object called listen because I want to give this object the properties of a "traditional" object such as a movie clip without actually creating a movie clip and placing it on stage.


listen.onKeyDown = function() {

This is the event handler for the listen object. I want this code to execute only when a key is pressed, and I used the onKeyDown handler to ensure that.


if (Key.getCode() == Key.LEFT) {
prevImage();
} else if (Key.getCode() == Key.RIGHT) {
nextImage();
}

Because I want Flash to react when the left or right arrow keys are pressed, the above code uses an if and else-if statement to check and react accordingly when the arrow keys are pressed.

When the left key is pressed, the prevImage() function is called, and when the right key is pressed, the nextImage() function is called. Those two functions will be covered later, so don't worry about them right now.


Key.addListener(listen);

This line enables the listen object to react to the onKeyDown handler when a key, in our example, is pressed.


Left and Right Buttons
The following is the code for getting the left and right buttons to work:

previous_btn.onRelease = function() {
prevImage();
};
next_btn.onRelease = function() {
nextImage();
};

While users can navigate using the arrow keys, having on-screen buttons makes your photo gallery more user-friendly. The above section of code uses each button's onRelease event handler to invoke either the prevImage() or nextImage() function!

If you remember, you gave the previous and next buttons the instance names previous_btn and next_btn a few pages ago.


Displaying Current Position and the Total Number of Pictures
In the photo gallery, you see two numbers. The first number represents where you are in the photo gallery with respect to the second number - the total number of images in the gallery.

The code responsible for displaying the above info is:

function picture_num() {
current_pos = p+1;
pos_txt.text = current_pos+" / "+total;
}

The first line declares the function picture_num(). Contained within this function is the variable current_pos that updates itself each time the variable p is increased or decreased. You will later find out that the variable p's value is modified within our, you guessed it, the nextImage and previousImage functions!

Our final line:

pos_txt.text = current_pos+" / "+total;
In the above line of code, I display the data from the current_pos and total variables into our text field named pos_txt. Notice that I am combining - concatenating - the variables current_pos and total with the / character.
 

Will I explain the elusive nextImage and previousImage functions in the next page? Go to the next page to find out.

Onwards to the next page!


page 6 of 9


 




SUPPORTERS:

kirupa.com's fast and reliable hosting provided by Media Temple.