Help with instances?

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?

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

Sorry I missed it but now I got it and it helped! Thank you! :slight_smile: :slight_smile:

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

Thanks Jubba. I think I found an easier way to do it thanks to electrongeek. :slight_smile:
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… :smirk:
(probably gonna fight with it the rest of the night :wink:

Ok, I’m just gonna try to make my life a little easier and my sleep a little longer and ask from you guys. :slight_smile:
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. :slight_smile:

Thanks for helping me out, I really appreciate it.

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

All at once:


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:


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:


gotoAndPlay(_currentframe - 1)

For Flash MX


_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…

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

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. :stuck_out_tongue:

One mistake in my code


_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.

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.

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

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


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

Then put this code in the frame:


_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.

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

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

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. :slight_smile: )

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.

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?

Maybe this would be better

_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 :slight_smile:

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. :slight_smile:

I think it should be this way.


_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.

*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 :slight_smile:

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

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.

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