[ Pobierz całość w formacie PDF ]
.For more information about constructor functions,see Constructor functions on page 163.Note: If you don t create a constructor function, an empty one is created automaticallyduring compilation.8 Save the file as Person.as in the PersonFiles directory that you created in step 1.If you re using Flash MX 2004 (not Flash Professional), proceed to the next section.9 (Flash Professional only) Check the syntax of the class file by selecting Tools > Check Syntax,or pressing Control+T (Windows) or Command+T (Macintosh).If any errors are reported in the Output panel, compare the code in your script to the finalcode in step 7, above.If you can t fix the code errors, copy the completed code in step 7 fromthe Help panel.Using classes: a simple example 159Creating an instance of the Person classThe next step is to create an instance of the Person class in another script, such as a frame script ina Flash (FLA) document or another AS script, and assign it to a variable.To create an instance ofa custom class, you use the new operator, just as you would when creating an instance of a built-inActionScript class (such as the XML or TextField class).For example, the following code creates an instance of the Person class and assigns it to thevariable newPerson.var newPerson:Person = new Person("Nate", 32);This code invokes the Person class s constructor function, passing as parameters the values "Nate"and 32.The newPerson variable is typed as a Person object.Typing your objects in this way enables thecompiler to ensure that you don t try to access properties or methods that aren t defined in theclass.(The exception is if you declare the class to be dynamic using the dynamic keyword.See Creating dynamic classes on page 173.)To create an instance of the Person class in a Flash document:1 In Flash, select File > New, select Flash Document from the list of document types, andclick OK.2 Save the file as createPerson.fla in the PersonFiles directory you created previously.3 Select Layer 1 in the Timeline and open the Actions panel (Window > DevelopmentPanels > Actions).4 In the Actions panel, enter the following code:var person_1:Person = new Person("Nate", 32);var person_2:Person = new Person("Jane", 28);trace(person_1.showInfo());trace(person_2.showInfo());The above code creates two instances of the Person class, person_1 and person_2, and thencalls the showInfo() method on each instance.5 Save your work, then select Control > Test Movie.You should see the following in theOutput panel:Hello, my name is Nate and I'm 32 years old.Hello, my name is Jane and I'm 28 years old.When you create an instance of a class by calling its constructor function, Flash looks for anActionScript file of the same name as the constructor in a set of predetermined directorylocations.This group of directory locations is known collectively as the classpath (see Understanding the classpath on page 169).You should now have an overall idea of how to create and use classes in your Flash documents.The rest of this chapter explores classes and interfaces in more detail.160 Chapter 9: Creating Classes with ActionScript 2.0Creating and using classesAs discussed previously, a class consists of two parts: the declaration and the body.The classdeclaration consists minimally of the class statement, followed by an identifier for the classname, then left and right curly braces.Everything inside the braces is the class body.class className {// class body}You can define classes only in ActionScript (AS) files.For example, you can t define a class on aframe script in a FLA file.Also, the specified class name must match the name of the AS file thatcontains it.For example, if you create a class called Shape, the AS file that contains the classdefinition must be named Shape.as.// In file Shape.asclass Shape {// Shape class body}All AS class files that you create must be saved in one of the designated classpath directoriesdirectories where Flash looks for class definitions when compiling scripts.(See Understandingthe classpath on page 169.)Class names must be identifiers; that is the first character must be a letter, underscore (_), ordollar sign ($), and each subsequent character must be a letter, number, underscore, or dollar sign.Also, the class name must be fully qualified within the file in which it is declared; that is, it mustreflect the directory in which it is stored.For example, to create a class named RequiredClass thatis stored in the myClasses/education/curriculum directory, you must declare the class in theRequiredClass.as file like this:class myClasses.education.curriculum.RequiredClass {}For this reason, it s good practice to plan your directory structure before you begin creatingclasses.Otherwise, if you decide to move class files after you create them, you will have to modifythe class declaration statements to reflect their new location [ Pobierz całość w formacie PDF ]
zanotowane.pl doc.pisz.pl pdf.pisz.pl odbijak.htw.pl
.For more information about constructor functions,see Constructor functions on page 163.Note: If you don t create a constructor function, an empty one is created automaticallyduring compilation.8 Save the file as Person.as in the PersonFiles directory that you created in step 1.If you re using Flash MX 2004 (not Flash Professional), proceed to the next section.9 (Flash Professional only) Check the syntax of the class file by selecting Tools > Check Syntax,or pressing Control+T (Windows) or Command+T (Macintosh).If any errors are reported in the Output panel, compare the code in your script to the finalcode in step 7, above.If you can t fix the code errors, copy the completed code in step 7 fromthe Help panel.Using classes: a simple example 159Creating an instance of the Person classThe next step is to create an instance of the Person class in another script, such as a frame script ina Flash (FLA) document or another AS script, and assign it to a variable.To create an instance ofa custom class, you use the new operator, just as you would when creating an instance of a built-inActionScript class (such as the XML or TextField class).For example, the following code creates an instance of the Person class and assigns it to thevariable newPerson.var newPerson:Person = new Person("Nate", 32);This code invokes the Person class s constructor function, passing as parameters the values "Nate"and 32.The newPerson variable is typed as a Person object.Typing your objects in this way enables thecompiler to ensure that you don t try to access properties or methods that aren t defined in theclass.(The exception is if you declare the class to be dynamic using the dynamic keyword.See Creating dynamic classes on page 173.)To create an instance of the Person class in a Flash document:1 In Flash, select File > New, select Flash Document from the list of document types, andclick OK.2 Save the file as createPerson.fla in the PersonFiles directory you created previously.3 Select Layer 1 in the Timeline and open the Actions panel (Window > DevelopmentPanels > Actions).4 In the Actions panel, enter the following code:var person_1:Person = new Person("Nate", 32);var person_2:Person = new Person("Jane", 28);trace(person_1.showInfo());trace(person_2.showInfo());The above code creates two instances of the Person class, person_1 and person_2, and thencalls the showInfo() method on each instance.5 Save your work, then select Control > Test Movie.You should see the following in theOutput panel:Hello, my name is Nate and I'm 32 years old.Hello, my name is Jane and I'm 28 years old.When you create an instance of a class by calling its constructor function, Flash looks for anActionScript file of the same name as the constructor in a set of predetermined directorylocations.This group of directory locations is known collectively as the classpath (see Understanding the classpath on page 169).You should now have an overall idea of how to create and use classes in your Flash documents.The rest of this chapter explores classes and interfaces in more detail.160 Chapter 9: Creating Classes with ActionScript 2.0Creating and using classesAs discussed previously, a class consists of two parts: the declaration and the body.The classdeclaration consists minimally of the class statement, followed by an identifier for the classname, then left and right curly braces.Everything inside the braces is the class body.class className {// class body}You can define classes only in ActionScript (AS) files.For example, you can t define a class on aframe script in a FLA file.Also, the specified class name must match the name of the AS file thatcontains it.For example, if you create a class called Shape, the AS file that contains the classdefinition must be named Shape.as.// In file Shape.asclass Shape {// Shape class body}All AS class files that you create must be saved in one of the designated classpath directoriesdirectories where Flash looks for class definitions when compiling scripts.(See Understandingthe classpath on page 169.)Class names must be identifiers; that is the first character must be a letter, underscore (_), ordollar sign ($), and each subsequent character must be a letter, number, underscore, or dollar sign.Also, the class name must be fully qualified within the file in which it is declared; that is, it mustreflect the directory in which it is stored.For example, to create a class named RequiredClass thatis stored in the myClasses/education/curriculum directory, you must declare the class in theRequiredClass.as file like this:class myClasses.education.curriculum.RequiredClass {}For this reason, it s good practice to plan your directory structure before you begin creatingclasses.Otherwise, if you decide to move class files after you create them, you will have to modifythe class declaration statements to reflect their new location [ Pobierz całość w formacie PDF ]