# Random Image Generator

**URL:** <https://forum.kirupa.com/t/random-image-generator/260272>\
**Category:** flash\
**Created:** [May 14, 2008, 1:32am UTC](https://forum.kirupa.com/t/random-image-generator/260272 "2008-05-14T01:32:40Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Flightlessbird](https://avatars.discourse-cdn.com/v4/letter/f/cdc98d/32.png) [@Flightlessbird](https://forum.kirupa.com/u/Flightlessbird)\
**Post date:** [May 14, 2008, 1:32am UTC](https://forum.kirupa.com/t/random-image-generator/260272/1 "2008-05-14T01:32:40Z")

</div>

Hello,  
I’ve been looking at the Loading Random Background Image Tutorial ([http://http://www.kirupa.com/developer/mx/loadingrandombackground.htm )](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.

---

<div class="post-metadata">

**Author:** ![hasch2o](https://avatars.discourse-cdn.com/v4/letter/h/ba8739/32.png) [@hasch2o](https://forum.kirupa.com/u/hasch2o)\
**Post date:** [May 14, 2008, 3:08am UTC](https://forum.kirupa.com/t/random-image-generator/260272/2 "2008-05-14T03:08:45Z")

</div>

Hey Flightlessbird

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

```auto

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

---

<div class="post-metadata">

**Author:** ![Felixz](https://avatars.discourse-cdn.com/v4/letter/f/ecb155/32.png) [@Felixz](https://forum.kirupa.com/u/Felixz)\
**Post date:** [May 14, 2008, 12:41pm UTC](https://forum.kirupa.com/t/random-image-generator/260272/3 "2008-05-14T12:41:14Z")

</div>

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](http://www.kirupa.com/forum/attachment.php?attachmentid=46219&d=1209562183)

---

<div class="post-metadata">

**Author:** ![Flightlessbird](https://avatars.discourse-cdn.com/v4/letter/f/cdc98d/32.png) [@Flightlessbird](https://forum.kirupa.com/u/Flightlessbird)\
**Post date:** [May 14, 2008, 3:13pm UTC](https://forum.kirupa.com/t/random-image-generator/260272/4 "2008-05-14T15:13:51Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Felixz](https://avatars.discourse-cdn.com/v4/letter/f/ecb155/32.png) [@Felixz](https://forum.kirupa.com/u/Felixz)\
**Post date:** [May 14, 2008, 3:59pm UTC](https://forum.kirupa.com/t/random-image-generator/260272/5 "2008-05-14T15:59:53Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![doridori](https://avatars.discourse-cdn.com/v4/letter/d/3da27b/32.png) [@doridori](https://forum.kirupa.com/u/doridori)\
**Post date:** [May 14, 2008, 4:58pm UTC](https://forum.kirupa.com/t/random-image-generator/260272/6 "2008-05-14T16:58:06Z")

</div>

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.

```auto
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.

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

```

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

```auto
 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!

---

<div class="post-metadata">

**Author:** ![Felixz](https://avatars.discourse-cdn.com/v4/letter/f/ecb155/32.png) [@Felixz](https://forum.kirupa.com/u/Felixz)\
**Post date:** [May 14, 2008, 7:41pm UTC](https://forum.kirupa.com/t/random-image-generator/260272/7 "2008-05-14T19:41:45Z")

</div>

```auto
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

---

<div class="post-metadata">

**Author:** ![Flightlessbird](https://avatars.discourse-cdn.com/v4/letter/f/cdc98d/32.png) [@Flightlessbird](https://forum.kirupa.com/u/Flightlessbird)\
**Post date:** [June 1, 2008, 4:29pm UTC](https://forum.kirupa.com/t/random-image-generator/260272/8 "2008-06-01T16:29:18Z")

</div>

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;  
}

---

<div class="post-metadata">

**Author:** ![Flightlessbird](https://avatars.discourse-cdn.com/v4/letter/f/cdc98d/32.png) [@Flightlessbird](https://forum.kirupa.com/u/Flightlessbird)\
**Post date:** [June 1, 2008, 4:37pm UTC](https://forum.kirupa.com/t/random-image-generator/260272/9 "2008-06-01T16:37:36Z")

</div>

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