Filed under Flash!

Flash Annoyances 1

Scoping Problem in External SWFs

_root won’t work in scenarios when you’re dealing with external SWF files. If you do use loadMovie( ), MovieClip.loadMovie( ), or the MovieClipLoader class, _root will always point to _level0. If you choose to load by levels; loadMovieNum(“url”,depth) instead, _root will point to the current level that is active.

An example of a safer approach would be

_level0.content_mc.nestedClip._x ;

However, if you try to access your external movieClip right after your MovieClipLoader.loadClip( ) statement, _x in this case, returns a nasty undefined value. Loads are asynchronous. To solve this, do a % progress check, pretty much like what you would do for a preloader.

listener.onLoadInit = function( mc:MovieClip )
{
trace( mc.property );
}

The ComboBox Component Problem

1. When coding with ActionScript, drag the compiled combobox component to your current file’s library (F11), just to get it inside your working file’s Library.

2. Drop-down menus that works in a blank file, and yet doesn’t seem to work on complex files filled with assets and/or background images; the reason being that the drop-down ‘layer’ is at the backmost layer, any objects in front will hide it from view. To solve this problem, do a this._lockroot=true.

Using Getters and Setters

class State
{
var population:Number;
function State( )
{
//constructor
}

function setPopulation( population:Number) {    this.population = population +  ( population * 0.5 );    }
function getPopulation( ):Number {    return population;    }

}

Because of the versatility of getters and setters, getting and setting property values directly is considered bad coding practice. Set up and use Getter andSetter methods instead.
Using Implict Get and Set Methods

