Search code examples
actionscript-3flashoopmovieclip

Using a separate .fla file as a MovieClip in a larger project


Just to preface: I haven't used Flash in a long long time, however, I am still aware of the environment.

Backstory: I created a small .fla to perform actions on a MovieClip on the stage (in my case, a health/HP bar). I made the health effect using a Document Class (HealthBar.as).

The question: What I'm trying to figure out now is how, in a totally separate .fla, to create multiple instances of these health bars and be able to access the methods in Document Class HealthBar.as from the Document Class in this new .fla

If I am doing this incorrectly in the first place, feel free to yell at me, and let me know how doing something like this SHOULD have been done.

Thanks for any help


Solution

  • You're halfway there with a document class. Now you just need to make it into a proper class in your com.domain.className (or drop the .as file into the same directory as your fla). While creating classfiles is trivial, online examples seem to muck it up, so here's Adobe's official demo (bleh).

    That said, creating more healthbars basically looks like this...

    Class

    package {
        public class HealthBar extends Sprite {
            public function HealthBar() {
                // constructor
                trace("Healthbar created")
            }
        }
    }
    

    Document Code

    import HealthBar;
    
    for (var i:int = 0; i < 10; i++) {
        var randomHealthBar:HealthBar = new HealthBar(); // <-- magic sauce
        addChild(randomHealthBar);
    }
    
    // traces: "Healthbar created" 10 times