# Select a random movieclip form an array of movieclips

**URL:** <https://forum.kirupa.com/t/select-a-random-movieclip-form-an-array-of-movieclips/264242>\
**Category:** Uncategorized\
**Created:** [June 24, 2008, 11:21am UTC](https://forum.kirupa.com/t/select-a-random-movieclip-form-an-array-of-movieclips/264242 "2008-06-24T11:21:02Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![samuk](https://avatars.discourse-cdn.com/v4/letter/s/91b2a8/32.png) [@samuk](https://forum.kirupa.com/u/samuk)\
**Post date:** [June 24, 2008, 11:21am UTC](https://forum.kirupa.com/t/select-a-random-movieclip-form-an-array-of-movieclips/264242/1 "2008-06-24T11:21:02Z")

</div>

Hi, iv been trying to find out how i can attach a random movieclip form an array of movieclips.

Iv got this so far, i have searched the forum etc for help and i am pretty stuck.

```php
var logo_arr:Array = new Array("logo01", "logo02", "logo03", "logo04", "logo05", "logo06", "logo07", "logo08", "logo09", "logo010", "logo011");
 
var randomLogo:Number = logo_arr[Math.floor(Math.random()*11)];
 
var dynamiclogo_mc = this.attachMovie("randomLogo", "randomLogo", 3);
 
dynamiclogo_mc._x = Stage.width/2 - 180 ;
dynamiclogo_mc._y = Stage.height/2 - 420;

```

All my movieclips are in the library and have the linkage for actionscipt enabled.

I cant see what it is im missing?

Any help is much apreciated

Thanks, SamI:-)

---

<div class="post-metadata">

**Author:** ![randomagain](https://avatars.discourse-cdn.com/v4/letter/r/35a633/32.png) [@randomagain](https://forum.kirupa.com/u/randomagain)\
**Post date:** [June 24, 2008, 12:14pm UTC](https://forum.kirupa.com/t/select-a-random-movieclip-form-an-array-of-movieclips/264242/2 "2008-06-24T12:14:01Z")

</div>

erm because you are attaching the same MC?

attachMovie([“logo”+randomLogo],randomlogo,different depth )

sort of

---

<div class="post-metadata">

**Author:** ![samuk](https://avatars.discourse-cdn.com/v4/letter/s/91b2a8/32.png) [@samuk](https://forum.kirupa.com/u/samuk)\
**Post date:** [June 24, 2008, 2:54pm UTC](https://forum.kirupa.com/t/select-a-random-movieclip-form-an-array-of-movieclips/264242/3 "2008-06-24T14:54:39Z")

</div>

Sorry, im a newb at this and do not understand your reply???

---

<div class="post-metadata">

**Author:** ![randomagain](https://avatars.discourse-cdn.com/v4/letter/r/35a633/32.png) [@randomagain](https://forum.kirupa.com/u/randomagain)\
**Post date:** [June 24, 2008, 3:00pm UTC](https://forum.kirupa.com/t/select-a-random-movieclip-form-an-array-of-movieclips/264242/4 "2008-06-24T15:00:08Z")

</div>

geez

I’ve improved the code a little, you need to give each MC a different name and a different depth

noob yourself 😛

> 

var logo\_arr:Array = new Array(“logo01”, “logo02”, “logo03”, “logo04”, “logo05”, “logo06”, “logo07”, “logo08”, “logo09”, “logo010”, “logo011”);

var randomLogo = logo\_arr[Math.floor(Math.random()\*11)];

var dynamiclogo\_mc = this.attachMovie(randomLogo, randomLogo, 3\*something);

dynamiclogo\_mc.\_x = Stage.width/2 - 180 ;  
dynamiclogo\_mc.\_y = Stage.height/2 - 420;

---

<div class="post-metadata">

**Author:** ![samuk](https://avatars.discourse-cdn.com/v4/letter/s/91b2a8/32.png) [@samuk](https://forum.kirupa.com/u/samuk)\
**Post date:** [June 24, 2008, 3:13pm UTC](https://forum.kirupa.com/t/select-a-random-movieclip-form-an-array-of-movieclips/264242/5 "2008-06-24T15:13:16Z")

</div>

Thanks verymuch,

iv been looking at it so long it just didnt notice the

```php
:Number

```

DOH

---

<div class="post-metadata">

**Author:** ![randomagain](https://avatars.discourse-cdn.com/v4/letter/r/35a633/32.png) [@randomagain](https://forum.kirupa.com/u/randomagain)\
**Post date:** [June 24, 2008, 3:24pm UTC](https://forum.kirupa.com/t/select-a-random-movieclip-form-an-array-of-movieclips/264242/6 "2008-06-24T15:24:54Z")

</div>

NEVER datatype lol

well maybe once you’ve finished

---

<div class="post-metadata">

**Author:** ![takethetrain](https://avatars.discourse-cdn.com/v4/letter/t/ba8739/32.png) [@takethetrain](https://forum.kirupa.com/u/takethetrain)\
**Post date:** [June 24, 2008, 3:33pm UTC](https://forum.kirupa.com/t/select-a-random-movieclip-form-an-array-of-movieclips/264242/7 "2008-06-24T15:33:50Z")

</div>

> [@randomagain](#):
>
> NEVER datatype lol

yikes, that’s unfortunate. there’s nothing wrong with datatyping–as long as you pick the correct type (which would be movieclip, not number, in this case).

gotta go with adobe on this one:

> Because data type mismatches trigger compiler errors, strict data typing helps you find bugs in your code at compile time and prevents you from assigning the wrong type of data to an existing variable. During authoring, strict data typing activates code hinting in the ActionScript editor (but you should still use instance name suffixes for visual elements).

Using strict data typing helps ensure that you don’t inadvertently assign an incorrect type of value to a variable. Flash checks for typing mismatch errors at compile time, and displays an error message if you use the wrong type of value. Therefore, using strict typing also helps to ensure that you do not attempt to access properties or methods that are not part of an object’s type. Strict data typing means the ActionScript editor automatically shows code hints for objects.
