ActionScript 3.0 Cookbook: Chapter 6, Display List
Pages: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
The init event is fired when the loaded movie has initialized enough that its methods and properties are available to be interacted with. The movie can be controlled only after the init event has been fired from the loader. Attempting to interact with a loaded movie before it has initialized will generate runtime errors.
To control the loaded movie, you'll first need to get a reference to it. This is done via the content property of the Loader class. In the preceding code, the loader variable refers to the Loader that pulled in the external .swf file, so you can access the movie via loader.content. If the loader variable weren't available, the event.target.content path could be used instead to get to the contents of the Loader. This is because event.target refers to the instance that generated the event, which is the same instance that the loader variable refers to.
The content property is read-only, and returns an object of type DisplayObject. In the LoaderExample.swf code, you'll notice that instead of typing the movie variable as a DisplayObject, the same type as what the content property returns, the * type was used. This is necessary because trying to call the getColor( ) or setColor( ) methods on the movie reference generates compile-time errors if movie is typed as a DisplayObject.
The movie being loaded, ExternalMovie.swf, has two public methods available for interaction. These methods are not part of the DisplayObject class; therefore, trying to call one of the methods from a variable of type DisplayObject is an error. The * type allows you to call any method that you'd like on the loaded movie. If the method does not exist in the loaded movie, a ReferenceError is thrown during execution.
The getColor( ) method returns the color of the circle in ExternalMovie.swf to LoaderExample.swf. The LoaderExample.swf reports the color as 0, which is the same as 0x000000, or the color black. The setColor( ) method allows LoaderExample.swf to change the color of the circle drawn by ExternalMovie.swf. In this case, the color of the circle is set to red, and you can see that the ExternalMovie.swf updates the display after the new circle color value is set.
It is only possible to interact with .swf files of Version 9 and above using this technique. When loading Version 8 and below .swf files, this technique won't work because ActionScript 3.0 code runs independently of ActionScript 1.0 and 2.0. Communication with these .swf files is not trivial and involves using LocalConnection as a workaround to send and receive messages. See Chapter 19 for details.
Recipes 1.13 and 6.6
You want users to interact with your movie using their mouse.
Use the various mouse events to listen for mouse interactions on display objects of type InteractiveObject. Use the read-only mouseX and mouseY properties from DisplayObject to examine the mouse location relative to a display object, or the localX and localY properties from the MouseEvent passed to a mouse event handler.
This excerpt is from ActionScript 3.0 Cookbook. Well before Ajax and Windows Presentation Foundation, Macromedia Flash provided the first method for building "rich" web pages. Now, Adobe is making Flash a full-fledged development environment, and learning ActionScript 3.0 is key. That's a challenge for even the most experienced Flash developer. This Cookbook offers more than 300 solutions to solve a wide range of coding dilemmas, so you can learn to work with the new version right away.
Basic mouse interaction can be created with the SimpleButton class, as described in Recipe 6.5. The SimpleButton class provides an easy way to create a clickable button with different button visual states: up, over, and down.
However, there are times when buttons just don't provide enough interactivity. By listening to the various mouse events, you can create interesting interactive experiences. For instance, consider that you want to track the mouse cursor to create an interactive drawing program, drawing lines on-screen based on the user's mouse movement. Or, consider that you have a maze that a user must navigate their mouse through without colliding with the walls to find the exit. Or, perhaps the user's mouse movement needs to control the direction of a golf club, and the mouse button is used to swing.
These situations require use of the special InteractiveObject display object, which provides the ability to respond to the user's mouse. If you go back to the introduction for this chapter, you'll recall that the InteractiveObject class is a base class fairly high in the display object class hierarchy. Because of this, the Sprite, Loader, TextField, and MovieClip classes are all examples of the InteractiveObject class since they fall underneath InteractiveObject in the hierarchy, and you may already be familiar with their use.
Instances of the InteractiveObject dispatch the necessary events specific to mouse interaction. The following is a list of more useful mouse events:
click
doubleClick
mouseDown
mouseUp
mouseOver
mouseMove
mouseOut
mouseWheel