4. RUTA METODOLÓGICA
4.6. Nivel de Tematización
4.6.10. La televisión como un espacio de reflexión y autonomía para el estudiante y el profesor
The user interface should be modularized into multiple Scenes realized by independent Flash Docu- ments (principle 3). ActionScript code for the Scene (e.g. to initialize its user interface and establish relationships to the application logic) should be placed in an ActionScript class associated with the Scene’s Flash Document (principle 1). In addition, a mechanism is required to perform the transitions between Scenes at runtime.
class MyScene extends MovieClip implements Scene {
private static var theInstance : MyScene;
private function Scene1() { }
public static function loadInstance():Scene { if(theInstance == null) {
theInstance = new MyScene(); }
return theInstance; }
}
Listing 7.3: Implementation of the Singleton pattern in an example Scene classMyScene.
Accessing Scenes Basically, two different principles can be considered for their implementation: Either manage the different Scenes by a central instance (using e.g. the design patternState[Gamma et al.95]) or by implementing them as independent objects without a central instance (using e.g. the design patternSingleton[Gamma et al.95]). Here, the latter approach is used as it is more modular and enables to add additional Scenes more easily to the application. Therefore the Scenes are implemented using the Singletondesign pattern so that they can be accessed and activated from any other Scene. Using Singleton, the Scene class instantiates itself and thus has full control whether to keep or re- initialize its inner state (see also “Resuming Scenes” on page 111). The transitions are managed directly by the Scene class itself as well.
Listing 7.3 shows the resulting code for an example Scene classMyScene. The class construc- tor is declared as private according to the Singleton pattern. The class provides a static operation which returns the current unique instance of the class. Its name is in the original Singleton pattern is
getInstance(). As for our purposes we have to combine this with a loading mechanism (explained
below) its name isloadInstance(). All concrete Scenes are subclasses of the interfaceScenedefined in our framework.
Loading Scenes As mentioned above, it should be possible to load Scenes separately. Be default, Scenes are loaded on demand, i.e. when a Scene should be invoked the first time. To allow execution of other operations during the loading time, an event listener (calledSceneLoadingListener) is passed to the Scene which notifies (call of operationsceneLoaded(s: Scene)) when the loading process has finished. Listing 7.4 shows the resulting implementation of the operationloadInstance().
The loading itself is performed using the built-in ActionScript class MovieClipLoader. As ex- plained on page 150 listeners can be implemented using the object-based mechanisms of ActionScript as ActionScript 2 does not support anonymous inner classes.
Listing 7.5 shows how another Scene defines a SceneLoadingListener to loadMyScene. In ad-
dition, it calls as example the operationstartMySceneofMyScene. The example also shows how a
parameter can be passed (here to the operationstartMyScene).
Connecting a Scene with an ActionScript Class In the Flash versions examined here, it is only possible to associate an ActionScript class to a MovieClip. As Flash Documents are no MovieClips, it is remains the question how a Flash Document representing a Scene can be associated with the Ac-
public static function loadInstance(sll:SceneLoadingListener):Void { if(theInstance == null) {
var loader = new MovieClipLoader(); //use built-in class MovieClipLoader loader.onLoadInit = function(loaded_mc) { //define event listener:
sll.sceneLoaded(loaded_mc); //notify sll when loaded }
loader.loadClip("MyScene.swf", container); //start loading }
else{ //if already loaded then just pass it to sll sl.sceneLoaded(theInstance);
} }
Listing 7.4: The revised operationloadInstancewith a loading mechanism
public function invokeMyScene():Void {
var listener = new DefaultSceneLoadingListener(); var oldScene = this;
var parameter = 5;
listener.sceneLoaded = function (s: Scene) { // Define event handling oldScene._visible = false; // Old Scene must be set invisible
MyScene(s).startMyScene(parameter); // Call operation ’startMyScene’ of loaded Scene
}
MyScene.loadInstance(listener); }
Listing 7.5: Example how to use the SceneLoadingListener to load the SceneMySceneand invoke an
example operationstartMyScene.
tionScript class for the Scene. Our proposed structure provides the following solution: A MovieClip is created which has no visual representation of its own and acts as container for the Scenes. When a Scene is loaded it is placed into the container MovieClip. This can be achieved using the built-in ActionScript classMovieClipLoader whose operation loadClip(url:String, target:Object) specifies as target a MovieClip where an SWF file from the URL is loaded into. The ActionScript class for the Scene can then be assigned to the container MovieClip.
Listing 7.6 shows the resulting (final) version of the operationloadInstance().
Alternatives Flash offers some alternative mechanisms which should be briefly discussed in context of Scenes:
• Flash “Scenes”: Earlier versions of Flash already provided a mechanism called “Scenes” to divide the user interface into multiple independent units. However, it is deprecated today for several reasons explained in [deHaan06].
• Flash “Screens”: Since Flash 83it is possible to structure an application into multipleScreens
which are a quite similar concept like the Scene concept here in this thesis. It is also possible to load the content of a Screen dynamically from a separate document and to associate a Screen with a custom ActionScript class. Moreover, the Flash authoring provides an additional view 3
public static function loadInstance(sl:util.SceneLoadingListener):Void { if(theInstance == null) {
var depth = _root.getNextHighestDepth(); // required for new MovieClip var container = _root.createEmptyMovieClip("container"+depth, depth); //
create new container MovieClip var loader = new MovieClipLoader(); loader.onLoadInit = function(loaded_mc) {
loaded_mc.__proto__= new MyScene(); //attach MyScene as class to loaded_mc sl.sceneLoaded(loaded_mc); } loader.loadClip("MyScene.swf", container); } else{ sl.sceneLoaded(theInstance); } }
Listing 7.6: Final version of operationloadInstancein classScene1
for Screen-based applications showing the application’s Screen hierarchy and the transitions between the Screens.
A drawback of Screen usage is that Screens are not completely transparent, i.e. some of their properties and behavior can not be (directly) accessed by ActionScript code. For instance, the transitions between the Screens must be defined by predefined “Behaviors” instead of within ActionScript classes. Thus, the overall structure using Screens becomes even more complex. Nevertheless, the Screens concept fits well to the structure proposed here and could be a useful future extension.