Creating an Advance Flash Image Gallery - Page 6
By Blue_Chi | Flash CS3 | ActionScript | Advanced | Page 1, 2, 3, 4, 5, 6, 7We are going to add in this section preloaders to all of our thumbnails and full images. Having a preloaders is important for galleries that load dynamic content on the internet because of the amount of time it could take to download the files. The trick for creating preloaders is discussed in detail in our previous MovieClipLoader Class tutorial. This section of our tutorial will have the following outline:
- Create a preloader for the thumbnails by doing the following:
- Use the existing listener to create a text field when the file starts loading.
- Display the percentage of how many bytes have loaded in this text field during the loading process.
- Remove this text field when the file completes loading.
- Create a preloader for the full image by doing the following:
- Create a listener.
- Create a text field when the file starts loading.
- Display the percentage of how many bytes have loaded in this text field during the loading process
- Display the image name in the text field when the file completes loading.
We will do each of these preloaders on their own, we will start off with the thumbnails one:
Creating a Thumbnail Preloader
We already have a listener for the thumbnails which we have used to assign the .onRelease code when the thumbnail completes loading. We are going to make additional usage of this listener to (1) detect when the loading process starts and (2) update us with loading progress.
We have to go back to the callThumbs function and add the .onLoadStart event listener. We are going to create a text field named my_text which we will use later to display what is happening. We will make this text unselectable to avoid the funny cursor when its highlighted look nicer.
_root.createEmptyMovieClip("container_mc",_root.getNextHighestDepth());
container_mc._x = _root.gallery_x;
container_mc._y = _root.gallery_y;
var clipLoader = new MovieClipLoader();
var preloader = new Object();
clipLoader.addListener(preloader);
for (i=0; i<_root.myImagesTotal; i++) {
thumbURL = myImages[i].attributes.thumb_url;
myThumb_mc = container_mc.createEmptyMovieClip(i, container_mc.getNextHighestDepth() );
myThumb_mc._y = _root.thumb_height*i;
clipLoader.loadClip("thumbs/"+thumbURL,myThumb_mc);
preloader.onLoadStart = function(target) {
target.createTextField("my_txt",target.getNextHighestDepth(),0,0,100,20);
target.my_txt.selectable = false;
};
preloader.onLoadComplete=function(target){
target.onRelease=function(){
callFullImage(this._name);
}
}
}
}
We will now update this text field throughout the loading progress by using the .onLoadProgress event listener.
_root.createEmptyMovieClip("container_mc",_root.getNextHighestDepth());
container_mc._x = _root.gallery_x;
container_mc._y = _root.gallery_y;
var clipLoader = new MovieClipLoader();
var preloader = new Object();
clipLoader.addListener(preloader);
for (i=0; i<_root.myImagesTotal; i++) {
thumbURL = myImages[i].attributes.thumb_url;
myThumb_mc = container_mc.createEmptyMovieClip(i, container_mc.getNextHighestDepth() );
myThumb_mc._y = _root.thumb_height*i;
clipLoader.loadClip("thumbs/"+thumbURL,myThumb_mc);
preloader.onLoadStart = function(target) {
target.createTextField("my_txt",target.getNextHighestDepth(),0,0,100,20);
target.my_txt.selectable = false;
};
preloader.onLoadProgress = function(target, loadedBytes, totalBytes) {
target.my_txt.text = Math.floor((loadedBytes/totalBytes)*100);
};
preloader.onLoadComplete=function(target){
target.onRelease=function(){
callFullImage(this._name);
}
}
}
}
Finally, we will remove this text field when the loading progress has completed:
_root.createEmptyMovieClip("container_mc",_root.getNextHighestDepth());
container_mc._x = _root.gallery_x;
container_mc._y = _root.gallery_y;
var clipLoader = new MovieClipLoader();
var preloader = new Object();
clipLoader.addListener(preloader);
for (i=0; i<_root.myImagesTotal; i++) {
thumbURL = myImages[i].attributes.thumb_url;
myThumb_mc = container_mc.createEmptyMovieClip(i, container_mc.getNextHighestDepth() );
myThumb_mc._y = _root.thumb_height*i;
clipLoader.loadClip("thumbs/"+thumbURL,myThumb_mc);
preloader.onLoadStart = function(target) {
target.createTextField("my_txt",target.getNextHighestDepth(),0,0,100,20);
target.my_txt.selectable = false;
};
preloader.onLoadProgress = function(target, loadedBytes, totalBytes) {
target.my_txt.text = Math.floor((loadedBytes/totalBytes)*100);
};
preloader.onLoadComplete=function(target){
target.my_txt.removeTextField();
target.onRelease=function(){
callFullImage(this._name);
}
}
}
}
This completes our thumbnail preloader, you can test the preloader on your local machine by testing your movie and then pressing Ctrl+Enter again to Simulate Download.
Creating a Full Image Preloader
Adding a preloader to our full images requires the same procedure, but because we do not have a listener created for it we will have to create one before we start. So go to your callFullImage() function and create one:
myURL = myImages[myNumber].attributes.full_url;
_root.createEmptyMovieClip("fullImage_mc",_root.getNextHighestDepth());
fullImage_mc._x = _root.full_x;
fullImage_mc._y = _root.full_y;
var fullClipLoader = new MovieClipLoader();
var fullPreloader = new Object();
fullClipLoader.addListener(fullPreloader);
}
We now have to create a text field for the .onLoadStart event listener, this text field will later display the status of the download:
myURL = myImages[myNumber].attributes.full_url;
_root.createEmptyMovieClip("fullImage_mc",_root.getNextHighestDepth());
fullImage_mc._x = _root.full_x;
fullImage_mc._y = _root.full_y;
var fullClipLoader = new MovieClipLoader();
var fullPreloader = new Object();
fullClipLoader.addListener(fullPreloader);
fullPreloader.onLoadStart = function(target) {
target.createTextField("my_txt",target.getNextHighestDepth(),0,0,200,20);
target.my_txt.selectable = false;
};
}
We will now display the percentage of how much has loaded using the .onLoadProgress event listener:
myURL = myImages[myNumber].attributes.full_url;
_root.createEmptyMovieClip("fullImage_mc",_root.getNextHighestDepth());
fullImage_mc._x = _root.full_x;
fullImage_mc._y = _root.full_y;
var fullClipLoader = new MovieClipLoader();
var fullPreloader = new Object();
fullClipLoader.addListener(fullPreloader);
fullPreloader.onLoadStart = function(target) {
target.createTextField("my_txt",target.getNextHighestDepth(),0,0,200,20);
target.my_txt.selectable = false;
};
fullPreloader.onLoadProgress = function(target, loadedBytes, totalBytes) {
target.my_txt.text = Math.floor((loadedBytes/totalBytes)*100);
};
}
The last part here is a bit trickier, we are going to display the image title, which we have placed in our XML file. This value could be extracted from our array of XML elements using the attributes property, the same way we are extracting our full image URL. To do this, we will first retrieve the title value and store it in a variable:
myURL = myImages[myNumber].attributes.full_url;
myTitle = myImages[myNumber].attributes.title;
_root.createEmptyMovieClip("fullImage_mc",_root.getNextHighestDepth());
fullImage_mc._x = _root.full_x;
fullImage_mc._y = _root.full_y;
var fullClipLoader = new MovieClipLoader();
var fullPreloader = new Object();
fullClipLoader.addListener(fullPreloader);
fullPreloader.onLoadStart = function(target) {
target.createTextField("my_txt",target.getNextHighestDepth(),0,0,200,20);
target.my_txt.selectable = false;
};
fullPreloader.onLoadProgress = function(target, loadedBytes, totalBytes) {
target.my_txt.text = Math.floor((loadedBytes/totalBytes)*100);
};
}
We will now simply assign this variable as the text of our text field when our full image is fully loaded:
myURL = myImages[myNumber].attributes.full_url;
myTitle = myImages[myNumber].attributes.title;
_root.createEmptyMovieClip("fullImage_mc",_root.getNextHighestDepth());
fullImage_mc._x = _root.full_x;
fullImage_mc._y = _root.full_y;
var fullClipLoader = new MovieClipLoader();
var fullPreloader = new Object();
fullClipLoader.addListener(fullPreloader);
fullPreloader.onLoadStart = function(target) {
target.createTextField("my_txt",target.getNextHighestDepth(),0,0,200,20);
target.my_txt.selectable = false;
};
fullPreloader.onLoadProgress = function(target, loadedBytes, totalBytes) {
target.my_txt.text = Math.floor((loadedBytes/totalBytes)*100);
};
fullPreloader.onLoadComplete = function(target) {
target.my_txt.text = myTitle;
};
}
Check it out now, test your movie and then Simulate Download by pressing Ctrl+Enter.
Summary of this section:
- Used the thumbnail listener to create a text field when the loading process starts.
- Updated the thumbnail text field using the listener to display the loading progrss.
- Created a full image listener and used it to create a text field when the loading process starts.
- Updated this text field using the listener to display the loading progress.
- Retrieved the value of the image title and displayed it in the text field when the loading process completes.
We now have completed all the technical aspects of our gallery. You can now use this fully functional gallery on the internet and update it with ease using the XML file. However, you might want to improve its feel by reading the final page of our tutorial.
- Next Page.
