Flash Action Script 3.0

variable declaration

Defining your own variables is easy. Let’s take a look at a typical variable declaration and stepthrough its different parts in the list that follows:

* var food : String;

food = “pizza”;

* var: All variables are defined by using the var keyword followed by the name of the

* variable. This is different from previous versions of ActionScript where the var keyword
was optional.
* food: This is the name of the variable. This can be any word or string of letters and numbers beginning with either a letter or an underscore. By convention, we recommend naming your variables using descriptive words in camel case and starting with a lowercaseletter.

For example, mainCourse = “pizza”;.

* String: This defines the data type or the type of information that the variable can hold. The word String could be replaced with any class or interface name. We’ll talk more about data types later in this chapter.

* food = “pizza”;: This statement sets the value of food to the word “pizza” using the = (assignment) operator. The quotes around the word pizza signify that this is text rather than a variable called pizza.

Variables can also be declared and assigned a value in a single line:

var drink:String = “Root beer”;

You can even declare several variables on a single line:

var breakfast:String, lunch:String, dinner:String;

After a variable is defined, it can be used to replace the use of its value. For example:

trace(“My favorite food is “ + food);

// displays “My favorite food is pizza”

Using Access Controls

This is called the access control attribute. It indicates whether the variable or function will be available to classes other than the one in which the object is defined. We’ll talk more about classes and what
all of this means later on.

Four keywords are defined in ActionScript 3.0 for defining access. They are:

# public: This indicates that a variable or method is available to all classes that attempt toaccess it. That is, any code can access a variable or call a method if it is marked with this keyword.

# private: Private variables and methods can be accessed only by the class in which they are defined. Attempting to access these methods or variables anywhere outside of the class where they are defined will result in a compile-time error. This also includes child classes. Unlike ActionScript 2.0, child classes no longer have access to private members. Instead you should use the protected keyword, which we discuss in a moment.

# internal: Internal objects are neither public nor private. They are available only to classes within the same class package as the class where the object is defined. That is, any class that has the same package declaration (e.g., package com.wiley.bible {…}) will be able to call the method or access the variable.

This is the default access control namespace for all objects in ActionScript 3.0.

# protected: Protected is similar to internal in that it limits access to certain other classes; however, protected classes can be accessed only by classes that extend the functionality of the class (subclasses).

A classic example of OOP structure, and specifically inheritance, defines a set of transportation vehicles. You might start with a generic Vehicle class that includes traits common to all vehicles , such as the basic physics of movement. You might then create three subclasses: GroundVehicle, WaterVehicle, and AirVehicle. These classes would alter or introduce traits specific to ground, water, and air travel, respectively, but not yet be complete enough to represent an actual vehicle. Further derived classes might be Car and Motorcycle (descending from GroundVehicle ) , Boat, and Submarine (descending from WaterVehicle), and Plane and Helicopter (descending from AirVehicle). Depending on the complexity of your system, you can carry on this process, creating individual models with individual settings for fuel consumption, friction, and so on.

