Random Image Generator

Hello,
I’ve been looking at the Loading Random Background Image Tutorial (http://http://www.kirupa.com/developer/mx/loadingrandombackground.htm )
and found it very informative and helpful, except for the fact that it does not seem to work with ActionScript 3.0 . It looks as if the problem lies in the “choice” property.
Could someone suggest a solution, or alternative method to have three random images?

Code as it is now:

choice = Math.round(Math.random()*3);
switch (choice) {
case 0 :
location.loadMovie(“quotation0.swf”);
break;
case 1 :
location.loadMovie(“quotation1.swf”);
break;
case 2 :
location.loadMovie(“quotation2.swf”);
break;
}

Thank you.

Hey Flightlessbird

did you declare your variable “choice” somewhere?
if not, try this:


var choice:uint = Math.round(Math.random()*3);
switch (choice) {
case 0 :
location.loadMovie("quotation0.swf");
break;
case 1 :
location.loadMovie("quotation1.swf");
break;
case 2 :
location.loadMovie("quotation2.swf");
break;
}

hth

It’s not random, but timebased.
U can use that without date to get it random
http://www.kirupa.com/forum/attachment.php?attachmentid=46219&d=1209562183

The first solution provided did not work, because “the method loadMovie is no longer supported”. I’m assuming this is because ActionScript 3.0 does not use that anymore?

The second solution looks really complicated… Could you provide some information about what I’m supposed to be changing to make this applicable to my movie? I’d rather not fumble through your code and possibly mess the whole thing up.

u can remove that filter applying part and when function is loading an image just make it random

As you have already discovered yourself, the loadMovie method does not exist in AS3.

If you haven’t been working with AS3 before, it might take you some time to get this working in AS3.
I’ll tell you in short terms what you’ll have to do.

Generally, to load an external .swf, image, or video file, you use a Loader object.
This loader object will be replacing your current movieclip object called “location”.

So first you create an instance of the Loader class. Then you add it as a child of the stage to make it visible.

var location:Loader = new Loader();
addChild(location);

Now you have a Loader object on the stage. (Invisible so far, because it contains nothing)

Now you can access your Loader similarly to your previous location movieclip.
To load something into the Loader you use it’s *load *method.
You will also always have to create an URLRequest when loading an URL.

location.load(new URLRequest("mySwfUrl.swf"));

Now you can replace your AS2 loadMovie lines with this line.
The *switch *statement will look the same.

 choice = Math.round(Math.random()*3);
switch (choice) {
case 0 :
    location.load(new URLRequest("quotation0.swf"));
    break;
case 1 :
    location.load(new URLRequest("quotation1.swf"));
    break;
case 2 :
    location.load(new URLRequest("quotation2.swf"));
    break;
}

Most likely, there will be other places in your code that needs to be translated to AS3.
Anyways, hope this helps as a start.
Be sure to read up on some AS3 basics to get a deeper understanding, it helps alot and makes it easier to learn more!

location.load(new URLRequest("quotation" + int(Math.random()*3) + ".swf"));

in example ebove move can never load, beecause Math.round(Math.random()*3); can return: 0,1,2,3.
And switch is just waste

Doridori, the error I got after implementing your solution was “access of undefined property choice”

here’s what the code looks like now:
var location:Loader = new Loader();
addChild(location);

location.load(new URLRequest(“quotations.swf”));

choice = Math.round(Math.random()*2);
switch (choice) {
case 0 :
location.load(new URLRequest(“quotation0.swf”));
break;
case 1 :
location.load(new URLRequest(“quotation1.swf”));
break;
case 2 :
location.load(new URLRequest(“quotation2.swf”));
break;
}

Thank you Felixz! It works perfectly now. :slight_smile:
You saved me, thank you so much. ^^