PDA

View Full Version : DuplicateMovieClip Problem


friz2002
12-11-2002, 10:41 PM
Hi,

I have a mc on the mzin timeline that is moving to the right, when it reaches a certain point, it goes back to it's original x position, with a random y position and duplicates 10 times.
No problem so far.
What I would like is that the "new" mc's do the same as the first. So, move to the right, reposition, etc

this is what I have so far, but only the original mc moves and repositions...


naarRechts = function(){
this._x += this.speed;
this.teVer();
}
teVer = function(){
if(this._x>=500){
this._x = -33;
this.dup();
}
}
dup = function(){
i=0
while(i<10){
duplicateMovieClip(_root.raket_mc,"raket"+i+"_mc",i);
setProperty ("raket"+i+"_mc", _y, random(275));
setProperty ("raket"+i+"_mc", _x, random(275));
setProperty ("raket"+i+"_mc",_xscale, random(200)+50);
i++;
}
}
sneller = function(){
this.speed +=10;
}
_root["raket"+i+"_mc"].speed = 5;
_root["raket"+i+"_mc"].onEnterFrame = naarRechts;
_root["raket"+i+"_mc"].teVer = teVer;
_root["raket"+i+"_mc"].dup = dup;
_root["raket"+i+"_mc"].onKeyDown = sneller;
Key.addListener(_root["raket"+i+"_mc"]);


Thx

jimburton
12-12-2002, 12:26 PM
hi there,

the code that assigns the handlers etc at the bottom of your code needs to be within the while loop:


naarRechts = function(){
this._x += this.speed;
this.teVer();
}
teVer = function(){
if(this._x>=500){
this._x = -33;
this.dup();
}
}
dup = function(){
i=0
while(i<10){
duplicateMovieClip(_root.raket_mc,"raket"+i+"_mc",i);
var temp = _root["raket"+i+"_mc"];
temp._y = random(275);
temp_x = random(275);
temp._xscale = random(200)+50;
temp.speed = 5;
temp.onEnterFrame = naarRechts;
temp.teVer = teVer;
temp.dup = dup;
temp.onKeyDown = sneller;
Key.addListener(temp);
i++;
}
}
sneller = function(){
this.speed +=10;
}

friz2002
12-12-2002, 01:02 PM
Thx again Jim :)
It only worked if I put the original functions at the bottom (see bold code).
Is this the corect way or is there a shorter way of doing this?

naarRechts = function(){
this._x += this.speed;
this.teVer();
}
teVer = function(){
if(this._x>=500){
this._x = -33;
this.dup();
}
}
dup = function(){
i=0
while(i<(random(10)){
duplicateMovieClip(_root.raket_mc,"raket"+i+"_mc",i);
var temp = _root["raket"+i+"_mc"];
temp._y = random(400);
temp._x = random(275);
temp._xscale = random(200)+50;
temp.speed = 5;
temp.onEnterFrame = naarRechts;
temp.teVer = teVer;
temp.dup = dup;
temp.onKeyDown = sneller;
Key.addListener(temp);
i++;
}
}
sneller = function(){
this.speed +=10;
}
raket_mc.speed = 5;
raket_mc.onEnterFrame = naarRechts;
raket_mc.teVer = teVer;
raket_mc.dup = dup;
raket_mc.onKeyDown = sneller;
Key.addListener(raket_mc);

jimburton
12-12-2002, 01:41 PM
It only worked if I put the original functions at the bottom (see bold code).

whoops, yeah the code in the loop only adds the eventhandler etc to the duplicated mcs....

Is this the corect way or is there a shorter way of doing this?

I'd say it was a reasonable way except that you don't need every mc to be a listener to the key object...you could make a seperate object to listen to the key and increment a variable on the root called speed and then change your movement function:


speed = 5;
//seperate listener
var foo = new Object();
foo.onKeyPress = function(){
_root.speed +=10;
}
Key.addListener(foo);
naarRechts = function(){
this._x += _root.speed;
this.teVer();
}
// no need to refer to this.speed anywhere now...


this is presuming that all raket_mcs should accelerate at same rate...