I am using Flash CS3 with ActionScript 2 and am having a problem getting getBytesLoaded and getBytesTotal to work properly. Probably I am taking the reading in the wrong spot, but I can’t figure out where I should be doing it. The code will read an xml file which contains pictures and url links to where I want each picture to go, and then loads and shows a picture for 3 seconds, then goes on to the next one. I am trying to show the preloader bar “LOADING” by adjusting the _xscale as a percentage of BytesLoaded/BytesTotal. The code should work, but my getBytesLoaded and getBytesTotal is either showing 0% or 100% and nothing in between. Here is my code and I have left some of my trace statements in the code below as well. The problem occurs in the [FONT=courier new,courier][SIZE=2]this.onEnterFrame = function()[FONT=arial,helvetica,sans-serif] which is about half way thru the code:[/FONT]
[/SIZE][/FONT]
function loadXML(loaded) {
if (loaded) {
xmlNode = this.firstChild;
image = [];
linkto = [];
description = [];
total = xmlNode.childNodes.length;
for (i=0; i<total; i++) {
image* = xmlNode.childNodes*.childNodes[0].firstChild.nodeValue;
linkto* = xmlNode.childNodes*.childNodes[1].firstChild.nodeValue;
description* = xmlNode.childNodes*.childNodes[2].firstChild.nodeValue;
}
firstImage();
} else {
content = "error: xml file not loaded!";
}
}
xmlData = new XML();
xmlData.ignoreWhite = true;
xmlData.onLoad = loadXML;
xmlData.load("slideshow/slideshow_info.xml");
/////////////////////////////////////
picInterval = 3000; //Interval between when pic auto switches to the next pic. It's in milliseconds, so 3000 is three seconds.
picTimer = picInterval; //This variable will be compared to getTimer function below
prev_btn.onRelease = function() { //Go to next pic
prevImage();
};
next_btn.onRelease = function() { //Go to next pic
nextImage();
};
linkto_btn.onPress = function(){ //Open the link in the current window
if (loaded == filesize) {
getURL(linkto_txt.text, "_self");
}
};
/////////////////////////////////////
p = 0;
this.onEnterFrame = function() {
filesize = picture.getBytesTotal();
loaded = picture.getBytesLoaded();
preloader._visible = true;
var percentLoaded:Number = Math.round((picture.getBytesLoaded() / picture.getBytesTotal()) * 100);
trace("PercentLOADED- "+percentLoaded);
if (loaded != filesize) {
trace("NotLOADED- "+loaded+" "+filesize);
preloader.preload_bar._xscale = 100*loaded/filesize;
picTimer = getTimer() + picInterval; //Need to keep resetting picTimer ahead while pic is loading, otherwise when a large pic finishes loading it will skip ahead several pics instead of to the next one
} else {
trace("YesLOADED- "+loaded+" "+filesize);
preloader._visible = false;
if (picture._alpha<100) {
trace("AlphaPERCENT- "+picture._alpha);
picture._alpha += 10;
}
}
if (getTimer() > picTimer) { //Automatically go to the next image after picInterval milliseconds
nextImage();
}
};
////////////////////////////////////
function displayImage(q) {
picture._alpha = 0;
picture.loadMovie(image[q], 1);
//desc_txt.text = description[q];
//linkto_txt.text = linkto[q];
picture_num();
picTimer = getTimer() + picInterval;
}
function nextImage() {
if (p<(total-1)) {
p++;
} else { //this else statement to loop from last image back to first
p=0;
}
if (loaded == filesize) {
displayImage(p);
}
}
function prevImage() {
if (p>0) {
p--;
} else { //this else statement to loop from 1st to last image
p=total-1;
}
if (loaded == filesize) {
displayImage(p);
}
}
function firstImage() {
if (loaded == filesize) {
displayImage(0);
}
}
function picture_num() {
current_pos = p+1;
pos_txt.text = current_pos+" of "+total;
}
Here is what the [FONT=courier new,courier][SIZE=2]slideshow/slideshow_info.xml [FONT=arial,helvetica,sans-serif]file looks like[/FONT]:[/SIZE][/FONT]
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<images>
<pic>
<image>slideshow/pic1.jpg</image>
<linkto>http://www.google.com</linkto>
<caption>Info Slide #1</caption>
</pic>
<pic>
<image>slideshow/pic2.jpg</image>
<linkto>http://www.cnn.com</linkto>
<caption>Info Slide #2</caption>
</pic>
</images>
Thanks