How can I make a superclass that is available in Flex and Flash?

347 views Asked by At

I am working on a project that is utilizing Flash Professional and Flash Builder. The art team creates assets in Flash Pro and publishes the project as a SWC. I then copy the SWC into my Flash Builder project's lib directory and access the assets through that.

The art team is creating a lot of the same type of thing; let's say they're making lots of lizard symbols/components. All of these lizards have similar animations (walk_left, walk_right, etc.) and because of this they all have constants defined in ActionScript (WALK_RIGHT_START, WALK_RIGHT_END, etc.) relating to the keyframes in their animations.

From the Flex side of things, whichever Lizard I load, I want to be able to tell it to walk left. So I created a superclass in Flash Professional and now all of the AS classes for the lizards in Flash Professional extend this class - Lizard.as

This is all fine and good, but when I want to add more complexities to my lizards, I don't want to have to open up the Flash Pro project, edit the class, recompile the SWC, send it back to Flex, and then test it out. I want to be able to have my Lizard superclass available for editing in Flex - so I made another superclass in Flex: Lizard.mxml

In Flex, each lizard component (like Gecko.mxml) currently extends Lizard.mxml, and each contains an AS component defined in the SWC (Gecko.as in this case).

This seems pretty convoluted: An mxml component extending an mxml superclass, containing an AS component which extends an AS superclass.

What I'd like to do is define one superclass in the Flex project, have my Flash Pro defined components extend this class (which is the part I believe to impossible), and then have my Flex Components (Gecko.mxml) extend those.

Is there any way to do this?

1

There are 1 answers

2
Daniel On BEST ANSWER

You can't change a class, you can only extend it.

Couple years ago I was working on a project where the interface was done in Flash and compiled in the flex compiler.

I had different ui components that consisted of different classes. But needed to treat them the same for the logic I was putting in. That meant all the classes had to have the same base class, which had to by my own base class. so a Button and a List both had to extend a customUIClass. Which as far as I know you cannot do (please let me know if you're reading this and know of a way)

my solution was to create a wrapper.

As applied to your issue would go something like this

  • make a "superclass" called Lizard(or animal,organism etc) that extends a MovieClip( or casaMovieClip).
  • Lizard would have a variable private var _view:MovieClip
  • and a init constructor 'init($type:MovieClip)` (not the actual constructor)
  • where _view=$type and you add view addChild(_view);

then you would create your actions like this

function walk():void{
   _view.gotoAndPlay("WALK");
} 

last thing would be to create the Gecko class, which would extend the Lizard, and would have this as constructor

function Gecko(){
   init(new flaGecko());
}

then if you still need to override any functions you can do that

or you could just create var gecko:Lizard = new Lizard(new flaGecko()); but then you would need the init() to be the constructor.

for further reading do look up the Decorator Design Pattern. I've found the AS3 design patterns book to be quite helpful.