PDA

View Full Version : duplicateMovieClip



Hartwig
May 10th, 2002, 08:15 AM
Hi there!

Following problem:

I want e.g. 25 cicles on the screen. Position and size shall come from a .txt-file. Does it work with duplicateMovieClip. I don't want to draw 25 circles by hand.
Ideas?

THX a lot

Hartwig

lobstars
May 10th, 2002, 11:11 AM
couldn't you just use the one circle and drag it on to the stage 25 times then manipulate each instance as you'd like?

hartwig
May 10th, 2002, 11:56 AM
yes of course i could, but in fact i used 25 as example and i have nearly 500 circles. so i would go crasy dragging and manipulating each circle.

Hartwig

lobstars
May 10th, 2002, 12:57 PM
so duplicate movie clip then :)

Hartwig
May 11th, 2002, 02:17 AM
OK. Let me re-ask my question:
How could the script look like, that duplicates my circle and reads its properties from a textfile. Duplicate does work but the the variables from the textfile are ignored. It only works when I use fix values.

Help me please!

THX Hartwig

upuaut8
May 11th, 2002, 02:57 AM
ok.. lobstar is being oblique.. :)

A) keep in mind that if you do this, you're going to have 500 movie clips on the stage. If all of these are moving, it's going to bog down the processor a lot. I have no clue what sort of frame rate you'll be able to achieve that way.. but this is the script that will do it.

lets say you place one circle on the stage and give it an instance name of "myCircle"

now you could place this in any frame to duplicate the movie clips.

for (i=0;i<500;i++){
myCircle.duplicateMovieClip("myCircle"+i,i);
}

this will place 500 duplicates of the circle on the stage, in the exact same location as the first circle. Each one will be named myCircle0, myCircle1, myCircle2, etc until you reached myCircle499.

if you were going to possition them as you duplicated them, you could add the coordinates into the for loop like so

for (i=0;i<500;i++){
myCircle.duplicateMovieClip("myCircle"+i,i);
["myCircle"+i]._x=myvariable;
["myCircle"+i]._y=myvariable;
}

pezdemon
May 11th, 2002, 03:11 AM
LoL, remind me never to go on that flash, my computer struggles with 25 randomly moving circles... 500 would destroy it!

Hartwig
May 11th, 2002, 03:47 AM
Hi!

THX for the code. I haven't tried it yet, but when reading it I can't get rid of the feeling that all the circles will be placed on the same position, because myVaraible doesn't change. I have more variables myVariable1, myVariable2,...,myVariable500 (just for the x-position).
Do I have to put them in [], too?

Hartwig

upuaut8
May 11th, 2002, 05:14 AM
as long as your variables are named that way it's easy

for (i=0;i<500;i++){
myCircle.duplicateMovieClip("myCircle"+i,i);
["myCircle"+i]._x="myVariable"+i;
["myCircle"+i]._y="myVariable"+i;
}

lobstars
May 11th, 2002, 12:01 PM
Sorry!!! I have just started doing landscape gardening for a living :) DARN IT I'M TIRED.....
Sorry for not being more helpful :)

upuaut8
May 11th, 2002, 02:39 PM
:) it's ok man.. we understand.

lobstars
May 11th, 2002, 03:35 PM
Cheers David :)

Hartwig
May 12th, 2002, 02:17 PM
Hi again!

Something doesn't work with your code. The instances of the circle are created, but the assignement of the new values of the coordinates from the variables from the .txt-file take no effect: all circles are at the same position. Any alternatives (e.g. setProperty,...)

THX for any help

Hartwig

I am not Jubba
May 12th, 2002, 02:28 PM
set property is evil, never use it. It is a depreciated command as far as I know. Make sure that you have the correct path to your TEXT file, and make sure you load the variables from the text file first. the problem is that it is not getting the variables...

suprabeener
May 12th, 2002, 02:32 PM
you'll have to evaluate those variables references:


for (i=0;i<500;i++){

myCircle.duplicateMovieClip("myCircle"+i,i);

_root["myCircle"+i]._x = _root["myVariable"+i];

_root["myCircle"+i]._y = _root["myVariable"+i];

}
that will set each circle's x and y to its corresponding myVariable.

