Creating interfaces as data types
319
5.
In Box.as, type the following ActionScript code into the Script window:
class Box implements IMovable {
public var xPos:Number;
public var yPos:Number;
public function Box() {
}
public function moveUp():Void {
trace("moving up");
// method definition
}
public function moveDown():Void {
trace("moving down");
// method definition
}
}
6.
Save your changes to the ActionScript document.
7.
Create a new Flash document named
boxTest.fla,
and then save it in the same directory as
the two previous ActionScript documents.
8.
Select Frame 1 of the Timeline, open the ActionScript editor, and then type the following
ActionScript code into the Actions panel (or Script window):
var newBox:Box = new Box();
This ActionScript code creates an instance of the Box class, which you declare as a variable
of the Box type.
9.
Save your changes to the Flash document, and then select Control > Test Movie to test the
SWF file.
In Flash Player 7 and later, you can cast an expression to an interface type or other data
type at runtime. Unlike Java interfaces, ActionScript interfaces exist at runtime, which
allows type casting. If the expression is an object that implements the interface or has a
superclass that implements the interface, the object is returned. Otherwise,
null
is
returned. This is useful if you want to ensure that a particular object implements a certain
interface. For more information on type casting, see
Chapter 4, “About casting objects,”
on page 111
.
10.
Add the following code at the end of the ActionScript code in boxTest.fla:
if (IMovable(newBox) != null) {
newBox.moveUp();
} else {
trace("box instance is not movable");
}
Содержание FLASH 8-LEARNING ACTIONSCRIPT 2.0 IN FLASH
Страница 1: ...Learning ActionScript 2 0 in Flash...
Страница 8: ...8 Contents...
Страница 18: ...18 Introduction...
Страница 30: ...30 What s New in Flash 8 ActionScript...
Страница 66: ...66 Writing and Editing ActionScript 2 0...
Страница 328: ...328 Interfaces...
Страница 350: ...350 Handling Events...
Страница 590: ...590 Creating Interaction with ActionScript...
Страница 710: ...710 Understanding Security...
Страница 730: ...730 Debugging Applications...
Страница 780: ...780 Deprecated Flash 4 operators...
Страница 830: ...830 Index...