Flashadvisor logo
:: Desktop Shortcut
:: Flash Help
Advices from Experts

 Submit Here! · Link to Us · Newsletter · Tell a Friend ·

      Add Tutorial |

 

Advertise here










 

Google

Search WWW   
FlashAdvisor.com

 Home > Tutorials  > ActionScript

"Starfield Simulation" effect using "duplicateMovieClip"

Author: Saumitra Karandikar | Email


Advertisement


The new Movie Clips created using the duplicate Movie Clip are created at the same (x,y) location as the original movie clip on the stage. But we want our Movie Clips to appear at random locations. So, we use the function from the Math class 'random". The "random" function creates a pseudo random number between 0 and 1.
We will use this to generate a random number and further multiply it with 200 so that we get larger values. We may also choose to be more imaginative with the random function and generate smarter values so that our flying Movie Clips spread across the stage. However to keep things simple, lets first use the random function multiply the generated value with 200 to get values between 0 and 200.

The locations for new random movies can be represented by:


randomX = 200*Math.random();
randomY = 200*Math.random();


We will now assign x and y positions to the new Movie Clips. The overall function and all the actions combined in the "actions" layer now appear as below:


//initialize counter
mycount = 0;
//function to create multiple movieclips from single Movie Clip
function createFlyingMC() {
if (mycount<50) {
_root.flying_mc.duplicateMovieClip("flying_mc"+mycount,_root.getNextHighestDepth());
randomX = 200*Math.random();
randomY = 200*Math.random();
eval("flying_mc"+mycount)._x = randomX;
eval("flying_mc"+mycount)._y = randomY;
mycount++;
} else {
clearInterval(ID);
for(var i = 0;i<50;i++){
removeMovieClip(eval("flying_mc"+i));
}
}
}
//function to periodically call another function
ID = setInterval(createFlyingMC, 500);


Notice the use of if - else loop to detect Movie Clip count. Also notice the use of clearInterval to clear the created timer and consequently saving on system memory.

In the else loop we have used removeMovieClip method. This method is used to remove any Movie Clip created using duplicate Movie Clip. Thus after a period of 50 * 0.5 seconds i.e., 25 seconds all the Movie Clips created get removed.
See the snapshot of the actions box in the fig 1.3 below:


Fig 1.3

See the result of the "test movie" in fig 1.4 below:


Fig 1.4

Also notice that the original Movie Clip still exists on the screen. This is because we haven't done anything to it. Usually, the original Movie Clip is not brought to stage and is used from the library using the "attachMovieclip" method and removed when its use is complete OR the original movie clip is set to invisible after the duplication job is complete OR is even placed off the stage when initialized so that it is anyway not in view.
To extend this, you can change the color or text inside the flying Movie Clips; change their orientation or alpha or the x and y scale randomly.

I am sure "you love Flash" too but you can be further more imaginative than I have been here by using the "duplicateMovieClip" action in Flash actionscript. For example, creating ripples in water, creating fireworks, creating firing bullet games etc.
Download Source File

| 1 | 2 | 3 |

Rate This Tutorial :
We hope the information helped you. If you have any questions or comments, please don't hesitate to post them on the Forums section
Submit your Tutorial at Click Here
  

Home | About Us | Contact Us | Feedback | Advertise with us

Best viewed in 800x600 resolution with Internet Explorer.

Site Developed and Hosted by  ethicsolutions.com
All rights reserved with FlashAdvisor

Flash is a registered trademark of Adobe Systems, Inc.

28 users online