ie. _root.myCircle4's x and y will be set to _root.myVariable4

Hartwig
May 12th, 2002, 02:46 PM
OK you're right, as usual. The variables are read and the properties are set correct. Now the next problem: Can I tween these duplicated circles? With start- and endsizes coming from a txt-file, too? Once I heard of this:

onClipEvent (load) {
&nbsp &nbsp &nbsp &nbsp finalsize = _root.variable2;
&nbsp &nbsp &nbsp &nbsp size = _width;
&nbsp &nbsp &nbsp &nbsp diff = finalsize-size;
&nbsp &nbsp &nbsp &nbsp width = _root.variable1;
&nbsp &nbsp &nbsp &nbsp height = _root.variable1;
&nbsp &nbsp &nbsp &nbsp _height = height;
&nbsp &nbsp &nbsp &nbsp _width = width;
&nbsp &nbsp &nbsp &nbsp _x = posx;
&nbsp &nbsp &nbsp &nbsp _y = posy;
}

onClipEvent (enterFrame) {
&nbsp &nbsp &nbsp &nbsp if (_width<finalsize) {
&nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp _xscale += diff/50;
&nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp _yscale += diff/50;
&nbsp &nbsp &nbsp &nbsp }
}

It's for one specific circle. But how can I use this here????

THX

Hartwig

upuaut8
May 12th, 2002, 03:33 PM
suprabeener is awesome.. listen and learn from everything he says. He's taught me more than almost any book I've read.

suprabeener
May 12th, 2002, 08:40 PM
upu - thanks for the kudos!

hartwig -

if you're going to keep this many variables for each movie, how about creating a more organised method of storing them? let's use an array of custom objects:


// create the array of objects

_root.settings = [

{_x:300,_y:300,_xscale:150,_yscale:150},

{_x:200,_y:200,_xscale:200,_yscale:200}

];
i used shorthand to create both the array and the objects in it, but it's pretty clear how it works, right? see how each element of settings is an object with various property / value pairs?

while poking around in the help files, i discovered that macromedia has built in a way to grab you variables from an object when using duplicateMovieClip! perfect!!

now we use that array to create our movies, good to encapsulate it in a function:


function makeDemMovies() {

// declare local variables

var j;

// for each object in settings...

for(j in _root.settings){

// the last argument is the object that you grab the values from

_root.myCircle.duplicateMovieClip("myCircle"+j,j,_root.settings[j]);

}

}
then call the function to put it into action:


makeDemMovies();
et viola! many movies!

this method involves making an object for each clip you want to duplicate ... lots of typing.

bon chance!

edited because i used i in the for loop (again) and it made everything italic, doh

Hartwig
May 13th, 2002, 02:18 AM
Hi suprabeener!

I understand your description to make many movies (I think), but I can't get how this should help me to tween each of these circles in a different way. duplicateMovieClip makes X circles from one and I don't know how to treat these X new circles sepperately. Please explain the next step after duplicateMovieClip.
It would be very appreciated,

Hartwig

suprabeener
May 13th, 2002, 01:00 PM
ah, i see what you mean.

the elements in the object don't have to be properties, they can be anything, and the duplicated clip will inherit them. for instance:


_root.settings = [

{_x:300,_y:300,_xscale:150,_yscale:150,myVariable:"someString"},

{_x:200,_y:200,_xscale:200,_yscale:200}

];

for(j in settings){

myCircle.duplicateMovieClip("myCircle"+j,j,settings[j]);

}

trace(myCircle0.myVariable); // outputs "someString"


then you want them to tween to some size right?

let's create a tweenSpd and a tweenMax variable to facilitate that, and write a method to make each movie tween.

first, the method:


MovieClip.prototype.tween = function(){

if(this._xscale < this.tweenMax){

this._xscale = this._yscale += this.tweenSpd;

}else{

this.onEnterFrame = null;

}

}
so this is a pretty simple method. the clip will check if it's too big, if not it grows uniformly in along both axis, if it is, it cancels the onEnterFrame event (itself).

