PDA

View Full Version : Help making a solution


Flash Gordon
08-20-2007, 11:46 PM
Where to begin....

I have to create these "InfoModules" that are manufactured by an .swf file. The modules textual appearance changes by an xml. Which InfoModule loads at run time I don't know as that is also determined by the xml file and a factory. The same InfoModule has be able to have additional "decorations" to it (see first image attached and both are a representation of InfoModule1) depending on which .swf manufactures it. In the module on the left, there is 1 additional visual element added, that is a line. In the module on the right, there are 3 additional visual elements added: 2 lines and a grey box. However, there may be no visual elements added as this is to be determined by the .swf that makes the InfoModules. For instance, if InfoModule2 is instantiated, no decorations are needed.

Here is how the modules are being manufactured:

private function onXML(event:Event):void
{
_xml = XML(event.currentTarget.data);

_module = infoModuleFactory(_xml.@module);
addChild(_module);
}

private function infoModuleFactory(def:String):InfoModule
{
// dynamically get class instance from xml doc
var ModuleReference:Class = getDefinitionByName(def) as Class;
return new ModuleReference(_xml);
}

a diagram of the Modules is also attached.

I just don't know how to add these visual elements. I was thinking of getting rid of the getDefinitionByName way of doing this, and making each "template" .swf file have its own factory and when InfoModule1 is created in the factory, adding those visual elements. If InfoModule2 is create, no visual elements...etc. The problem with this is when I add a new InfoModuleNth, I will have to go back through all the "template" factories and include this new InfoModule. Maybe that just comes with the territory....

:confused:

Flash Gordon
08-21-2007, 12:57 AM
Here is code of what I was thinking. Please let me know how this looks to you. I need it to stay flexible and easily maintainable. I don't want to program this thing and then 6 months down the road realize I did a horrible job. I need your experience!

DocumentClass

package
{
public class DocumentClass extends Sprite
{
private var _factory:InfoModuleFactory = new InfoModuleFactory();


public function DocumentClass()
{
//
}

private function onXML(event:Event):void
{
_xml = XML(event.currentTarget.data)

_infoModule = _factory.create(InfoModuleFactory.INFO_MODULE1);
_infoModule.consume(_xml);
_infoModule.build();
_infoModule.x = 20;
_infoModule.y = 30;
addChild(_infoModule);

// can only decorate after module is built because decorations are based upon the inner works of the modules
var decorate:DecorateModule = new DecorateModule(_infoModule);
decorate.build(InfoModuleFactory.INFO_MODULE1);
}
}
}


InfoModuleFactory: this class creates the actual modules

package
{
public class InfoModuleFactory
{

public static const INFO_MODULE1:String = "infoModule1";
public static const INFO_MODULE2:String = "infoModule2";


public function InfoModuleFactory()
{
//
}

public function create(type:String):InfoModule
{
var infoModule:InfoModule;
switch(type)
{
case INFO_MODULE1:
infoModule = new InfoModule1();
break;
case INFO_MODULE2:
infoModule = new InfoModule2();
break;
}

return infoModule;
}
}
}

DecorateModule: adds specific graphics to each module including the scrollbar graphics. Each main .swf file will have it's own DecorateModule class.

package template1
{
public class DecorateModule
{

protected var _module:InfoModule;
public function DecorateModule(module:InfoModule)
{
_module = module;
}

public function build(type:String):void
{
switch(type)
{
case "infoModule1":
addLine();
break;
case "infoModule2":
// nothing
break;
}
}

private function addLine():void
{
var line:MovieClip = new LineGraphic(); // gets line from library
var mask:DisplayObject = _module.getChildByName("mask");
line.x = mask.x;
line.y = mask.y + mask.height + 20;
_module.addChild(line);
}
}
}


Cheers if you got this far :o
:)

Flash Gordon
08-22-2007, 07:12 PM
Anybody?

If need be, I'll play you for your input.

Assertnfailure
08-25-2007, 05:41 AM
Well, if there's no way around the use of a unique concrete class for each module, but you want to make it extensible, why not have each concrete class be represented as an external swf, then you can have your factory load the external swf that matches the name of the module in the xml. Then have each swf's document class abstracted to the same base class, and you can handle each one the same way.

Anybody?

If need be, I'll play you for your input.

You'll play me?? I've been had! =o

Flash Gordon
08-25-2007, 07:43 AM
...then you can have your factory load the external swf that matches the name of the module in the xml. Then have each swf's document class abstracted to the same base class, and you can handle each one the same way.Much thanks for the reply! :) If I understand you correctly, that is about how I have it now, except for that I'm not loading .swf's but just instantiating classes. What is the benefit of making them actual .swf as apposed to just classes?

You'll play me?? I've been had! =o
HA! Chump, you've been punk'd. LoL....DOH....:) .... quite the typo.

Thanks again for the response!

Assertnfailure
08-25-2007, 07:55 AM
Well external swfs are the only way to inject classes into a precompiled application. There are two benefits with this.

1) You can add more swfs instead of recompiling the same swf over and over...which is nicer for distribution.

2) You cut down on the filesize of the primary swf, because the code is dispersed across multiple files, and not all files need to be loaded if they aren't all used.

Flash Gordon
08-25-2007, 08:51 PM
That's true about the loading .swf's at runtime and that's a good point. However, in my case I would also need to recompile a separate DecorateModule for each template and load that as well as DecorateModule is unique to each template. You gave me something to definitely think about!

The strange thing about OOP is it doesn't always lend to the smoothest user experience. It can slow down game play and if I'm loading 15 different modules it can increase initial loading times (having to wait for loads rather than just instantiating classes). However, it does always make maintenance easier, so in the end it is a decision of which is the more important for this situation.

Thanks a lot for all the great responses, Assertnfailure. I really appreciate the help you have given me!

Assertnfailure
08-26-2007, 08:28 PM
Yeah....OOP is really more of a utilization of faster computers to focus on development efficiency rather than process efficiency. After all, even simple things like breaking code into functions uses additional resources.

Haha, no problem dude. Conventions and architecture are much more interesting topics than "how to attach a movieclip," and it seems you're the only one utilizing this section so far. ;)