I’m trying to replace instances created by dragging and dropping into a MovieClip through Flash’s IDE with actual classes so I can add them to a game loop and have them as actual entities. In other words, I’m trying to create a streamlined way to allow developers to visually add their entities to a platforming engine I’m working on.
This is my third attempt at it and I’m completely stuck.
The code below loops through a movieclip that contains an exported symbol with a class linked to it named MyEntity. However, it loses its extension to BaseClass and thus doesn’t move when compiled.
It inherits: MovieClip > BaseClass > MyEntity. However when compiled with the IDE it ignores BaseClass and just does MovieClip > MyEntity.
My code is designed to find and store the position of MyEntity, remove it from the container movieclip, add a brand new instance of it (with the base being proper) then set that new instance to the same position of the original.
for ( var i:int = 0; i < LayerInIDE.numChildren; i++ )
{
// first we want to get all the display objects in the layer
// these are objects that were placed from within the Flash IDE (ie. dragged and dropped into the MovieClip
var original:DisplayObject = LayerInIDE.getChildAt( i );
// we want to get the class of the display object so we can recreate it as a specific entity class that it
var originalName:String = getQualifiedClassName( original );
var originalClass:Class = getDefinitionByName( originalName ) as Class;
// debug trace, see output below
trace( originalName, originalClass, originalClass is BaseClass );
// filter out movieclips
if ( originalClass != MovieClip )
{
// remove the original
LayerInIDE.removeChild( original );
// recreate the class with the correct extension
var newEnt = originalClass();
newEnt.x = original.x; newEnt.y = original.y;
LayerInIDE.addChild( newEnt );
}
}
This does not work.
It outputs Game.entities::MyEntity [class MyEntity] false. MyEntity is a proper class and DOES extend from BaseClass. However, the issue is the IDE weirdly removes the reference to the base class – as if MyEntity never had a base class. I cannot seem to recreate it as getting the reference to the class also returns that MyEntity never had a BaseClass. However, if I type in var newEnt = MyEntity(); instead of getting the class name through getDefinitionByName it works normally and extends from BaseClass.
I need it to extend from BaseClass as that is the main class all entities in my game engine require to use.
Any ideas? Or is there a better way?
After much tinkering and generally breaking Flash, I got the solution to my problem. Thanks to Stephen Braitsch‘s help for getting me in the right direction.
Okay, so this solution is a little complicated (and hacky) but it gets around the issues I was previously discussing.
First you’ll need the main function that scans through all the display objects in a specific movieclip. I’ve named mine
LayerInIDE.There’s two functions needed.
ReplaceEntsandRecreate.ReplaceEntsscans through all the objects and determines if they extend fromBaseClass. If they do, it goes ahead and throws them throughRecreate.Recreatetakes the original class and position and recreates the class allowing us to access the asset during runtime properly.Then the
BaseClasscontains:Once that is all set, we’ll need to also make the
MyEntityclass:Just a final note,
Games.entities.Layeris just a simpleMovieClip. I only created my own class for it for organization purposes in this.The solution here is simple. When a user drags an object that extends from
BaseClassinto a layer, the function runs on start up and replaces the asset with the proper class and allows for an entity manager to take over and handle the asset appropriately. It allows for what I wanted to achieve, an IDE-powered level editor that automatically does all the work placing the entities where you want them.Thanks for all the help guys.