Breaking code into multiple swfs

I want to break my flash actionscript source code into manageable pieces. The application that I’m creating works something like this: you’re presented with an initial screen. From that screen, you click a button, and you’re brought to another screen. From that screen, you press a button and you’re brought to yet another screeen. I would like to break each of those screens into it’s on swf file. Is that possible?

I’m not relying on the swsiwyg editor for this. Basically, I my .fla file consists of one frame, on one layer that calls the main class, and that class takes it from there. That main class then switches the to apropriate screen when it needs to, such as when the user clicks a button.

Now, each of those scene makes use of some common framework code, so the directory structure looks something like: /root/common_code, /root/scenes/scene1, /root/scenes/scene2, etc. Each /root/scene/X corresponds to a screen in my application.

Again, each scene makes use of AND it’s called by /root/common_code

So it’s kinda doing that. But I don’t if/how is possible to break each of those in it’s own swf.

I want to do this so that I can work on each ‘screen’ independent of the main code, and then have that code be able to load screens on demand.

Thanks

I feel this is an issue that would be important to advanced action script developers, which leads me to believe that I may not be giving enough information to such users to be able to help me. If that is the case, please let me know.

There’s a problem, big one really. I’m sorry to say that architecting an app in this manner will not work. You’re using “scenes” right? You will need to move on from the “old” way of working flash. Even if you tried to make the “scenes” work and load different .swfs, you will have many problems. Most notably:

  1. Event listeners in one .swf file are forbidden from registering for events with objects in the other .swf.

  2. When an event-dispatch targets an object in a display hierarchy, any objects inaccessible to the target’s .swf file are not included in the event flow.

Since flash is an event based language, this renders things mostly impossible to do.

What your are describing is very much like an Observer pattern. Where multiple classes that all depend on the state of a specific class can subscribe to that class and get automaticly notified when it’s state changes - or when a user manipulates the enviroment. This way the data is in sync with each other.

Another possibilty is the MVC approach. Right now I’m using a variation of this model for an AIR app. Where the Model class is a static Singleton that dispatches events to the Controller and renders the views- or in your case the “scenes”.

Thank you for you reply, ultraky2

I’m not using flash scenes. I should have never used that term to describe breaking up my code. I tried to use the word “screen” to define different a complete, logical chunk of the application. I’ll use the word “module” to describe that now.

A module could be considered similar to a flash “scene”, meaning that you could think of it as a separate entity within the application. This separation is only logical, though. So within the application you could have a module for drawing a picture, and another one for viewing that picture. You could think of the first module as a mini paint program, and the other one as a picture viewer. My goal is to have the drawing module and the viewing module (a gallery, maybe) as separate SWFs which are loaded on demand by a base SWF (whose purpose would basically be to load theses things and manage them while they live).

ultraky2, I actually have an abstraction in my framework which is an MVC subsystem. This very subsystem is what I’m trying to break down. So there’s something like:


<root_dir>/
      framework_code/ <-- handles general, environment stuff.
      modules/ 
         controller/
          model/
          view/

Under the modules directory lives an MVC subsystem. The application that I’m actually creating is a video game, an RPG. So, to give concrete example: You could fight monsters in different locations. So you’ll randomly be walking in a town, and encounter a monster to fight. You’ll enter a little ‘Screen’ where you’ll be fighting that monster. So there will be different maps, different battle grounds, among other things. There will be a controller for each different module type. So there will be a controller that handles town maps, a controller that handles battle grounds, and so on. There will be many views for each controller (so many to 1 relationship). The models will be shared among all views.


modules/
     controller/
          town_map.as
          battle.as
     models/
     views/
          town_map/
               icy_town.as
               water_town.as
          battle/
               cave_battle.as
               desert_battle.as

In this example, which is a real one that I have working, but which is not broken up into multiple SWFs, there are 4 modules (currently this thing is compiled into 1 swf):

  1. town_map -> icy town (consist of the town_map controller, the icy_town view, and makes uses of possibly all models)
  2. town_map -> water_town (consist of the town_map controller, the water_town view, and makes uses of possibly all models)
  3. battle -> cave_battle (consist of the cave_battle controller, the cave_battle view, and makes uses of possibly all models)
  4. battle -> desert_battle (consist of the desert_battle controller, the desert_battle view, and makes uses of possibly all models)

It’s simple to see the benefits of breaking this thing into multiple SWFS: less loading, proper code containment, among others.

You can certainly represent each of these “screens” with a separate SWF. You may run into some security policy sorts of issues later, depending on where the SWFs are that you’re loading. Also, it doesn’t always make sense to put everything in a separate SWF- it makes more files for you to compile, and if you’re using a common library asset or class in your SWFs, it will exist in multiple files and therefore be redundant.

All the info you need (I think) is in the Loader livedoc.

This whole directory system that you’re talking about? Once you get your SWFs loading, you can set up whatever directory system you want. The ball’s in your court on that one.

I think I have a pretty good handle on HOW to load SWFs. My issue is more along the lines of: I have all this source code under a directory structure, but how do I tell flash compiler “Create one swf from these set of files, create another one from these ones, …”.

Do I have to create a separate .fla file for each SWF to be created and added it to the same project?

Simply put, each SWF needs to be generated from a unique FLA file. You can’t make multiple SWFs from one FLA simultaneously.

Thank you, Rezmason