package {

import flash.display.MovieClip;

public class Main extends MovieClip {

public function Main() {

trace(“Flash”);

}

}

The first line, along with the closing brace in line 12, defines the class’s package. A package is a mandatory structure that ensures your class is known to the compiler. Next, you must import any classes that you need to use in your package.

A document class essentially serves as a shortcut for creating an instance of a movie clip or sprite (a new Flash object that is nothing more than a oneframe movie clip) and adding it to the display list so it can be displayed by Flash Player. (This is true even when there is nothing to display, as in this case. We will cover manipulating the display list in Chapter 4.)

All document classes must be derived from either the MovieClip or Sprite class. (Other custom classes that are not document classes do not need to be extended from MovieClip or Sprite if that is not appropriate.) This example uses MovieClip so you must import the MovieClip class, as seen in line 3.

Line 5, along with its closing brace on line 11, is the class definition. Its name is arbitrary but, when naming it, you should follow a few basic rules and conventions. The name should be one word that does not already exist in ActionScript, it should start with an alpha character (rather than a number or other character), and it is typically capitalized. The class must be public,
meaning that other classes can access the constructor, and it must extend MovieClip or Sprite, as described previously.

Line 7, along with its closing brace on line 9, is the class constructor. This is the main function that automatically runs when creating an instance of this class. It, too, must be public and must have the same name as the class. Other functions (if any) can, and must, have unique names. All that remains is to add the lone method required in this case. The constructor must trace “Flash”
to the Output panel, so add the following to line 8:

public function Main() {

trace(“Flash”);

}

Once finished, you must save the file in the same directory as your FLA file for now. (Later on, you’ll learn how to place your class files in other locations.) You must give the file the same name as the class, but add an .as extension.

****************************************************************************

Tutorial Collect From www.republicofcode.com/tutorials/flash/ Special Thanks Blue_Chi

AS3: Changing Colors

By Blue_Chi | Flash CS4 | ActionScript 3.0 | Beginner

This tutorial will teach you how to change the color of any visible object by using the ColorTransform class. Using this class you will be able to apply a single color tint to any object at any time. The example below uses this class to change the color of the object when the corresponding button is pressed.

Content on this page requires a newer version of Adobe Flash Player.

Get Adobe Flash player
Changing Colors

Unlike the majority of other simple properties such as alpha, x, y, width, and height, the color of an object cannot be directly changed on the object itself. Instead you must create a new instance of the ColorTransform class, any color transformation desired are to be applied to this instance, and then this instance is used to overwrite the actual ColorTransform property attached to the target object. This property itself is a sub property of the Transform property of the Display objects such as Shapes, Sprites, and MovieClips.

This process can be carried out through the generalized code shown below, where myTargetObject is a reference to the object on the stage which we would like to change the color of and 0xFFFFFF is the color we want to apply.

var myColorTransform = new ColorTransform();
myColorTransform.color = 0xFFFFFF;
myTargetObject.transform.colorTransform = myColorTransform;

This is a very simplified use of the colorTransform object, we are going to have a practical example next.
Practical Code

We are going to create a small example where we draw a square and then change its color using the ColorTransform object.

Start off by creating a new Flash movie in AS3 format. Right-click the only frame you have on your timeline and select Actions to open up the Actions Panel. All of our project will be written in code and no objects will be created manually on stage.

The first step in our tutorial is to draw the square which we will be changing the color of later. To do this we are going to use the graphics class, our square will be named my_square, we will draw it using the drawRect() method, and then we will make it visible on the stage by using the addChild() method.

var my_square:Shape = new Shape();
my_square.graphics.beginFill(0x000000,1);
my_square.graphics.drawRect(10,10,100,100);
addChild(my_square);

You can learn more about Drawing Vectors and Display List by reviewing our tutorials on these topics.

You can test your movie at this stage by going through Control>Test Movie to see that you have drawn the square on the stage.

AS3 – Drawing a Square

To change the color of this object we need to create a ColorTransform instance. To do this we used the var keyword along with our desired named for the instance:

var my_square:Shape = new Shape();
my_square.graphics.beginFill(0x000000,1);
my_square.graphics.drawRect(10,10,100,100);
addChild(my_square);

var my_color:ColorTransform = new ColorTransform();

We can now change a number of attributes of this ColorTransform instance, the easiest way for changing the color of the object is to use the color property which accepts color values in RGB format. If we want to color our square in red we can use the RGB code 0xFF0000:

var my_square:Shape = new Shape();
my_square.graphics.beginFill(0x000000,1);
my_square.graphics.drawRect(10,10,100,100);
addChild(my_square);

var my_color:ColorTransform = new ColorTransform();
my_color.color = 0xFF0000;

Our my_color object is now configured, we need to apply it to my_square to have the color transformed:

var my_square:Shape = new Shape();
my_square.graphics.beginFill(0x000000,1);
my_square.graphics.drawRect(10,10,100,100);
addChild(my_square);

var my_color:ColorTransform = new ColorTransform();
my_color.color = 0xFF0000;
my_square.transform.colorTransform = my_color;

Remember that the ColorTransform property is a sub-property of the Transform property.

That should do it. You can test your movie now to see your square colored in red.

AS3 – ColorTransform – Red

This method could be used to change the color of any object. All you have to do is simply change the RGB code in your color property and then apply your ColorTransform property to your object.
Other Properties of the ColorTransform Class

In addition to the possibility of setting a color directly as an RGB code, you can also configure the following properties:

* alphaMultiplier – Used to configure the transparency of the object – Accepts a decimal value between 0 and 1.
* alphaOffset – Used to affect existing transparency of the object – Default value is zero, but can go up to 255 or down to -255.
* blueMultiplier – Used to configure the blue channel value of the color – Accepts a decimal value between 0 and 1.
* blueOffset – Used to affect existing blue channel value of the color – Default value is zero, but can go up to 255 or down to -255.
* greenMultiplier – Used to configure the green channel value of the color – Accepts a decimal value between 0 and 1.
* greenOffset – Used to affect existing green channel value of the color – Default value is zero, but can go up to 255 or down to -255.
* redMultiplier – Used to configure the red channel value of the color – Accepts a decimal value between 0 and 1.
* redOffset – Used to affect existing red channel value of the color – Default value is zero, but can go up to 255 or down to -255.

It is possible to use these properties together to apply the specific color your need. Here is an example on how they could be used:

var my_square:Shape = new Shape();
my_square.graphics.beginFill(0x000000,1);
my_square.graphics.drawRect(10,10,100,100);
addChild(my_square);

var my_color:ColorTransform = new ColorTransform();
my_color.alphaMultiplier = 0.5;
my_color.blueOffset = 150;
my_color.greenOffset = 100;
my_color.redOffset = -50;
my_square.transform.colorTransform = my_color;

Retrieving Existing ColorTransform Property

At times you might want to retrieve the existing ColorTransform Property of an object and then update that property instead of creating a new one. To do this you simply create a new variable and store a reference for your existing ColorTransform there:

var my_square:Shape = new Shape();
my_square.graphics.beginFill(0x000000,1);
my_square.graphics.drawRect(10,10,100,100);
addChild(my_square);

var my_color:ColorTransform = my_square.transform.colorTransform;

We can then modify some values or change a specific color channel without affecting the rest:

var my_square:Shape = new Shape();
my_square.graphics.beginFill(0x000000,1);
my_square.graphics.drawRect(10,10,100,100);
addChild(my_square);

var my_color:ColorTransform = my_square.transform.colorTransform;
my_color.redOffset += 100;

Finally, we apply the property the same way we have done in the past:

var my_square:Shape = new Shape();
my_square.graphics.beginFill(0x000000,1);
my_square.graphics.drawRect(10,10,100,100);
addChild(my_square);

var my_color:ColorTransform = my_square.transform.colorTransform;
my_color.redOffset += 100;
my_square.transform.colorTransform = my_color;

The same technique could be used to modify all other properties as well.

This concludes our tutorial. I hope that you learnt something new from it. If you have any questions or comments feel free to post them at the Republic of Code Forum.

– End of Tutorial

****************************************************************************

AS3 Tutorial Tutorial

Menu Effect in Actionscript 3

Actionscript 3 Tutorial

Actionscript 3 Tutorial

Advanced Preloader with Reflection