# Help with instances?

**URL:** <https://forum.kirupa.com/t/help-with-instances/8764>\
**Category:** Uncategorized\
**Created:** [November 27, 2002, 11:31pm UTC](https://forum.kirupa.com/t/help-with-instances/8764 "2002-11-27T23:31:26Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![RealTK](https://avatars.discourse-cdn.com/v4/letter/r/9dc877/32.png) [@RealTK](https://forum.kirupa.com/u/RealTK)\
**Post date:** [November 27, 2002, 11:31pm UTC](https://forum.kirupa.com/t/help-with-instances/8764/1 "2002-11-27T23:31:26Z")

</div>

I’m trying to make duplicated movie clips scroll down the screen (asteroids) for an experimental game but I just can’t get it working. :-\  
I’m using this to duplicate the movieclip:

i=i+1;  
duplicateMovieClip (“m0”, “m”, i);

and trying to get the movieclips to scroll down with this:

p=eval(“m” add i);  
p.\_y+=2;

Can anyone tell me somekind of an example to make renamed duplications scroll down the screen?

---

<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:** [November 28, 2002, 12:08am UTC](https://forum.kirupa.com/t/help-with-instances/8764/2 "2002-11-28T00:08:00Z")

</div>

Here’s a simple fla that might come in handy. Hope it helps. =)

---

<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:** [November 28, 2002, 12:22am UTC](https://forum.kirupa.com/t/help-with-instances/8764/3 "2002-11-28T00:22:19Z")

</div>

Sorry I missed it but now I got it and it helped! 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:** [November 28, 2002, 12:28am UTC](https://forum.kirupa.com/t/help-with-instances/8764/4 "2002-11-28T00:28:48Z")

</div>

Real: Welcome to the boards. You have the right idea. You want to add a value to the y value try this

ii++  
m0.duplicateMovieClip(‘copy’+ii,ii)  
ext = \_root[‘copy’+ii]  
ext.\_y += 2

---

<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:** [November 28, 2002, 12:41am UTC](https://forum.kirupa.com/t/help-with-instances/8764/5 "2002-11-28T00:41:12Z")

</div>

Thanks Jubba. I think I found an easier way to do it thanks to electrongeek. 🙂  
I just made the code inside the movieclip in its own frames.  
Now I just have to duplicate it… Let’s see how easy that’ll be… 😏  
(probably gonna fight with it the rest of the night 😉

---

<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:** [November 28, 2002, 12:51am UTC](https://forum.kirupa.com/t/help-with-instances/8764/6 "2002-11-28T00:51:58Z")

</div>

Ok, I’m just gonna try to make my life a little easier and my sleep a little longer and ask from you guys. 🙂  
How should I duplicate the movieclip? I need it to be duplicated in certain periods of time with a timer like:  
time++;  
if ( time\>100 ) {  
duplicateMovieClip (\_root.m0, “m”, 0 ); // possible?  
}

and I need them to duplicate in a random fashion along the x axel which is 640 pixels long…  
Maybe you get the idea even with the lack of my explanation skills. 🙂

Thanks for helping me out, I really appreciate it.

---

<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:** [November 28, 2002, 1:04am UTC](https://forum.kirupa.com/t/help-with-instances/8764/7 "2002-11-28T01:04:34Z")

</div>

ok, one question: Do you want then to appear at different random times? or all at once?

All at once:

```php

for(ii=1;ii<=100;ii++){
      duplicateMovieClip('m0', 'm'+ii, ii)
      ext = _root['m'+ii]
      ext._x = Math.random()*640
      ext._y += 6
}

```

At random times:

For Flash 5

Frame 1:

```php

nFrequency = Math.round(Math.random()*5)
if(nFrequency == 5){
      ii++
      duplicateMovieClip('m0', 'm'+ii, ii)
      ext = _root['m'+ii]
      ext._x = Math.random()*640
      ext._y += 6
}

```

Frame 2:

```php

gotoAndPlay(_currentframe - 1)

```

For Flash MX

```php

_root.onEnterFrame = function(){
      nFrequency = Math.round(Math.random()*5)
      if(nFrequency == 5){
            ii++
            duplicateMovieClip('m0', 'm'+ii, ii)
            ext = _root['m'+ii]
            ext._x = Math.random()*640
            ext._y += 6
      }
}

```

I don’t have Flash with me, so I can’t test these codes to make sure they are debugged correctly. But they look right to me…

---

<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:** [November 28, 2002, 1:17am UTC](https://forum.kirupa.com/t/help-with-instances/8764/8 "2002-11-28T01:17:47Z")

</div>

THANKS!! You got the duplication working! You’re a genius! 🙂 🙂  
I wanted them at random times for Flash MX.  
Still, they won’t move down… Working on it… Tip me if you want. 🙂

---

<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:** [November 28, 2002, 1:32am UTC](https://forum.kirupa.com/t/help-with-instances/8764/9 "2002-11-28T01:32:35Z")

</div>

Oh, I got another thing going on here…  
So there are still 2 things I really need help with… Sorry for being such a bother… :-\  
The duplications wouldn’t move down. Don’t know why but they seemed to twitch at the top when I tried debugging but they only moved when they were duplicated?? Anyways another thing came up… The collision detection. I need to get a ship to collide with the asteroids. So I need to know how to assign many targets for the collision detection like this:  
if (\_root.ship, hitTest (\_root[“m”+ii])) // that didn’t work though

I really have to go sleep so I’ll be checking the thread first thing in the morning. Thank you all for your help. 😛

---

<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:** [November 28, 2002, 1:34am UTC](https://forum.kirupa.com/t/help-with-instances/8764/10 "2002-11-28T01:34:14Z")

</div>

One mistake in my code

```php

_root.onEnterFrame = function(){
      nFrequency = Math.round(Math.random()*5)
      if(nFrequency == 5){
            ii++
            duplicateMovieClip('m0', 'm'+ii, ii)
            ext = _root['m'+ii]
            ext._x = Math.random()*640
            }
      ext._y += 6 //changed this
}

```

I just moved the ext.\_y += 6 out of the if statement. It should work now.

---

<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:** [November 28, 2002, 10:36am UTC](https://forum.kirupa.com/t/help-with-instances/8764/11 "2002-11-28T10:36:21Z")

</div>

Thanks, it works better now but they still stop when the next one duplicates. :-\  
Even though I think the bigger problem here is going to be the collision detection. _working on it_:beam:

I don’t get it. :-\ I think it’s because it only moves the one that the value ii currently is.

---

<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:** [November 28, 2002, 12:56pm UTC](https://forum.kirupa.com/t/help-with-instances/8764/12 "2002-11-28T12:56:53Z")

</div>

Yeah sorry. I see whats happening. This would be much easier if I had Flash on my computer. ☹

Try this. Put the following code on the Movieclip you are going to be duplicating.

```php

onClipEvent(load){
     _y = 0
}
onClipEvent(enterFrame){
     _y += Math.random()*10
}

```

Then put this code in the frame:

```php

_root.onEnterFrame = function(){
      nFrequency = Math.round(Math.random()*5)
      if(nFrequency == 5){
            ii++
            duplicateMovieClip('m0', 'm'+ii, ii)
            ext = _root['m'+ii]
            ext._x = Math.random()*640
      }
}

```

that **should** work.

---

<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:** [November 29, 2002, 7:15am UTC](https://forum.kirupa.com/t/help-with-instances/8764/13 "2002-11-29T07:15:21Z")

</div>

Thanks!! You totally solved the duplication problem. 🙂  
Now I’m trying to get the hitTest to work with all of them. 😏

Maybe I could use hitTest with multiple objects as the target?

---

<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:** [November 29, 2002, 8:19am UTC](https://forum.kirupa.com/t/help-with-instances/8764/14 "2002-11-29T08:19:53Z")

</div>

hit test is easy. You can use the highly underused “for…in” loop . The for in loop will detect everything within a target, and perform the same action on each, even if you don’t know what their names are.

for (mc in \_root) {  
\_root.ship.hitTest(mc);  
}

or something like that. (I don’t have Flash on my machine here either. 🙂 )

Mind you, I’m not 100% on this either… but the best way of implimenting this might be like so

\_root.ship.onEnterFrame = function(){  
for (mc in \_root) {  
this.hitTest(mc);  
}  
}

maybe one of my more experienced MX brothers can help out fixing that code up, but the ‘for…in’ loop is definitely the way you want to go for hit detection.

---

<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:** [November 29, 2002, 8:46am UTC](https://forum.kirupa.com/t/help-with-instances/8764/15 "2002-11-29T08:46:51Z")

</div>

I tried to implement the code and make the ship disappear using this:

\_root.ship.onEnterFrame = function(){  
for (mc in \_root) {  
if (this, hitTest(mc)) {  
this.\_alpha-=20;  
}  
}  
}

But the ship disappeared instantly. :-\  
Can you help me out?

---

<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:** [November 29, 2002, 9:01am UTC](https://forum.kirupa.com/t/help-with-instances/8764/16 "2002-11-29T09:01:08Z")

</div>

Maybe this would be better

```auto
_root.ship.onEnterFrame = function(){
  for (mc in _root) {
    if (this, hitTest(_root[mc])) {
      this._alpha-=20;
    }
  }
}

```

And the reason why it disappears is probably because ship itself is in \_root (as your path seems to show). And ship is definitely colliding with ship 🙂

---

<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:** [November 29, 2002, 9:09am UTC](https://forum.kirupa.com/t/help-with-instances/8764/17 "2002-11-29T09:09:35Z")

</div>

well keep in mind that I’m still guessing here so the further we explore this without help from Jubba or one of the others, the more we may have to backtrack, and or the more you may be needlessly slamming your head against a wall. 🙂

I think it should be this way.

```php

_root.ship.onEnterFrame = function(){
  for (mc in _root) {
    if (this.hitTest(mc)) {
      this._alpha-=10;
    }
  }
}

```

but this (if it works correctly) will only fade the ship to \_alpha 90. If you want it to completely disapear we’ll have to work that out differently.

Keep in mind also that this assumes your asteroids are located in the \_root timeline.

---

<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:** [November 29, 2002, 9:53am UTC](https://forum.kirupa.com/t/help-with-instances/8764/18 "2002-11-29T09:53:20Z")

</div>

> \*Originally posted by ilyaslamasse \*  
> And the reason why it disappears is probably because ship itself is in \_root (as your path seems to show). And ship is definitely colliding with ship 🙂

Yes but how will I be able to fix that? :-\

---

<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:** [November 29, 2002, 10:18am UTC](https://forum.kirupa.com/t/help-with-instances/8764/19 "2002-11-29T10:18:35Z")

</div>

if it were me, I’d consider throwing all of the asteroids into a single movie clip. Separation from the main timeline would help in a number of areas.

I should have a working example of what I’m thinking in a couple of hours.

---

<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:** [November 29, 2002, 10:28am UTC](https://forum.kirupa.com/t/help-with-instances/8764/20 "2002-11-29T10:28:38Z")

</div>

Ok, I’ll try to code it like you said while I wait for your example. 🙂  
Thanks. =)

[Next page](https://forum.kirupa.com/t/help-with-instances/8764.md?page=2)
