PDA

View Full Version : LaoDing random SWF files from XML file without repeating



zohazoya
August 26th, 2008, 01:10 PM
Hey all,

i have been searching for 2 days for a solution for this problem I am having. I would appreciate it if someone would tell me if its even doable so i wouldb e be wasting my time

I am trying to upload external swf files into an empty movie in flash via xml( the name of the swf files will be pulled and read from the xml file so we could easily update it). I want it to start loading any random swf file form the list and then within that be able to go through the other swf file( by clicking on a next button) .

the problem is that i need to remember what swf files it already displayed so it would repeat showing the same files.

i think i need to add an array but i am not sure how to work with that.

This is what i have so far in my action script:jailbreak
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(Ev ent.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) );
}


Waiting to hear from someone

JonnyR
August 27th, 2008, 03:58 AM
Hi zohazoya, and Welcome to the Kirupa Community.

First off, please wrap all code in [ as ] [ /as ] tags to help with legibility.

You could indeed use an Array to keep track of the loaded .swf files - I recommend you are up on Array.push() (http://www.adobe.com/support/flash/action_scripts/actionscript_dictionary/actionscript_dictionary064.html) and don't forget that your need to call new Array() (http://www.adobe.com/support/flash/action_scripts/actionscript_dictionary/actionscript_dictionary059.html) to initialise it before use.

Let us know if you get stuck
Jonny.

Anil_kumar
August 27th, 2008, 04:25 AM
its solved

download its source file from here
http://www.2shared.com/file/3830221/f7465b7d/Random_Loader_-_Copy.html


var pHeight:Number = 200;
var pWidth:Number = 200;

var SWFArray:Array = new Array();
var InformArray:Array = new Array();

var stImage:String;

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.swf.length();

for each (var item:XML in xmlData..swf) {
SWFArray[item.@id.toString()]=item.name.toString();
}

//var stImage:String = xmlData.swf.@id[Math.floor(numImages*Math.random())].toString();
//stImage =SWFArray[ Math.floor(numImages*Math.random())];

selectFile();
//picLoader.contentLoaderInfo.addEventListener(Event .COMPLETE, gotPic);
//picLoader.load( new URLRequest("swf/"+stImage+".swf"));

listLoader.removeEventListener(Event.COMPLETE, gotList);

}

function selectFile():void {
picLoader = new Loader();
do {
stImage =SWFArray[ Math.floor(numImages*Math.random())];
} while (InformArray.indexOf(stImage)!=-1);
InformArray.push(stImage);
trace(InformArray);
picLoader.contentLoaderInfo.addEventListener(Event .COMPLETE, gotPic);
picLoader.load( new URLRequest("swf/"+stImage+".swf"));
}

//var thisBmp:Bitmap;

function gotPic(evt:Event):void {

thisBmp.source=picLoader.content;


//thisBmp = Bitmap(picLoader.content);
//thisBmp = 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(Ev ent.COMPLETE, gotPic);
if (InformArray.length!=SWFArray.length ) {
btnAnother.visible = true;
}
}

btnAnother.addEventListener(MouseEvent.CLICK, anotherPic);

function anotherPic(mevt:MouseEvent):void {

btnAnother.visible = false;

selectFile();


//removeChild(thisBmp);

//picLoader = new Loader();
//
//var stImage:String =SWFArray[ Math.floor(numImages*Math.random())];
//
//picLoader.contentLoaderInfo.addEventListener(Event .COMPLETE, gotPic);
//picLoader.load( new URLRequest("swf/"+stImage+".swf"));
}

Anil
anilkumarnd@gmail.com

zohazoya
August 27th, 2008, 08:34 AM
Hey Anil,

Thank you very much!! Its brilliant i just tested it and it works perfectly! You are a life saver!

One more thing though which i need to figure out:

How do you make it reset and start doing the same process after it picked all the files . In other words, when the loader realizes it played all the files in the XML file, it should reset it self and start loading again 1 random file then doing the same process it did before.

Thank you !

Anil_kumar
August 27th, 2008, 09:13 AM
edit your code or download a new source file from here
http://www.2shared.com/file/3831334/274993e1/RandomLoader2.html

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

var SWFArray:Array = new Array();
var InformArray:Array = new Array();

var stImage:String;

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.swf.length();

for each (var item:XML in xmlData..swf) {
SWFArray[item.@id.toString()]=item.name.toString();
}

selectFile();

listLoader.removeEventListener(Event.COMPLETE, gotList);

}

function selectFile():void {

picLoader = new Loader();


if (InformArray.length==SWFArray.length) {
InformArray.splice(0,InformArray.length);
}

do {
stImage =SWFArray[ Math.floor(numImages*Math.random())];
} while (InformArray.indexOf(stImage)!=-1);

InformArray.push(stImage);

trace(InformArray);

picLoader.contentLoaderInfo.addEventListener(Event .COMPLETE, gotPic);
picLoader.load( new URLRequest("swf/"+stImage+".swf"));
}



function gotPic(evt:Event):void {

thisBmp.source=picLoader.content;


picLoader.contentLoaderInfo.removeEventListener(Ev ent.COMPLETE, gotPic);
btnAnother.visible = true;



}

btnAnother.addEventListener(MouseEvent.CLICK, anotherPic);

function anotherPic(mevt:MouseEvent):void {

btnAnother.visible = false;

selectFile();

}
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

Anil
anilkumarnd@gmail.com

zohazoya
August 27th, 2008, 10:06 AM
Thanx alot! its working as it should now. Brilliant work. Thanx a million again

Nice one!;)

zohazoya
September 3rd, 2008, 11:01 AM
:( I am having a problem in making it work when inserting the SWF in HTML (dreamweaver). It is working fine on its own but when you put it inside the html div, it doesnt work.
To be more specific, it looks like it is just reading the main swf of UI loader but not linking the XML files or the toher swf files. Any Idea what is happening here?

this is the html

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

</div>
<script type="text/javascript">
var so = new SWFObject('movies/RandomLoader.swf', 'movie1', '400', '400', '8');
so.addParam('wmode', 'transparent');
so.write('movie1');
</script>
</div>
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

Thanx

Anil_kumar
September 3rd, 2008, 11:08 AM
post ur source files plz. let's solve it tomorrow

zohazoya
September 3rd, 2008, 01:09 PM
Here are the source files

http://www.2shared.com/file/3869394/d0f2dcf4/Test_Loader.html

thanx

Anil_kumar
September 4th, 2008, 02:50 AM
open your "RandomLoader.fla" and edit these lines i

line 9
var listLoader:URLLoader = new URLLoader( new URLRequest("filelist.xml") );
to
var listLoader:URLLoader = new URLLoader( new URLRequest("movies/filelist.xml") );


and
line 51
picLoader.load( new URLRequest("swf/"+stImage+".swf"));
to
picLoader.load( new URLRequest("movies/swf/"+stImage+".swf"));



when you edit your "RandomLoader.fla" don't try to test your movie bcoz it will search for the files "filelist.xml " and "*.swf" in the specified path and will not work as u expect

so just publish it using Shift+F12 (not Ctrl+Enter) and run your "ZZ.htm"

it should work

otherwise contact me :}



Anil
Flash Workshop (http://flash-workshop.blogspot.com/)
anilkumarnd@gmail.com

zohazoya
September 4th, 2008, 07:16 AM
Wooohooo it worked!:beam:
Thanx again!:) You Rock!