Hi All,
I’ve learnt recently that AS has an issue with loading data from an XML in the order that it is presented in the XML file and this is affecting an image Gallery which I created.
Basically the images are not displaying in the order I want and here is the code I am using (Note: All works fine under Windows, but run it under Linux and the load order is out):
import mx.transitions.Tween;
import mx.transitions.easing.*;
var myShowXML = new XML();
myShowXML.ignoreWhite = true;
myShowXML.load("slideshow.xml?"+Math.random());
//XMLUtils.sortXMLByAttribute(myShowXML,'count');
myShowXML.onLoad = function()
{
_root.myWidth = 1080;
_root.myHeight = 1920;
_root.mySpeed = 2;
_root.myImages = myShowXML.firstChild.childNodes;
_root.myImagesNo = myImages.length;
createContainer();
callImages();
};
function createContainer()
{
_root.createEmptyMovieClip("myContainer_mc",1);
myContainer_mc.lineStyle(5,0x000000,100);
myContainer_mc.lineTo(_root.myWidth,0);
myContainer_mc.lineTo(_root.myWidth,_root.myHeight);
myContainer_mc.lineTo(0,_root.myHeight);
myContainer_mc.lineTo(0,0);
myContainer_mc._x = (Stage.width - myContainer_mc._width) / 2;
myContainer_mc._y = (Stage.height - myContainer_mc._height) / 2;
}
_global.xml_time = new Array();
function callImages()
{
_root.myMCL = new MovieClipLoader();
_root.myPreloader = new Object();
_root.myMCL.addListener(_root.myPreloader);
_root.myClips_array = [];
_root.myPreloader.onLoadStart = function(target)
{
_root.createTextField("myText_txt",_root.getNextHighestDepth(),0,0,100,20);
_root.myText_txt._x = (Stage.width - _root.myText_txt._width) / 2;
_root.myText_txt._y = (Stage.height - _root.myText_txt._height) / 2;
_root.myText_txt.autoSize = "center";
_root.myText_txt.text = "test";
};
_root.myPreloader.onLoadProgress = function(target)
{
_root.myText_txt.text = "Now loading.. " +
_root.myClips_array.length + "/" + _root.myImagesNo + " Completed";
};
_root.myPreloader.onLoadComplete = function(target)
{
_root.myClips_array.push(target);
target._alpha = 0;
if (_root.myClips_array.length == _root.myImagesNo)
{
_root.myText_txt._y = myContainer_mc._y + myContainer_mc._height;
_root.target_mc = -1;//moveSlide();
//myShowInt = setInterval(moveSlide, (_root.mySpeed*1000)+1000);
}
};
//trace(_root.myClips_array);
//myImages.sortOn(sort_array.attributes.count);
//trace(_root.myImages);
//_root.myImages.sortOn("count", Array.NUMERIC | Array.DESCENDING);
//_root.myImages.sortOn("count", Array.NUMERIC | Array.DESCENDING);
//_root.myImages.sortOn ("count");
//
//trace(_root.myImages[0].attributes.count);
/*trace(_root.myImages[0]);
trace(_root.myImages[1]);
if(_root.myImages[1].attributes.count==1){
_root.myImages[0].attributes.url=_root.myImages[1].attributes.url;
trace("veve");
}*/
//trace(_root.myImages);
/*****begin of sort array******/
var entry_items:Array = new Array();
for (var i:Number = 0; i < _root.myImagesNo; i++)
{
var temp_item:Object = new Object();
temp_item.count_num = Number(_root.myImages*.attributes.count);
temp_item.entry = _root.myImages*;
entry_items.push(temp_item);
}
entry_items.sortOn("count_num",Array.NUMERIC);
_root.myImages = [];
for (var i:Number = 0; i < entry_items.length; i++)
{
//trace(entry_items*.count_num);
//trace(entry_items*.entry);
_root.myImages* = entry_items*.entry;
//trace(entry_items*.entry.attributes.url);
}
/*****end of sort array******/
for (i = 0; i < _root.myImagesNo; i++)
{
temp_url = _root.myImages*.attributes.url;
temp_mc = myContainer_mc.createEmptyMovieClip(i, myContainer_mc.getNextHighestDepth());
xml_time* = _root.myImages*.attributes.duration;
//trace("this xml_time " + xml_time*);
//_root.myImages.count_num = _root.myImages*.attributes.count;
_root.myMCL.loadClip(temp_url,temp_mc);
}//i=0;
//
//_root.myImages = entry_items;
//trace(_root.myImages[0]);
}
_global.stop_time;
stop_time = 1000;
_global.num;
num = 0;
setTimer(stop_time);
function setTimer(stop_time)
{
myShowInt = setInterval(Timer_1, stop_time);
}
function Timer_1()
{
//trace("num is " + num);
stop_time = xml_time[num];
//trace("this stop_time " + xml_time[num]);
if (num < (_root.myImagesNo-1))
{
moveSlide();
num++;
} else {
moveSlide();
num = 0;
}
}
function moveSlide()
{
current_mc = _root.myClips_array[_root.target_mc];
new Tween(current_mc, "_alpha", Strong.easeOut, 100, 0, 1, true);
_root.target_mc++;
if (_root.target_mc >= _root.myImagesNo)
{
_root.target_mc = 0;
}
_root.myText_txt.text = _root.myImages[target_mc].attributes.title;
next_mc = _root.myClips_array[_root.target_mc];
new Tween(next_mc, "_alpha", Strong.easeOut, 0, 100, 1, true);
clearInterval(myShowInt);
setTimer(stop_time);
}
And here is the XML I am using:
<?xml version="1.0"?><slideshow id="18"><image url="green.jpg" title="" duration="10000" count="1"/><image url="yellow.jpg" title="" duration="5000" count="2"/><image url="pink.jpg" title="" duration="1500" count="3"/><image url="red.jpg" title="" duration="3000" count="4"/><image url="blue.jpg" title="" duration="2000" count="5"/></slideshow>
A friend who battles that same thing in Java suggested the problem lies here:
I don’t know this programming language well enough to be able to tell you what to change. However your problem is that you are displaying
_root.myClips_array, and it will be in the order the images are
loaded, because of
_root.myPreloader.onLoadComplete = function(target) {
_root.myClips_array.push(target);
}
That bit of code is called each time an image finishes loading. The
just-loaded image is added to the end of myClips_array, with no regard
to the original image order. So you need to find some way to ensure
the the loaded images are in the same order as in the xml file.
But I do not know how to change my code accordingly.
If anyone out there could lend a helping hand it would be greatly appreciated.
Thanks
Tim