class State    {
var statePopulation:Number;
function State( ) { //constructor }

function set population( statePopulation:Number)
{
this.statePopulation = statePopulation + ( statePopulation * 0.5 );
}
function get population( ):Number
{
return statePopulation;
}
}

Though it might not be obvious, the names of the setPopulation( ) and getPopulation( ) methods have been changed to set population and get population , respectively. This change converts the methods to what are known as implicit get and set methods. What does this mean? You get the best of both worlds – property values can be set or retrieved within the class file by using functions, but referencing them in a script is as easy as this:

northCarolina.population = 80000000;

OR this   var myVariable:Number = northCarolina.population; 

With this syntax, it seems as if we’re once again referencing the population propertydirectly, but we’re actually calling either the set/ get population method to take care of the state’s population. Notice that we changed the name of the population property to statePopulation. If we hadn’t done this, using the following syntax would result in an error:

northCarolina.population = 80000000;

Flash wouldn’t know whether we were attempting to set the property named population  or invoking the set population method because doing either requires the same syntax. Changing the property name to statePopulation solves this problem.
Note: Using implicit get and set methods offers no technical advantages over using the getter and setter method as described earlier, other than saving a few keystrokes.

Defining Members
Not all class members are created equal. Using special keywords, members can be configured in various ways, allowing you to specify how the member is accessed and the scope of its influence. private, public.

Static Members
If a property is static, it’s created in memory only once. All instances of the classse the same copy of this member. If any instance of the class edits the value of the propert, all instances of the class see the new value.
Methods can be static too, thus providing universal functionality. These methods can be called from any instance to update and return the values of static properties.

An interesting aspect about static methods & properties is that they can be accessed simply by referencing the class name, followed by the name of the property/ method, such as the following:

Sky.setTotalStars(99999999999);
var  numStars:Number = Sky.getTotalStars( );

Note: A static method can change the value of a static property, but a static method cannot change the value of an instance-based (non-static) property.

Understanding Inheritance

A class can gain (inherit) all members from another class. This is called inheritance. The  class that’s gaining the members is called a subclass, and the class from which it inherits is called the superclass.

A class can only extend from 1 other class. Replacing a property value or method of the superclass with a new one in the subclass is caled overriding.

You can explicitly call the constructor of the base superclass like this
super( );

More on superclasses … to be continued

Frequently used scripts

onEnterFrame = function( ) {
//….

if ( blah) delete onEnterFrame;
}

same applies to set/clearInterval ( );

Creating custom classes

A simple class example

class Cube
{
private var length:Number;
private var height:Number;
private var width:Number;

//constructor
function Cube ( length:Number, height:Number, width:Number )
{
this.length = length;
this.height = height;
this.width = width;
}

function getVolume( ) : Number
{
return length * ( height * width);
}

} //end class

//create a Cube object
var myCube = new Cube( 3,3,3);

Now, you can access or set properties using the dot syntax

myCube.length = 3; //set length
myCube.length; //get length
myCube.color = “red”; //hits an error

In the above statement, Flash looks for the property [color] in the class defination. Color property doesn’t exist and hence the compiler returns an error. However, at times when you do need to create new properties or methods that were not defined in the class file. You add the Dynamic modifier to the class defination.

dynamic class Cube {

}

Understanding the Classpath
When compiling a SWF file from a FLA file, Flash checks to see whether you created an instance of a custom class. If a custom class is being used, Flash attempts to load that class to include it inside the SWF file. The directories in which Flash searches for are called the classpath.

The global classpath is the same no matter which FLA file you’re authoring. The document level classpath can be changed for a specific FLA file without affecting the classpaths seen by other FLA files.
Global Classpath
By default, the global classpath points to 2 directories. The first is the diretory which your FLA file resides. The second being the directory that contains the class files and interfaces for Flash’s built-in classes.

Steps to edit the Global classpath
1. Select Edit -> Preferences.
2. Click the ActionScript menu item.
3. Click the ActionScript 2.0 Settings button.

Document level classpath
This is empty bydefault. You can add any number of directories to this classpath to make Flash search for necessary classes while compiling an authoring file to a SWF file. This classpath exists only for the current FLA file.

To edit document level classpath
1. Select File > Publish Settings
2. Select the Flash tab
3.Click settings button next to ActionScript 2.0

Using Packages and Importing Classes
When you use packages, the class file syntax changes slightly and can complicate instantiating an instance of that class. The name declaration of the class must contain the path to the class file from the root classpath directory in which the class path resides.

class testPackage.TestClass
{
function TestClass( ) { … }

}

Note:
testPackage exists in a
classpath directory
Note: It’s considered good coding practice to name packages with all lowercase letters. It’s also becoming an industry standard to name your class structure based on the domain name. Therefore, a class built for macromedia.com would be placed somewhere inside the com.macromedia package. This follows a convention from Java.

Import statements
import testPackage.TestClass;
var myInstance:TestClass = new TestClass( );

Files residing in subpackages/ subfolders will have to be imported separately.While import statement allows you to use the shorthand when referencing class names, they are exclusive to frames; meaning in order for other frames to use the shorthand method of referencing class names, they must include the import statement also.

Color, String & Selection classes

Using the Color class

To use a color class, you must first create an instance using the constructor. It takes in 1 paramater: the movie clip it should modify.

var myCol:Color = new Color( shirt_mc );
myCol.setRGB(0x FF3300);     //Accepts in a hex value

E.g.     btn.onRelease = function ( )
{
var R:Number = random(256);   //generates values between 0 to 255
var G:Number = random(256);
var B:Number = random(256);
var hexString:String = R.toString(16) + G.toString(16) + B.toString(16);
var hexNum:Number = parseInt( hexString, 16 );
var shirtColor:Color = new Color( shirt_mc );
shirtColor.setRGB(hexNum);
}

Convert variables R,G and B to base 16 values using toString(16)
Convert to hex number from the existing String above, using parseInt( ) function.

Working with String and Selection class
Note that the value of a text field instance is considered an instance of the String class, nameOfTextField.text.

indexOf( ) lets youfind the first occurance of a character group or characters in a String. The result returned is a number corresponding to the letter index where the String starts. If it finds no occurances in the line, it returns a value of -1.

length (property)     E.g. zipCode_txt.text.length

Selection Class
The Selection class allows you to control various aspects of the currently focused text field, including highlighting text, getting and setting the caret’s (current insertion point) position, and more.

You can use the setFocus( ) method to override the user’s current cursor, which is important because you can only use other Selection class methods on the textfield currently in focus.

Selection.setSelection(param1, param2)    //Highlights from pt1 to pt 2
Parameters uses character index.
Selection.setFocus(“txtField”);
Selection.setSelection(11, 14);

Follow

Get every new post delivered to your Inbox.