Download presentation
Presentation is loading. Please wait.
Published byTine Petersen Modified over 6 years ago
1
Programming games Classes and objects (used for Jigsaw, Bouncing stuff, other projects) Homework: Complete cannonball. Video or Audio. Your own project.
2
General motivation Writing code is hard!
It is nice if there is a systematic way to re-use code that works! Languages have various features to do this. For a single application: One way is to write a function that can be called (invoked) from multiple places. the function's parameters can be used for information For one or more applications, other ways involve setting up multiple files.
3
Objects: Built-in The MovieClip symbols that you create in the Library and move instances of to the Stage are objects. Button component is an object recall the label attribute/property Strings of characters are objects. Timers are objects. These objects have attributes (aka properties, data) and have methods (code) target.gotoAndPlay(2);
4
Programmer defined objects
You/programmers can define your own objects. You do this by coding a Class definition. This is done in a separate file, file extension .as for ActionScript. Class definitions define the data (properties=data=attributes) and the behavior (code=methods=functions) for the objects.
5
Examples Several examples on my page, including bouncing things jigsaw
shooter state capitals quiz
6
Bouncing things Looking at the program, there must be opportunity for common (shared) code in the stars, rectangles, circles But there are differences. Flash ActionScript (and many other languages) have feature of building class definitions based on other class definitions to do this.
7
NOTE In the past, students were assigned the Jigsaw application.
This used programmer defined classes, using external .as files. A package called jigsaw containing one class definition for Piece. Folder named jigsaw, file in the folder called Piece.as This gives you preview/preparation for larger ActionScript projects You may do [a] jigsaw for your final project. You may do bouncing stuff ?
8
document class You can put code in an [external] .as file without a package name. This is called the Document class. You make a pointer to this in the .fla file. AND/OR You put code in one or more .as files and put them all in a folder. The name of a folder is the package name. Suppose it is jigsaw. In the .fla file, you put in a statement import jigsaw.*;
9
Package, class, object A Class definition defines objects of the class
Data (attributes, properties) Code (methods) A Class file [for example, Piece] is contained in a package [for example, jigsaw] NOTE: the jigsaw package only has one class (one file) Note: the convention is to make the name of a class start with a capital letter.
10
constants The modifier const indicates that something will not be changed once it is initialized internal static const ROTUNIT=15; The internal means it is only used by code in the package. The static means just one copy exists, not one for each Piece I regret this use of the word STATIC since it does not mean that it cannot change which is the meaning in STATIC text fields as well as the English meaning of term. I'll repeat this in later slide. The const means it will not be changed It is convention (considered a good practice) to give constants a name in capital letters.
11
Declarations The var and const terms are used to define variables and constants Associate names with values The term after the : indicates the datatype. internal var offsetx:Number = 0; internal var mclip:MovieClip;
12
Class data & methods (repeat)
A Class definition also can include variables for the whole class and methods for the whole class The term static is used to indicate a class variable or method. THESE CAN CHANGE. The static means there is just one of them. YES, THIS ISN'T A GOOD TERM. Example: Math has the constant PI and Math has the methods: random and floor Example: Piece has variables pieces, dragging, and methods: buildit, mixup, and checkit.
13
Constructor method The constructor method is the one with name the same name as the Class. It is called when the term new is used. It builds (constructs) the object.
14
The pieces array Jigsaw keeps an array of all the Piece objects created. In the Piece method (this is the thing being constructed), there is a statement Piece.pieces.push(this); The push adds the piece being built (indicated by the built-in term this) to the single pieces array. One array 'holds' references to all the jigsaw pieces.
15
The import statement The .fla file has an import statement to bring in the jigsaw package. import jigsaw.*; Again, there is just one class in jigsaw. The Piece.as file has import statements to bring in the necessary parts of ActionScript. import flash.display.*; import flash.events.*; import flash.ui.Keyboard;
16
About import The import statement means you can use a shorter form of name for these built-in elements. If you forget to include it, the error statements are pretty suggestive that you left something out. Go to help and see what package is required.
17
Modifiers In ActionScript, you can specify what code can access variables and methods. The modifier public means … any code can. The modifier internal means … only code in the package. The modifier private means … only code in the class.
18
Why not… Make everything public so no restriction? [You can do this…]
Putting in the other modifiers provides an extra level of checking assuming you are thinking things out. The default is internal. If you want a variable or method defined in the class definition in the .as file to be accessed by frame code in the .fla file, then you need to make it public
19
Reminder The real need and power of object-oriented programming in ActionScript, Java, etc., is for Large projects Many programmers HOWEVER, it also serves us for Jigsaw because all the code in the .as file will work for any jigsaw. Your work to make each jigsaw puzzle (getting an image, slicing it up, calculating the offsets) is in the .fla! Put this another way: write one Piece.as file, and use it for any or all jigsaw puzzles! Note: this is all part of ActionScript 3.0
20
Piece.as package jigsaw { import statements public class Piece {
static var statements var statements public function Piece( ) { } internal function startdragging(ev) { } … public static function buildit() { } }
21
Folder location The Piece.as file is located in a folder called jigsaw in a folder called as3 (the last name is mine—you can change it). You must use Publish Settings to set the Classpath. It appears that as3 must be the first folder on a drive AND the drive must also contain the .fla file (though it can be anywhere on that drive). See extra tutorial on this topic.
22
Strong recommendation if you want to do Jigsaw (or bouncing stuff)
Print out the whole tutorial and read away from the computer. Do not [just] copy and paste code Much of the code is repeated and shown out of order Review the coding in outline form and then study the details. Write the code in outline form and then fill in the details.
23
Classwork/homework Work session
Finish cannonball and start video/audio project Confer with me on what you are doing Last project is totally your choice. You can build on sample projects.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.