Thursday, 15 January 2015

SHUFFLE IMAGES IN AS3



--------------------------------------------------------------------------------------
SHUFFLE IMAGES IN AS3 WITH XML FILE
----------------------------------------------------------------------------------

var pHeight:Number = 200;
var pWidth:Number = 200;
var listLoader:URLLoader = new URLLoader( new URLRequest("filelist.xml") );
var arrPicLoaders:Array = new Array();
var arrPicList:Array = new Array();
var numImages:Number;
listLoader.addEventListener(Event.COMPLETE, gotList);
function gotList(evt:Event):void {
var xmlData:XML = XML(listLoader.data);
numImages = xmlData.pix.length();
var arrShuffled:Array = shuffleList(numImages);
var stImage:String;
for (var i=0; i<numImages; i++) {
stImage = xmlData.pix[arrShuffled[i]].toString();
arrPicLoaders[i] = new Loader();
arrPicLoaders[i].contentLoaderInfo.addEventListener(Event.COMPLETE, gotPic);
arrPicLoaders[i].load( new URLRequest(stImage) );
}
listLoader.removeEventListener(Event.COMPLETE, gotList);
}
function shuffleList(n:Number):Array {
var startList:Array = new Array(n);
var endList:Array = new Array(n);
var i,j:Number;
for (i=0; i<n; i++) {
startList[i] = i;
}
for (i=0; i<n; i++) {
j = Math.floor(Math.random()*startList.length);
endList[i] = startList.splice(j,1)[0];
}
return(endList);
}
function gotPic(evt:Event):void {
var thisBmp:Bitmap = Bitmap(evt.target.content);
var thisWidth:Number = thisBmp.width;
var thisHeight:Number = thisBmp.height;
thisBmp.scaleX = pWidth/thisWidth;
thisBmp.scaleY = pHeight/thisHeight;
arrPicList.push(thisBmp);
if (arrPicList.length == numImages) {
placePictures();
}
}
function placePictures():void {
for (var i=0; i<arrPicList.length; i++) {
arrPicList[i].x = pWidth*Math.floor(i/2);
arrPicList[i].y = pHeight*(i%2);
addChild(arrPicList[i]);
}
}

--------------------------------------------------------------------------------------------------------
filelist.xml
XML FILE CODE
YOUR FLA AND XML FILE SHOULD BE SAVE IN SAME FOLDER
-----------------------------------------------------------------------------------------------------------






-----------------------------------------------------------------------------------------------------------

<?xml version="1.0" encoding="utf-8"?>
<imagelist>
<pix>https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg9bMH9FNuektGtBKB2kzE8pd6UmYCfyCDuacnBxgii0sXcvZRg0ygSKpojH05q1UugH6jH2J103gh_CA2TK9NVAMpDDUrPc209h1ityQ7HLzyEEkF2UvtCieswr7Mtw7dCGt1Zw-Hrq0P8/s1600/pic1.jpg</pix>
<pix>https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhEvvAlMDALeRvoPmrl9tDROWKQNxalCyI1strRMdcbbUqOCqmB_atuWvXJhzHgdUWuodZYsIrjATyg-gvEpxrFbBc3-I-Us_QdCaF0pAakstylK5xxMhp_Lhm_55fG7Os_rL4JsAXdpT5s/s1600/pic2.jpg</pix>
<pix>https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg7lBdFtyIBXesMvDQ1FbE6iEL3Fr-Zb9e2wZb330qA25aTMBgGd-yU0PSS-yB9jwqcUpE_iZaiGVaHyTmF5FdvmNVsHXMNFZtF80XlJG2Dqgumfu4lhkLh3XYja5V7VD7t1yubwClgSysO/s1600/pic3.jpg</pix>
<pix>https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhcPwpTzrkFKXF0_QWqlAwMInUo9HMnjiajIQ8Pn2jUtOfk4JQeGjQ4M8T7UEos2b16CribqNwc96uK8tJjZA9wJ0gTLuqKTiHelBZpcD6htiF2yxGFt_1ZAlocfmUz2yfBAvxbzYT-JajP/s1600/pic4.jpg</pix>
</imagelist>
-----------------------------------------------------------------------------------------------------------
EXAMPLE 2
ONLY RANDOM PIC CODE WITH ABOVE XML FILE
-----------------------------------------------------------------------------------------------------------
var pHeight:Number = 200;
var pWidth:Number = 200;
var listLoader:URLLoader = new URLLoader( new URLRequest("filelist.xml") );
var picLoader:Loader = new Loader();
listLoader.addEventListener(Event.COMPLETE, gotList);
function gotList(evt:Event):void {
var xmlData:XML = XML(listLoader.data);
var numImages:Number = xmlData.pix.length();
var stImage:String = xmlData.pix[Math.floor(numImages*Math.random())].toString();
picLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, gotPic);
picLoader.load( new URLRequest(stImage) );
listLoader.removeEventListener(Event.COMPLETE, gotList);
}
function gotPic(evt:Event):void {
var thisBmp:Bitmap = Bitmap(evt.target.content);
thisBmp.x = 0;
thisBmp.y = 0;
var thisWidth:Number = thisBmp.width;
var thisHeight:Number = thisBmp.height;
thisBmp.scaleX = pWidth/thisWidth;
thisBmp.scaleY = pHeight/thisHeight;
addChild(thisBmp);
picLoader.contentLoaderInfo.removeEventListener(Event.COMPLETE, gotPic);
}

-----------------------------------------------------------------------------------------------------------
EXAMPLE 3
RANDOM PIC CODE WITH BUTTON WITH ABOVE XML FILE

-----------------------------------------------------------------------------------------------------------

var pHeight:Number = 200;
var pWidth:Number = 200;
var listLoader:URLLoader = new URLLoader( new URLRequest("filelist.xml") );
var picLoader:Loader = new Loader();
listLoader.addEventListener(Event.COMPLETE, gotList);
var xmlData:XML;
var numImages:Number;
function gotList(evt:Event):void {
xmlData = XML(listLoader.data);
numImages = xmlData.pix.length();
var stImage:String = xmlData.pix[Math.floor(numImages*Math.random())].toString();
picLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, gotPic);
picLoader.load( new URLRequest(stImage) );
listLoader.removeEventListener(Event.COMPLETE, gotList);
}
var thisBmp:Bitmap;
function gotPic(evt:Event):void {
thisBmp = Bitmap(picLoader.content);
thisBmp.x = 0;
thisBmp.y = 0;
var thisWidth:Number = thisBmp.width;
var thisHeight:Number = thisBmp.height;
thisBmp.scaleX = pWidth/thisWidth;
thisBmp.scaleY = pHeight/thisHeight;
addChildAt(thisBmp,0);
picLoader.contentLoaderInfo.removeEventListener(Event.COMPLETE, gotPic);
btnAnother.visible = true;
}
btnAnother.addEventListener(MouseEvent.CLICK, anotherPic);
function anotherPic(mevt:MouseEvent):void {
btnAnother.visible = false;
removeChild(thisBmp);
picLoader = new Loader();
var stImage:String = xmlData.pix[Math.floor(numImages*Math.random())].toString();
picLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, gotPic);
picLoader.load( new URLRequest(stImage) );
}




EmoticonEmoticon