7.2 A General Structure for Flash Applications
7.2.3 Complete Structure
The Scene concept described in the last section builds the core for the application structure. This section describes the other elements according to the principles from section 7.2.1 and shows the resulting overall structure. Figure 7.9 exemplifies the structure by an extract of the Racing Game example from chapters5 and 6.
Application Logic The application logic is implemented in conventional way as ActionScript classes (principle 1). They constitute the ‘model’ in terms of MVC and are placed in a packagemodel(prin- ciple 2).
In the Racing Game example, the foldermodel would contain ActionScript classesCar, Track,
Player, etc. (fig. 7.9).
Media According to principle 4, (complex) Media Components are located in separate files in a central foldermediaso that it is possible to apply them multiple times within the application. Graph- ics and animations are implemented as a Flash Document containing the graphic or animation as MovieClip. In general, audio, video and images are not created in Flash itself but just imported into Flash Documents. Thus, within our structure, the audio, video or image files are placed into the folder
media IntroVideo.flv model ... Car.as CarAnimation.fla TrackAnimation.fla TrackAnimation Track.as Player.as ... Game.fla Game Car.as CarAnimation EngineSound.mp3 Game.as car engineSound carAnimation time time_textfield time_label ... Time.as ... refers to refers to
associated with associated with
associated with
for each scene in the application
once for the whole application
Figure 7.9: Proposed Structure applied to the Racing Game example.
To apply media from foldermediain different Scenes it is possible to either create a reference in the Scenes’ Libraries or to load them dynamically during runtime into the Scenes. For the purpose here, the former possibility should be preferred as it enables the developer to visually integrate the ex- ternal MovieClip into the user interface at authoring time. A reference in the Library can be specified in the properties dialog of Library Items (invoked in their context-menu). For example, in case of a MovieClip, it is possible to specify a reference on a MovieClip in an external Flash Document. As result, the MovieClip adopts the content from the referenced external file. The relationship between source and target MovieClip is by reference, i.e. changes in the external MovieClip are adopted in the referencing MovieClip.
The foldermediain in figure 7.9 contains for instance a Flash DocumentCarAnimation.flawhich contains the actual MovieClipCarAnimation. It also contains sound and video files. The files in the folder media are referenced from elements e.g. in the SceneGame.
User Interface Elements The user interface elements are located in the Scene on the Stage. They are either instances of Media or from Flash user interface components. In our structure they constitute the ‘view’ in terms of the MVC pattern (principle 2).
The user interface elements are associated with ActionScript classes as well (principle 1). It de- pends on the type of element whether this is directly possible in Flash. For instance, for Flash user interface components can be not be associated with custom classes directly. It is also not possible to subtype them as no inheritance mechanism exists for Flash Components. Thus, all user interface ele- ments are encapsulated into MovieClips which are associated with an ActionScript class. As explained in section 7.1.3 (“Accessing Stage Content”) the MovieClip’s content (e.g. a Flash Component) can
be accessed in the ActionScript class as class properties via their instance name. The ActionScript classes are placed into a folder with the name of the Scene they belong to.
In the example in figure 7.9 the SceneGamecontains for instance the user interface elementscar andtime. They are encapsulated into MovieClips so that they can be associated to the ActionScript classesCarandTimein the folderGame. The MovieCliptimeconatins two Flash Components, a text label for the name and a text field for the actual value. The MovieClipcarcontains two media instances
engineSoundandcarAnimation which refer toEngineSound.mp3and the MovieClipCarAnimation
in the central foldermedia.
The relationship between user interface elements and application logic is implemented by the design patternObserver[Gamma et al.95] (associations between ActionScript classes are not shown in fig. 7.9). ‘View’ classes thus implement the interface Observerwhile ‘model’ classes extend the classObservableand notify their Observers after they have changed.
Like in many other implementations the ‘Controller’ part (in terms of MVC) is simplified by placing the event listening operations directly into the ‘View’ class to avoid a very large number of small classes. Event handling operations are specified either by overwriting operations of the
MovieClip directly (e.g. defining an operationonKeyDown()) or by attaching anonymous listeners
to its content (e.g. a mouse listener for a contained user interface component). As explained in section 7.2.2 (“Loading Scenes”) ActionScript 2 does not support anonymous inner classes but the object-based mechanisms can be used instead.
Scenes Finally, at some point the application must be initialized and the relationships between ‘Model’, ‘View’, and ‘Controller’ must be established. As the Scenes are the application’s main building blocks they are used for this task. They contain the user interface elements on their stage so that they are available as class properties in the Scene’s ActionScript class. This class thus initial- izes the application logic by creating new domain instances or receiving them by parameters of Entry Operations. It initializes the connections to ‘View’ classes by (due to the Observer pattern) calling
addObserver()operations. Besides, the Scenes are implemented as described in section 7.2.2.
Figure 7.9 shows as example the SceneGame. It is represented by a Flash Document containing user interface elements, an associated ActionScript class, and a folder containing the ActionScript classes for the user interface elements.