Loading
  Password:
  Lost password?  
LogoutLOGOUT
Join as a MemberJoin as a Member
 
FlashMove Live Support



Go Back   FlashMove Forums > Flash Coding > Actionscript 2.0

ReplyREPLY THREAD
 
Thread Tools Search this Thread Display Modes
  #1  
Old 12-11-2004, 07:04 PM
gvDan's Avatar
gvDan gvDan is offline
Registered User
 
Join Date: Dec 2004
Location: US
Posts: 367
gvDan is on a distinguished road
infinite Duplicate MovieClips

I would like to create many duplicates of a movieclip. Also, upon creation of each movieclip, i would like to set a few properties.

For the new MC name, i would use "MC" + i, and update the value of i each time. but, i dont know how to call the unique MC's. _root."MC"+i._x obviously doesn't work, so...

...I tried creating an array, but i think my code was a little screwed up.


Can someone explain how I might be able to modify many duplicates with and without an array?
  #2  
Old 12-11-2004, 07:16 PM
Scottae's Avatar
Scottae Scottae is offline
God Moderator
 
Join Date: Oct 2003
Location: Atlanta, Ga. USA
Posts: 6,531
Scottae is on a distinguished road
Source Example

Actionscript:
var gap:Number = 10;

for (var i = 0; i < 10; i++)
{
	// you can use array access notation like this
	_root["MC" + i]._x = i * gap;
	
	// or you can use the eval() function like this
	eval("MC" + i)._x = i * gap;
}
__________________
Our greatest glory is not in never falling but in rising everytime we fall. - Confucius

Blog | Shared Items
  #3  
Old 12-12-2004, 02:57 AM
pigpen pigpen is offline
Registered User
 
Join Date: Sep 2004
Posts: 319
pigpen is on a distinguished road
a shorter method

Here's another approach. A shortcut method without the need of using arrays. You can set movieclip properties while creating duplicate movie by setting parameters in the initObject.

This'll both duplicate a movieclip named "originalMC" AND place it at _x 200 and _y 100.

Actionscript:
var myDupe:MovieClip = originalMC.duplicateMovieClip("myDupe", 0, {_x:200, _y:100});

Additionally, you can also set properties now by using the myDupe variable because it contains a reference to the duplicate instance.


Actionscript:
var myDupe:MovieClip = originalMC.duplicateMovieClip("myDupe", 0, {_x:200, _y:100});
myDupe._width = 350; //set the width of duplicate instance

To use it in a loop using the loop from Scottae's array example:

Actionscript:
var gap:Number = 10;

for (var i = 0; i < 10; i++) {
	var myDupe:MovieClip = originalMC.duplicateMovieClip("MC"+i, i, {_x: i*gap});
	// 
	// You can also set properties through myDupe 
	// because myDupe is referencing "MC"+i
	myDupe._alpha = 50; //set every duplicate instance's alpha to 50%
}

This will now move every duplicate instance by _x 10 and set the alpha at 50%.

So there you go. You set the properties of the movieclip within the same line when you duplicate the movie, OR by manipulating the properties of the duplicate movieclip variable that holds the duplicate instance, "myDupe" in this case. This often makes it easier for me, rather than using the explicit references to arrays, like _root["MC" + i], as it is easy to get errors or forget the proper syntax.
  #4  
Old 12-12-2004, 06:15 AM
gvDan's Avatar
gvDan gvDan is offline
Registered User
 
Join Date: Dec 2004
Location: US
Posts: 367
gvDan is on a distinguished road
would this work?

i want to randomly display a new instance of a movieclip over time. each second or few seconds, a new clip will be created.

...that and set a random x position and alpha value for each. how would i get movieclips to appear over time??


right now, i have code on a single movieclip called "tree" on the stage. the code says:
Actionscript:
onClipEvent (enterFrame) {
	a = Math.random()*150;
	if (a<148) {
		newXPosition = Math.random()*250;
		this.duplicateMovieClip("tree"+i, 0);
		_root.treesArray["trees"+i]._x = newXPosition;
	}
}
....and it doesn't work.... what do i do?

Last edited by Scottae : 01-21-2005 at 08:32 PM. Reason: Format with AS tags
  #5  
Old 12-12-2004, 06:17 AM
gvDan's Avatar
gvDan gvDan is offline
Registered User
 
Join Date: Dec 2004
Location: US
Posts: 367
gvDan is on a distinguished road
correction:

if(a>148)

so that each frame, the random number is evaluated and then the code is supposed to execute
  #6  
Old 12-12-2004, 03:13 PM
Scottae's Avatar
Scottae Scottae is offline
God Moderator
 
Join Date: Oct 2003
Location: Atlanta, Ga. USA
Posts: 6,531
Scottae is on a distinguished road
Source

Well, you should have said you wanted that in the first place so we didn't have to do all this beating around the bush. Here's an example of how you can do it.
Attached Files
File Type: zip test.zip (5.1 KB, 26 views)
__________________
Our greatest glory is not in never falling but in rising everytime we fall. - Confucius

Blog | Shared Items
  #7  
Old 12-14-2004, 06:26 PM
gvDan's Avatar
gvDan gvDan is offline
Registered User
 
Join Date: Dec 2004
Location: US
Posts: 367
gvDan is on a distinguished road
Thank You! that worked just fine!


another question: how would i change the interval between each clip's duplication?
  #8  
Old 12-14-2004, 06:36 PM
Scottae's Avatar
Scottae Scottae is offline
God Moderator
 
Join Date: Oct 2003
Location: Atlanta, Ga. USA
Posts: 6,531
Scottae is on a distinguished road
Change the interval value.
__________________
Our greatest glory is not in never falling but in rising everytime we fall. - Confucius

Blog | Shared Items
  #9  
Old 12-15-2004, 01:03 AM
gvDan's Avatar
gvDan gvDan is offline
Registered User
 
Join Date: Dec 2004
Location: US
Posts: 367
gvDan is on a distinguished road
sorry, i need to be more complete when i post

is it possible to have varied intervals between each movie clip's creation?

the time in between each duplication would be different..?



sorry about all the confusion!
  #10  
Old 12-15-2004, 02:11 AM
Scottae's Avatar
Scottae Scottae is offline
God Moderator
 
Join Date: Oct 2003
Location: Atlanta, Ga. USA
Posts: 6,531
Scottae is on a distinguished road
I guess you could use Math.random(), or if you want specific values, maybe set them to an array and each time you set an interval, use the next value from the specified index of the array. Of course, each time you do this you need to clear the previous interval and then set another interval.
__________________
Our greatest glory is not in never falling but in rising everytime we fall. - Confucius

Blog | Shared Items


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

Smilies are On
[IMG] code is Off
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
duplicate movieclip holder bugjuice Flash MX ActionScript 1 02-19-2004 11:48 AM
can't remove duplicated movieclips gaz_the_mod Flash MX ActionScript 1 02-17-2004 01:00 PM
sortOn() on an array of MovieClips: problem aLearnerRather Flash MX ActionScript 3 01-06-2004 09:28 PM
duplicate _level problem zorro_m2h Newbies 0 09-15-2003 05:52 AM
Duplicate movieclips BUT... jeff2000 Flash 5 Actionscript 4 11-10-2001 06:50 AM



All times are GMT. The time now is 01:43 PM.




Powered by vBulletin version 3.5