all groups > flash actionscript > march 2007 >
You're in the

flash actionscript

group:

Loading random images in intervals


Loading random images in intervals jfalberg
3/29/2007 8:34:54 PM
flash actionscript:
I am very new to doing ActionScripts and I want to create an applet which reads
from an XML and builds an array of the URL for images, then loads the images in
a repeat with about 5 second intervals without the use of any buttons. Here's
what I have in my actionscript coding, but what does not seem to work is the
ability to switch images in a loop. How can I do that?



ap_xml = new XML();
ap_xml.ignoreWhite = true;
ap_xml.load('imglist.xml');
ap_xml.onLoad = function(success) {
if (success) {
xml2Array(this);
}
};

function xml2Array(filexml):Boolean {
var aImgFiles:Array = new Array();
aImgFiles = parseFile(filexml);
aImgFiles = myScramble(aImgFiles);

var mcLoader:MovieClipLoader = new MovieClipLoader();
mcLoader.addListener(loadListener);

var mc:MovieClip = this.createEmptyMovieClip("mc",
this.getNextHighestDepth());
for (j = 0; j < 5; j++) {
mcLoader.loadClip(aImgFiles[(j % aImgFiles.length)], mc);
}
return true;
}

function parseFile(xmlDoc_xml):Array {
var temp = new Array();
for (var a = 0; a<xmlDoc_xml.firstChild.childNodes.length; a++) {
temp.push(xmlDoc_xml.firstChild.childNodes[a].firstChild.nodeValue);
}
return temp;
}

function myScramble(fArray:Array):Array {
var tmpArray = new Array();
for(var i = fArray.length; i > 0; i--) {
var j = Math.random() * i;
j = Math.floor(j);
tmpArray.push(fArray[j]);
fArray.splice(j,1);
}
return tmpArray;
}
Re: Loading random images in intervals jfalberg
3/30/2007 1:25:38 PM
Re: Loading random images in intervals The Feldkircher
3/30/2007 6:11:23 PM
Hi

Can you post a sample of your XML structure, I can see how your loading your
image into your mc.
I think your problem is your creating a new emptymovieclip 'mc' on every pass,
and not loading it one pre-defined img_holder!!!!

Hope it helps
Re: Loading random images in intervals jfalberg
3/30/2007 8:28:51 PM
Thanks for the reply.

Here's a sample of my XML file as it is basically used to create a single
array to contain a relative URL of the image to use:

<?xml version="1.0" encoding="UTF-8"?>
<imglist>
<imgfile>images/N85735_101_12.jpg</imgfile>
<imgfile>images/N86579_101_12.jpg</imgfile>
<imgfile>images/N86712_101_12.jpg</imgfile>
<imgfile>images/N86716_101_12.jpg</imgfile>
</imglist>
Re: Loading random images in intervals The Feldkircher
4/1/2007 12:00:00 AM
OK - I have created a new .fla that will do what you want.
Send me an email address and I can send you the file.

Just mention that the randomization of images may need playing around with,
but the more images you have the less likely you will have repetition so close
together.

Ciao for Now
Re: Loading random images in intervals The Feldkircher
4/14/2007 12:00:00 AM
OK! to place multiples of the same Symbol on the Stage we need to change the
instance name slightly for each one.

The easiest way to do this is to place the amount (multiple) into a
for...loop. In your case you need four items

for(c=0;c:c<=3;c++)
{
var mc = home.attachMovie("imageContainer", "imageContainer" + c, 1+c);
mc._x = ?; // you will need to calculate x for each symbol
mc._y = ?; // also with y
}

So basically we have created imageContainer0 to imageContainer3 and using c+1
we have them on different depths.
Now all you have to do is reference each one in your loadImage loop.

Hope it helps
Re: Loading random images in intervals jfalberg
4/16/2007 12:00:00 AM
Thanks for your assistance The FeldKircher as I'm trying to understand the
following line of code:

var mc = home.attachMovie("imageContainer", "imageContainer" + c, 1+c);

The part I don't understand is whether the parameters only refer to the
objects in the library as opposed to each instance on stage and was wondering
if I have for example 4 different instances of "imageContainer" how can I
differentiate one from the other, or will I need to have 4 copies of the same
"imageContainer" object in the library with at least one of each on stage when
using 4 different of such.
Re: Loading random images in intervals SpaceGirl
4/16/2007 6:36:37 AM
[quoted text, click to view]

the second param is your new instance name, so you could create the
name on the fly

for (var i:0;i < 10; i++) {
home.attachMovie("nameOfMoveInLibrary", "newInstanceName" + i,
home.getNextHighestDepth());
}

This works because with each loop, the variable "i" increased by 1,
and then is appended to the instance name.

This would create 10 movies within "home" called "newInstanceName1",
"newInstanceName2" etc... You can then reference these just like any
other mc, such as:

home.newInstanceName1._x = 120;
Re: Loading random images in intervals The Feldkircher
4/16/2007 6:54:57 PM
Hi

No Problem, glad to help.

You got it, imageContainer is the Linkeage identifier of the library
MovieClip, imageContainer0..etc is the Instance name.

Extending what you have is a good idea, do you intend to open the image in its
own window or on Stage?.



Re: Loading random images in intervals jfalberg
4/17/2007 1:23:02 PM
I believe this will be as far as I'll go with this particular project though I
may revisit at some point later on. One thing I do notice is that it saves the
resource files to the Temporary Internet Files folder so I may want to limit
the image rotation to a certain amount. The images I am using are relatively
small (150 x 150 in pixels) so hopefully that won't be too much of a problem if
I limit it to say the first 80-100 images out of a possible many.
AddThis Social Bookmark Button