« Home « Kết quả tìm kiếm

Creating a Class


Tóm tắt Xem thử

- The code for defining an ActionScript class exists in its own .as file (remember that the .as file extension stands for ActionScript).
- If you create 13 classes, you have to create 13 .as files.
- A class file is nothing more than a text file containing ActionScript code.
- The name of each class file is the name of the class followed by the .as file extension.
- For example, if you create a TestClass class, it has to exist in a file with this exact name: TestClass.as..
- A class file can be created from within the Macromedia Flash authoring environment by opening the ActionScript editor (File >.
- Simply creating a text file with an .as extension doesn't automatically make it a functional class file.
- The contents of the file must be ActionScript whose syntax defines the class (including its methods and properties).
- For example, the following code, inside the TestClass.as file, is the beginning of a valid class file:.
- After the class statement is the name of the class being created: TestClass.
- The name of the class must be the same as the name of the .as file that contains the code defining the class.
- therefore, the TestClass class definition must be in a file named TestClass.as..
- A class file defines characteristics about the class by creating properties and methods..
- The properties and methods of a class are referred to as members, and they are all defined within the outermost curly braces of the class.
- In this example, the properties length, height, and width and the method getVolume() are all members of the Cube class..
- To create a new instance of a class to use its functionality in your project, the class must have a constructor method defined.
- The constructor must have the same name as the class.
- Let's modify the Cube class we just created to contain a constructor method and accept parameters so that we can create a custom cube instance:.
- After coding the Cube class, you save it as an .as file named Cube.as in the same directory as your Flash authoring file.
- (Alternatively, you can place your .as class files elsewhere, which we'll discuss in a moment.) You would create a new instance of the Cube class on a frame in your Flash authoring file in the following way:.
- You can then get the volume of the myCube object this way:.
- var volume:Number = myCube.getVolume();.
- By default, the properties and methods available to an instance of a class are fixed by the.
- properties and methods defined in its class file.
- For example, the Cube class file defines three properties (length, height, and width) and one method (getVolume.
- As a result, the myCube instance of the Cube class can access those particular properties and that method:.
- myCube.getVolume.
- myCube.length = 6;.
- myCube.height = 4;.
- myCube.color = "red";.
- In your application, perhaps Cube instances shouldn't have color properties, and instead you meant to assign a color property to the myCup instance of the Cup class, which would have been acceptable.
- When you export your movie, Flash's compiler sees that you've set the color property for an instance of the Cube class..
- It then looks at the Cube class definition to see whether the color property has been defined there.
- no object instance is allowed to perform an action unless that action is explicitly permitted by the class.
- While this can be a great way to help you debug your applications—because Flash displays an error when you attempt to use an object in the wrong way—there may be times when you do want instances of a custom object class to be able to add or access properties and methods that were not defined in its class file.
- You would simply need to add the dynamic class modifier to the class definition (notice the first line):.
- With the addition of the dynamic modifier, all instances of the Cube class, including the myCube instance we created earlier, can add any property or method, regardless of whether that property or method was originally defined in the Cube class file..
- SharedObject—are dynamic classes, which is why you can dynamically add properties and methods to instances of these classes.
- therefore, an instance of our custom Cube class can be created on the main timeline, or any movie clip's timeline.

Xem thử không khả dụng, vui lòng xem tại trang nguồn
hoặc xem Tóm tắt