Flylib.com

Books Software

 
 
 

Troubleshooting


Troubleshooting

Why doesn't my line show up at run-time?

Make sure that you define the lineStyle before starting your drawing. Until you define a linestyle, it is undefined and the subsequent lines will not be drawn.

Why is my symbol moving in the wrong direction?

Check the sign of the coordinates you're using. The Flash stage coordinate system has the y-axes positive in the downward direction. A negative _y is located above the stage, and a negative _x is located to the left of the stage.

Why is my fill color not working?

Check to make sure you closed the shape by returning to your starting point when drawing the lines of your shape.

Why does my line start from the upper-left corner?

Make sure to start with a moveTo() statement before using lineTo() or curveTo() . The starting point of a line or curve defaults to (0,0) if you don't use moveTo() to set another starting point.

Why is my gradientFill not showing up?

Make sure you have all parameters of beginGradientFill() set properly. Any missing properties of the matrix object can cause the method to not function. Check to make sure that you have the same number of elements in the colors, alphas , and ratios arrays. Make sure that you have set the fillType parameter to linear or radial .



Best PracticesWhen to Use with

Normally it's best to avoid the with statement. However, it can help make your code more readable when you use the drawing API. For example, consider the following:

this.attachMovie("myBox", "box", 1);
myBox.lineStyle(1, 0x669999, 100);
myBox.beginFill(0x336666, 80);
myBox.moveTo(0,0);
myBox.lineTo(100,0);
myBox.lineTo(100,100);
myBox.lineTo(0,100);
myBox.lineTo(0,0);
myBox.endFill();


Contrast that with the following:

this.attachMovie("myBox", "box", 1);
with (myBox){
    lineStyle(1, 0x669999, 100);
    beginFill(0x336666, 80);
    moveTo(0,0);
    lineTo(100,0);
    lineTo(100,100);
    lineTo(0,100);
    lineTo(0,0);
    endFill();
}


Another option for making this code more readable is to use a local variable (a variable whose scope is available only in the parent timeline) to refer to the new movie clip. Consider the following example:

var b:MovieClip = this.attachMovie("myBox", "box", 1);
b.lineStyle(1, 0x669999, 100);
b.beginFill(0x336666, 80);
b.moveTo(0,0);
b.lineTo(100,0);
b.lineTo(100,100);
b.lineTo(0,100);
b.lineTo(0,0);
b.endFill();




Chapter 13. Introduction To Class-Based Programming in Flash

In this chapter

Brief Introduction to OOP 268

Using the Built-In Classes in Flash 269

Creating Your Own Custom Classes 270

Static, Public, and Private Properties and Methods 277

Superclasses and Subclasses 277

Advanced Event Handling 279

Troubleshooting 281

Best PracticesOrganizing Your Code 281



Brief Introduction to OOP

Object-oriented programming (OOP) is sometimes shrouded in mystery and mysticism. You might have encountered phrases such as "the zen of OOP." But when you get right down to it, OOP is just a way of organizing code. I'd even go so far as to say that it's the best way to organize complex or large projects.

Chapter 11, "Introducing ActionScript," covered simple datatypes in Flash. Variables are containers, and datatypes are a way to say what kind of data can be stored in that container. A variable with a number datatype holds a number, a string holds a string of characters , and an array holds a list of strings, numbers , or objects.

Each datatype has certain properties and methods unique to its type. For example, a number can hold numerical values and have mathematical methods applied to it. A class defines a datatype.

When we start discussing objects, things start getting more interesting. An object can hold properties, which are simply variables of a datatype (the object container holds the variable container). Objects can also hold another complex datatype called functions . In Chapter 11, functions were defined as encapsulated segments of code with a single set of functionality. They are "run" when called by name in another section of code. When functions are stored in an object, they are called methods .

Classes

So how do you organize code differently when you work with ActionScript 2.0 and object-oriented programming in Flash? When Chapter 11 introduced ActionScripting, you learned to look at variables and functions as ways to organize your code. Now you're ready to move to a more sophisticated organizational structure where all of your code is stored in objects, which are simply containers of containers.

However, before you can create an instance of an object, you must have a class. A class describes an object as well as what properties and methods belong to that object. It can be one of the built-in classes, such as Math, or a custom class that you write yourself. In either case, a class is a datatype that describes all the properties and methods that are available to be applied to an object created from the class description.

You can think of a class as the blueprint for an object. Just as many buildings can be constructed from the same set of blueprints, multiple objects can be described by a single class. It is important to understand that all datatypes and objects in Flash are class-based. The MovieClip object, Date object, Array object, Math object, and even the Object object are all based on classes that are built into Flash. For a complete list of all built-in datatypes (classes) available to you in Flash, go to the Action panel and choose Types from the left Action listing.