now we need to add those variables to the objects, and have each clip tween when it loads. here's a version to include those things:


_root.settings = [

{_x:300,_y:300,tweenSpd:5,tweenMax:150},

{_x:200,_y:200,tweenSpd:10,tweenMax:200}

];

function makeDemMovies() {

var j,mc;

for(j in _root.settings){

mc = _root.sqr.duplicateMovieClip("myCircle"+j,j,_root.settings[j]);

mc.onEnterFrame = mc.tween;

}

}

ilyaslamasse
May 13th, 2002, 07:44 PM
Ou la la... I come back from vacation, and that's what I see... Bread on the board, du pain sur la planche as we say...

pom 0]

harwig
May 15th, 2002, 03:28 AM
Hi once again!

That's exactly what I want. But I don't know where to put the code. I guess on the object (the circle) I want to duplicate. But is it onClipEvent (load), onClipEvent (enterFrame)? And do I have to put the settings, the function and the method there?

THX

Hartwig

suprabeener
May 15th, 2002, 12:46 PM
assuming that myCircle is in _root, it can all go in frame 1. make sure to define the function before you call it, and don't run the code more than once.

Hartwig
May 16th, 2002, 02:05 AM
Hi!

Nothing tweens in my movie. Do you have a .fla which works. Could you send to hartwig@fuerst.cc.

THANKS THANKS THANKS THANKS THANKS

Hartwig

suprabeener
May 17th, 2002, 03:44 PM
here's some code you can drop into frame 1 of an empty fla.

there's a few modifications, a method for drawing circles has been added, and makeDemMovies() has been amended to remove the added movie after the duplication, and the duped layer is j+1 to avoid overwriting "myCircle".

the circles aren't really that great a shape, oh well ... ; )


_root.settings = [
{_x:300,_y:300,tweenSpd:5,tweenMax:150},
{_x:200,_y:200,tweenSpd:10,tweenMax:200}
];

MovieClip.prototype.makeCircle= function(r){
var c = this.createEmptyMovieClip("myCircle",0);
with (c){
lineStyle(0);
moveTo(0,-r);
curveTo(r,-r,r,0);
curveTo(r,r,0,r);
curveTo(-r,r,-r,0);
curveTo(-r,-r,0,-r);
}
}

function makeDemMovies() {
_root.makeCircle(50);
var j,mc;
for(j in _root.settings){
mc = _root.myCircle.duplicateMovieClip("dupedCircle"+j,j+1,_root.settings[j]);
mc.onEnterFrame = mc.tween;
}
_root.myCircle.removeMovieClip();
}

MovieClip.prototype.tween = function(){
if(this._xscale < this.tweenMax){
this._xscale = this._yscale += this.tweenSpd;
}else{
this.onEnterFrame = null;
}
}

makeDemMovies();

good luck!

Hartwig
May 20th, 2002, 07:58 AM
Hi!

Where comes r from, the variable the function uses? And can't I use an instance of a circle from my library called "myCircle" to be duplicated instead of using a method for drawing circles?

THX

Hartwig

suprabeener
May 20th, 2002, 04:24 PM
the r stands for radius (it could be named anything, r just makes sense), and it's passed in the brackets when you call the function. in the above code i passed it 50.

yes, you can use any old movie clip you choose, i just wanted to have a 100% code based solution so it was a pure cut and paste.

if the movie is on the stage to begin with, you can just get rid of the part that creates the movie, and the part that deletes it later on. just make sure it's named correctly.

if you want to attach the clip from your library, just change the part where the circle is created from the method i wrote, to a regular attachMovie call. you can delete it after or leave it on the stage, your call. same thing with the naming.

Hartwig
May 22nd, 2002, 06:02 AM
Hi!

It sounds so logical! But my circles aren't duplicated. The debugger window only shows on instance and not more. I drew this instance by hand because I got an error with the with-loop which should draw the prototype-circle. This instance is called myCircle but isn't duplicated. What am I making wrong. I dropped your cool code to frame one but it doesn't work as we want.

????????????????????????

Hartwig