Crook
Crook

Reputation: 319

as3 - How to make two movieclips share the same code?

I have four movieclips (the player, one arm, another arm, and a weapon) the arms are placed to player and the weapon is placed to the arms. The player should have one arm that displays below it. And the weapon and the other arm that should display on top of it.

However, I have two arms as two different movieclips but they both have the same exact code. If I kept both in the same movieclip, both arms would display on top the player or both would display below the player. And you can't have one movieclip splitting into two layers. And you can't have two movieclips sharing the same class.

So I would like to know if I can make these two arms share the same code without having to write it twice.

Upvotes: 0

Views: 145

Answers (2)

BadFeelingAboutThis
BadFeelingAboutThis

Reputation: 14406

The cleanest approach would be to make a base class. You can make a PlayerArmBase.asfile beside your .fla file, and have it look something like this:

package {
    import flash.display.MovieClip;
    import flash.events.Event;

    public class PlayerArmBase extends MovieClip {
        public function PlayerArmBase(){
            //don't do anything until this item has been added to stage/timeline
            this.addEventListener(Event.ADDED_TO_STAGE, addedToStage, false,0,true);
        }

        protected function addedToStage(e:Event):void {
           //this is the equivalent of where timeline code runs
           //put your code here for your arms

           //for example:
           this.addEventListener(Event.ENTER_FRAME, enterFrame, false, 0, true);
        }

        protected function enterFrame(e:Event):void {
            //do something every frame tick like point to the mouse position
        }
    }
}

Now, you can make both your arms extend this base class. To do so, right click each arm in flash pro, and go their properties. Check the "Export For Actionscript" check box in the advanced settings, then in the "Base Class" field type the name of your class.

enter image description here

Now all the code in that class will apply to both arm MovieClips.


Alternatively, you could put all the common code in the base class, then make another 2 classes (one for each arm) and put specific code in those and have them extend the base class. This is the same as the graphic above, except instead of the base class field, put LeftArm in the class field.

package {
    import flash.events.Event;

    public class LeftArm extends PlayerArmBase {
        public function LeftArm(){

        }

        //we can override the addedToStage function from the base class
        override protected function addedToStage(e:Event):void {
            super.addedToStage(e); //call the base class version of this function

            //do stuff specific to the left arm
        }
    }
}

This way you have code specific to each arm in it's own class, but have all the common code in 1 place. Super classes can access all functions and vars that are declared with the public or protected keywords. protected is like private except super classes can still access it. private can only be used in the class you defined it in.

Upvotes: 2

ElChiniNet
ElChiniNet

Reputation: 2902

Like I had said in the comments, I don't like to code inside the MovieClips, but if you prefer working in that way, do the next:

Put the code inside an empty MovieClip, then put that MovieClip inside each arm MovieClip and make a variable that reference to the parent to do the transformations that you need.

Here you have an example

Upvotes: 1

Related Questions