-1

Kindly help me update this code to AS3, it's in AS2. I am learning ActionScript, so it is still a monster to me... Please be easy on me.

stop();
particles_mc.duplicateMovieClip.visible = false;
trace ("undefined term")
var i:Number = 0; var
total:Number = 100;
for ( ; i < total; i++)

{

particles_mc.duplicateMovieClip.   ("particles_
mc"+i,i);
this["particles_mc"+i]._rotation = Math.floor.
(Math.random() * 360);
this["particles_mc"+i].gotoAndPlay ( Math.random
() *   particles_mc.MovieClip.totalFrames) );

}

Help Stack!

1 Answer 1

1

This looks pretty good. There are a couple methods that are no longer usable (mostly for the better) in AS3. I'll explain those below.


duplicateMovieClip is deprecated in AS3. There is no simple "duplicate" method in AS3, but there are several methods to make several copies of a movieclip (mc).

4as, a blog dedicated to flash programming, gives really good examples/tutorials for implementing a couple of those methods, but my favorite (and I believe the simplest), is to just create a custom class out of the mc you are going to be using more than once ("duplicating").

Begin by going to the mc in your library and right clicking to bring up the properties option. Once you have the Symbol properties window open, check the "Export for actionscript" box and remember the text that is in the "Class" textbox. *If you don't see these options in your Symbol properties window, make sure that your Advanced options are visible.

Now that you have created a class for your mc, in your code you can easily create a duplicate of the mc whenever you want by writing:

//this creates a new movieclip called mc that is a 
//duplicate of the movieclip class you created.
  //replace className with the text from the "Class" 
  //textbox in the symbol properties window
var mc:MovieClip = new className();

In order to put this duplicated mc on the stage, call the addChild() or addChildAt() method:

//adds a movieclip to the stage
addChild(mc);
//adds a movieclip to the stage at a certain depth
addChildAt(mc,0);

_rotation is no longer used in AS3. Instead use rotation without the underscore.

//rotate movieclip by a certain amount
mc.rotation=180;

totalFrames is actually technically AS3 (_totalFrames in AS2). However, the way you called it was incorrect. Instead of saying mc.MovieClip.totalFrames which is asking for the total number of frames from the movieclip called MovieClip inside of the movieclip called mc, you just have to say mc.totalFrames. That asks for the total number of frames inside the movieclip mc which, in this case, is what we want.


Here is your code re-written in as3 taking these changes into consideration (and with a bit of corrections for forgotten parenthesis and strange spacing):

//create the invisible starting mc **Not actually necessary**
/*
var start_mc:MovieClip = new part();
addChild(start_mc).visible = false;
*/
//initialize i
var i:Number = 0; 
//initialize total
var total:Number = 100;
//loop i to total
for(;i<total;i++){
    //Create a new mc
    var mc:MovieClip = new part();
    //Add that mc to the stage and name it "mc" followed by the number "i"
    addChildAt(mc,i).name="mc"+i;
    //rotate mc random number of degrees 0-359
    mc.rotation=(Math.floor(Math.random() * 360));
    //gotoAndPlay (do you really mean play?) random frame
    mc.gotoAndPlay(Math.floor(Math.random()*mc.totalFrames)+1);
}

You may notice that I am not using this["mc"+i] to change the movieclip after creating it. In as3 you can refer to a newly created movieclip by the variable you used to create it. In this case mc. However, if you don't trust this method, or if you need to change each mc individually later on, you can use the getChildByName() method.

this.getChildByName("mc"+i).rotation=180;

I hope this helps!

edit:

Working example

I added random scaling to make it prettier.

Try downloading the .fla to see how the code and movieclips are structured.

12
  • I appreciate this, thank you very much. I will be here again, infact, this is my home now! Don't get tired of me
    – Guyz
    May 18, 2013 at 12:56
  • If this helped you out, please be sure to accept the answer by clicking the check mark to the left of it.
    – Enigmadan
    May 18, 2013 at 18:58
  • Enigmadan, it did help a lot, thank you. My little problem though is the particles/stars I am going for are no longer born,emitting and flying from mc source symbol but instead, the mc which I tweened inside the mc, move to the right as a single object. How do I rectify that?
    – Guyz
    May 19, 2013 at 1:34
  • If I understood that correctly, this movieclip that you are duplicating is a tween of a star moving across the stage. When you give them random rotation, they are supposed to appear to have a burst effect like a firework?
    – Enigmadan
    May 19, 2013 at 1:54
  • 1
    just a note on this answer: you can actually reference to a newly created movieclip by the variable you used to create it in actionscript 2 as well
    – user151496
    May 23, 2013 at 16:39

Not the answer you're looking for? Browse other questions tagged or ask your own question.