Monday, December 1, 2008

how to write code basics

There are various characteristics of the movie clip that you can modify. For example, when it's selected there are values you can change in the Property inspector, like its x coordinate, or its width, or various color adjustments like changing its alpha (transparency), or applying a drop-shadow filter to it. Other Flash tools let you make more changes, like using the Free Transform tool to rotate the rectangle. All of these things that you can do to modify a movie clip symbol in the Flash authoring environment are also things you can do in ActionScript by changing the pieces of data that are all put together into a single bundle called a MovieClip object.
In ActionScript object-oriented programming, there are three types of characteristics that any class can include:
• Properties
• Methods
• Events

Properties
A property represents one of the pieces of data that are bundled together in an object. A song object might have properties named artist and title; the MovieClip class has properties like rotation, x, width, and alpha. You work with properties like individual variables--in fact, you might think of properties as simply the "child" variables contained in an object.
Here are some examples of ActionScript code that uses properties. This line of code moves the MovieClip named square to the x coordinate 100 pixels:
square.x = 100;
This code uses the rotation property to make the square MovieClip rotate to match the rotation of the triangle MovieClip:
square.rotation = triangle.rotation;
This code alters the horizontal scale of the square MovieClip so that it's one-and-a-half times wider than it used to be:
square.scaleX = 1.5;
Notice the common structure: you use a variable (square, triangle) as the name of the object, followed by a period (.) and then the name of the property (x, rotation, scaleX). The period, known as the dot operator, is used to indicate that you're accessing one of the child elements of an object. The whole structure together, "variable name-dot-property name," is used like a single variable, as a name for a single value in the computer's memory.

Methods
A method is an action that can be performed by an object. For example, if you've made a movie clip symbol in Flash with several keyframes and animation on its timeline, that movie clip can play, or stop, or be instructed to move the playhead to a particular frame.
This code instructs the MovieClip named shortFilm to start playing:
shortFilm.play();
This line makes the MovieClip named shortFilm stop playing (the playhead stops in place, like pausing a video):
shortFilm.stop();
This code makes a MovieClip named shortFilm move its playhead to Frame 1 and stop playing (like rewinding a video):
shortFilm.gotoAndStop(1);

Events
We've described a computer program as a series of instructions that the computer carries out step-by-step. Some simple computer programs consist of nothing more than that--a few steps which the computer carries out, at which point the program ends. However, ActionScript programs are designed to keep running, waiting for user input or other things to happen. Events are the mechanism that determines which instructions the computer carries out and when.
In essence, events are things that happen that ActionScript is aware of and can respond to. Many events are related to user interaction--like a user clicking a button, or pressing a key on the keyboard--but there are also other types of events. For example, if you use ActionScript to load an external image, there is an event that can let you know when the image has finished loading. In essence, when an ActionScript program is running, Adobe Flash Player just sits and waits for certain things to happen, and when those things happen, it runs the specific ActionScript code that you've specified for those events.

0 comments: