Request for help on architecture...

Hi everyone,

I started using AS3 (well, Flash on Flash Pro) a couple of months ago for a project so I’m a “novice”. My main need is to create a “master MovieClip” on the stage, I have created all basic functionality for that movieclip on separate movieclips and tested things but now I’m at a point where I need to bring them all together and create something that is expandable and well structured (object oriented). My need is the following:

  1. Dynamically load assets for the MovieClip
  2. Dynamically load classes that “install” various functionality on to assets from #1. Number of classes is unknown until runtime (user selects what features he/she wants on the movieclip), so I have figured out how to dynamically create a class from an SWF file in the local folder - this works. I just need help/idea with how to load multiple classes on the same assets (for example, movieclip has a baby’s figure, one class would load clothes on it, another class would load skin color of the baby, another class would load amount of hair (or a cap), etc)…
  3. If there is a way to “embed” user selections so that next time the user’s selections are automatically loaded. This is not too important at the moment.

I have all components for step #1 and #2 coded and they all work individually. I am requesting help on how to load multiple classes on same movieclip. If the main class is called “baby”, how do I extend all these other classes such as “hair”, "clothes’, “skinColor”, etc? I have figured out how to dynamically add mouse events on each class so I think I can manage the rest if someone can please point me in right direction. I read some articles on “dynamic class” but they also discouraged from using it due to extreme slowness if many components are loaded (such as my case). So my question - is there a way to dynamically “extend” a class onto another class? (I apologize if I’m using wrong words to describe my question, hopefully I am clear enough on what I’m trying to do). To give a better idea, I have an example code down here (I know it has issues, but hopefully it will show what I’m trying to do):

    package 
    {
        public class baby extends MovieClip
        {
          public var babyMC:MovieClip = new MovieClip();
          public function baby(mc:MovieClip=null)
          {
             if (mc == null)
                babyMC = this;
             var babyClothes:MovieClip = new clothes(babyMC);
             var babySkinColor:MovieClip = new skinColor(babyMC);
             var babyHair = new hair(babyMC);
             // if I "addChild" above movieclips here, it will create new children instead of using movieclip that is passed into it - not sure how to do this best, I want "clothes", "skinColor", and "hair" functionality **added** to MovieClip "mc" that was entered as parameter to the constructor code.
          }

          public function property1()
          {
          }
        }
    }
        
package
{  
     public class clothes extends baby
        {
              public function clothes()
              {
              }

              public propertyA()
              {
              }
        }
}

In above example, I want the main movieclip to be able to access both property1() AND propertyA() if a user selects to have clothes on the baby, otherwise property1() should work as default available function.

Thank you for any help!
-Desi
(I am using Flash Professional/AS3, CS6 on Windows 7)

You probably won’t actually notice any slowness.

To be blunt, you’re sort of abusing the notion of what a class is supposed to be in AS3. That’s okay, I can work around it and you’re a “novice”, so it’s understandable.

Anyway, one way to approach this that may fit into your current setup looks like this:

package {
	import flash.display.*
	public dynamic class baby extends MovieClip {
	  public var babyMC:MovieClip = new MovieClip(
												 
	  public function baby(mc:MovieClip = null){
		 if (mc == null) babyMC = this
		 var babyClothes:MovieClip = new clothes(babyMC)
		 // if I "addChild" above movieclips here, it will create new children instead of using movieclip that is passed into it - not sure how to do this best, I want "clothes", "skinColor", and "hair" functionality **added** to MovieClip "mc" that was entered as parameter to the constructor code.
	  }

	  public function property1(){}
	}
}
package {
	import flash.utils.*
	import flash.display.*
	public class clothes extends MovieClip {
		public function clothes(mc:MovieClip){
			for each(var Var:* in describeType(this)..variable){
				mc.constructor.prototype[Var..@name] = this[Var..@name]
			}
		}

		public var propertyA:Function = function(){
		  trace(this, 'foo')
		}
	}
}

That way, you can later call:

var b:baby = new baby()
b.propertyA() // [object baby] foo

That’s what I’ve done above. Though I should warn that it’s pretty antithetical to AS3’s class system.

1 Like

Hi Krilnon:

Thank you so much for clarifying with an example, I very much appreciate your response. And once again I apologize for not following proper programming method (but I’m trying & learning as I go), I would definitely like to do this the proper way because the application I’m developing is going to be used for a long time. I much rather do things right now to provide a better foundation rather than making a mess, so thank you for warning about my current algorithm!

I’m going to try dynamic class if that’s not going to be noticeably slower (I also say that because most part of my project will be running on a computer locally, not on a web-browser but as a standalone application). I’m guessing that’s going to speed up things further.

Just out of curiosity, how would you ideally structure architecture of what I’m trying to do? Is what you showed a good way to do it for a long-term project? If not, is there any other suggestion or website where I can read up and try to improve my class/object-oriented algorithm? So far I have been trying to dig through Adobe online help pages, this site, and Flashkit site. I tried actionscript3.org but that one seems to have a lot of spam.

Thank you again!!!
-Desi

One thing you might want to look into is the decorator pattern:

If you look at the second Java example, you’ll see that they do something very similar to what it seems like you’re talking about with the baby and clothes:

c = new Whip(new Sprinkles(new Milk(new SimpleCoffee())))

You could also look into traits or mixins. If you’re looking for ActionScript-specific implementations of those patterns, just Google around for the names + AS3, people have adapted most approaches to AS3 at some point.

Since you asked how I would ideally structure the app: I don’t know. My suggestions above are how some professionals would, given the amount of information you posted.

Mmm, depends. Maintainability and flexibility are often at odds with each other. Reflection, the technique I used in my first reply, is the ultimate flexible programming tool, so it’s at odds with maintenance in many ways. But it all depends on the goals of the project: do you predict changing demands on the code base (suddenly requiring walking babies), or just continual content additions, like adding jewelry (tiaras, etc.), tattoos, etc., which don’t require any changes to the architecture of the application.

1 Like

I think that’s exactly what I’m looking for - “Decorator Pattern”! Thank you, thank you! Also helps since it has actual code examples, I didn’t realize until now that Java looks very similar to AS3! I haven’t looked at Traits or mixins links yet, I’ll do that tomorrow before continuing my project. First I’m going to read/understand that wikipedia page on Decorator Pattern, I’m happy to see that these architecture designs already exist.

As far as my future goals with the project, it’s the first option that you mentioned (such as walking babies) - the goal is to add “functionality” such as adding custom animations or custom response to a mouse-click/etc (primary need) rather than just content additions (secondary need). I have to say that I really like this “Reflection” technique even if it weren’t best/recommended path because of ability to alter program execution at run-time. But for now I’m going to focus on Decorator Pattern, my immediate need! :smile:

Thank you again, have a good night!
-Desi

1 Like

By the way,

c = new Whip(new Sprinkles(new Milk(new SimpleCoffee())))

is exactly what I’m trying to do!!! :slight_smile: :slight_smile: :slight_smile: You have saved me tremendous amount of time showing that link!! Thanks!!!