# Random placement of MC inside another MC, rather than on main timeline

**URL:** https://forum.kirupa.com/t/random-placement-of-mc-inside-another-mc-rather-than-on-main-timeline/294
**Category:** flash
**Created:** [July 12, 2002, 3:28pm UTC](https://forum.kirupa.com/t/random-placement-of-mc-inside-another-mc-rather-than-on-main-timeline/294 "2002-07-12T15:28:39Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![Denvish](https://avatars.discourse-cdn.com/v4/letter/d/ce7236/32.png) [@Denvish](https://forum.kirupa.com/u/Denvish)
#### Post date: [July 12, 2002, 3:28pm UTC](https://forum.kirupa.com/t/random-placement-of-mc-inside-another-mc-rather-than-on-main-timeline/294/1 "2002-07-12T15:28:39Z")

</div>

Posted this in Flash 5 section, think it might be better off in here

good day flashers

I have a slightly tricky problem which I’m hoping you can help me with. I need to randomly position identical symbols in a movie - I have found a way I can do it in the main timeline using this code:  
[COLOR=red]  
n = 10;  
while (n\>0) {  
duplicateMovieClip ("/r1", n, n);  
n = n-1;  
setProperty ("/symbol", \_x, random(130));  
setProperty ("/symbol", \_y, random(148));  
}  
[/COLOR]  
[[U][COLOR=darkblue]RESULT[/COLOR][/U]](http://denvish.com/random.htm)  
The problem is that the \_x and \_y parameters only seem to work in the main timeline, and I need the whole thing to be self-contained inside a movie clip, so that I can easily paste it into other Flash movies (about 75 of them). Is it possible to specify a random location inside a MC? I’d really appreciate any help or tips you can offer on this.  
 ![](http://denvish.com/gifs/dd8.gif)  
Thank you

---

<div class="post-metadata">

### Author: ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)
#### Post date: [July 12, 2002, 4:49pm UTC](https://forum.kirupa.com/t/random-placement-of-mc-inside-another-mc-rather-than-on-main-timeline/294/2 "2002-07-12T16:49:46Z")

</div>

Hello Denvish! Yes, this thread might be best placed here, so I deleted your post in the Flash 5 forum to reduce cluttering and duplicate threads. You are randomly placing the duplicated movie, correct? According to the code you have provided, you are naming the duplicated movies “n.” I think you may be targetting the wrong movie. Give this code a whirl:

n = 10;  
while (n\>0) {  
duplicateMovieClip ("/r1", n, n);  
setProperty ([COLOR=red] **n** [/COLOR] , \_x, random(130));  
setProperty ([COLOR=red] **n** [/COLOR] , \_y, random(148));  
n–;  
}

I’m a bit fuzzy on what you are trying to accomplish, but I thought I’d take a stab at it. Also note I decremented the value “n” _after_ the properties were set.

---

<div class="post-metadata">

### Author: ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)
#### Post date: [July 12, 2002, 6:20pm UTC](https://forum.kirupa.com/t/random-placement-of-mc-inside-another-mc-rather-than-on-main-timeline/294/3 "2002-07-12T18:20:42Z")

</div>

ah… oops…no, that was my mistake when I copied the code - it should have been:  
[COLOR=red]  
n = 10;  
while (n\>0) {  
duplicateMovieClip ("/symbol", n, n);  
n = n-1;  
setProperty ("/symbol", \_x, random(130));  
setProperty ("/symbol", \_y, random(148));  
}[/COLOR]  
where ‘symbol’ is the name of the MC I’m randomly placing

as you can see from [[COLOR=darkblue]this .swf,[/COLOR] it does work - but what I want to be able to do is take the two levels from the main timeline, and incorporate them into a new movie clip symbol, so that I can easily paste them into other movies (I have built a small streaming music player - see music page [URL=http://denvish.com/pb/planetbob.htm][COLOR=blue]here](http://denvish.com/random.htm)[/COLOR] , and want to be able to add the effect (to the background) in each .fla file, in the easiest manner possible).

The other disadvantages of the duplicateMovie action are:

1. All the duplicate movies seem to appear at the FRONT (ie top level) of the .swf, no matter which level they are placed on - I would like to be able to find a way round that. If the actions could be placed inside an MC rather than on the main timeline I think that would solve the problem.
2. Each of the randomly-placed duplicated movies (10 of them, in the above code) appear in the same location each time they appear - so once each one has appeared once, it will always appear again in the same place (unless the page is refreshed). If possible, I would like each example to appear in a completely new position.

I realise I am asking a lot of you here, and probably confusing you with too much detail =) but I am keen to learn as much as I can about Flash, and in particular, Actionscripting; I’ve failed miserably in finding information about this problem anywhere else, so I really hope you can provide some pointers.

Have attached the .fla file - basically want to get the 2 layers from main timeline into a new MC, so I can bung it in any movie I wish

**whew** need a cuppa tea now 🙂

thanks for the reply & code, RenaissanceGirl, and well spotted on the mistake :o but it doesn’t really solve my problem…

---

<div class="post-metadata">

### Author: ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)
#### Post date: [July 12, 2002, 7:25pm UTC](https://forum.kirupa.com/t/random-placement-of-mc-inside-another-mc-rather-than-on-main-timeline/294/4 "2002-07-12T19:25:06Z")

</div>

Or  
to simplify the whole matter  
how the hell do I specify a random location for a movie clip/graphic symbol inside another movie clip?  
Having looked around, if I can do that, I can use attachMovie to achieve the rest

---

<div class="post-metadata">

### Author: ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)
#### Post date: [July 12, 2002, 8:26pm UTC](https://forum.kirupa.com/t/random-placement-of-mc-inside-another-mc-rather-than-on-main-timeline/294/5 "2002-07-12T20:26:11Z")

</div>

i don’t quite see how “/symbol” would refer to the duplicated clip…

but with attachMovie you needn’t worry about that since a happy thing about attachMovie is that the method returns a reference to the newly attached Movie. therefore:

```auto

function makeMoviesIn(target,movieToAttach){
   var mc,n = 10;
   while(n--){
      mc = target.attachMovie(movieToAttach,movieToAttach+n,n);
      mc._x = Math.random() * 130;
      mc._y = Math.random() * 148;
   }
}
// usage:
// makeMoviesIn(_root.holderClip,"attachMe");

```

---

<div class="post-metadata">

### Author: ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)
#### Post date: [July 12, 2002, 10:21pm UTC](https://forum.kirupa.com/t/random-placement-of-mc-inside-another-mc-rather-than-on-main-timeline/294/6 "2002-07-12T22:21:21Z")

</div>

tnx 4 the response sbeener will try that once I’ve had some sleep. Looks like it should do the job.
