Troubleshooting

     

Why can't I reference a variable on the Main Timeline from a subclip?

graphics/troubleshooting_icon.jpg

If you store variables in the Main Timeline and access them from subclips , don't forget to use _root when referencing the variables. Here's a common error: You look at the variable where it is declared in the Main Timeline and don't see _root there, so you forget to include _root when you reference the variable.


The operands are not equal, so why do they test equal?

One of the most common mistakes in ActionScript is using the assignment operator ( = ) when you should use the equality operator ( == ). The assignment operator sets a variable, array element, or object property equal to a value. The equality operator tests whether two expressions are equal. Your familiarity with standard arithmetic notation works against you here. Too often, you write if (x = 10) , a statement that the ActionScript interpreter sees as always true, when you mean if (x == 10) , a statement that is true only when x equals 10 .

Why is my switch statement failing to execute any case statement?

Remember, switch comparisons use strict equality. Are you sure your test value and case value are of the same datatype? If you're using a variable as a test value, an error may have caused your test value to be undefined . As a debugging measure, use the typeof operator to confirm the datatype of your test value just prior to the switch statement.

Why can't I access a variable or function on the same timeline?

If the playhead has never once entered the frame containing a particular variable or function, the ActionScript interpreter doesn't know about that variable or function.

Say you put this event handler on frame 1:

 _root.onEnterFrame = function() {      trace(x); }; 

You also put the statement x = 10; on frame 2 of the same timeline. In that case, the trace(x) statement in the event handler will display undefined the first time it executes (in the first frame) because x hasn't been defined yet. After the interpreter "sees" x (that is, after the playhead enters the second frame), it will always "remember" it, and the trace(x) statement in the event handler will display "10" upon entering all frames , even frame 1, from then on.

Note that all functions in a frame become known to the ActionScript interpreter as soon as it enters the frame. Thus, you can execute a function before you declare it, within a single frame:

 test(); // displays "hi" function test() {      trace("hi"); } 

Why am I getting an undefined return?

Functions return undefined when there is no return statement, when there is a return statement without a return value, or when you return a variable that hasn't been initialized . Check for one of these situations. In both of the following cases, for instance, what you want is return result; .

No return statement:

 function doubleIt(numberToBeDoubled) {      result = numberToBeDoubled * 2; } 

No return value:

 function doubleIt(numberToBeDoubled) {      result = numberToBeDoubled * 2;      return; } 

Part of my function never runs! Why?

If part of your function never seems to run, perhaps a return statement is getting in the way. Here's a simple example of the problem:

 1: function showRoomNumber(floor, num) { 2:      if (num < 0) 3:           err = "negative numbers not supported"; 4:           return(err); 5:      return ("Your room number is "+floor+num); // this line never executes 6: } 

The problem is that there are no curly braces around the two statements that are intended to be the error return (lines 3 and 4). Thus, line 4 always executes, causing the function to return. Line 3 is skipped if num is greater than or equal to . If line 3 doesn't execute, err isn't defined, and line 4 returns undefined . Meanwhile, line 5, which would return the room number, never executes at all.

The function should look like this:

 function showRoomNumber(floor, num) {      if (num < 0) {           err = "negative numbers not supported";           return(err)      }      return ("Your room number is "+floor+num); } 

I'm pressing Tab to change focus, but nothing is happening.

This is standard behavior in the authoring environment. Try testing your SWF in the browser and in the Flash Player.

Why can't I access information in my loaded SWFs?

You should be able to attach a movie from the Library of a loaded SWF, and access variables, movie clips, and functions defined in the loaded SWF.

If you cannot, you might not be referring to the items correctly. To view the paths and identifiers associated with the items, do the following:

  • Select Debug, List Variables to list variables.

  • Select Debug, List Objects to list objects.

If you're referring to the items correctly, you might be trying to access them before the SWF has loaded completely. Ideally, use MovieClipLoader to avoid this. If you need FP6 compatibility, try to make sure the file has loaded completely by doing one of the following:

  • Put in an adequate delay. For instance, you could put the code that accesses the items a number of frames after the code that loads the SWF. This assumes some level of predictability in load time, however.

  • Use the data event of the loadMovie() function in combination with the getBytesLoaded() and getBytesTotal() methods of the MovieClip class, the _framesloaded and _totalframes movie clip properties, or the deprecated ifFrameLoaded() function.

I'm using the "point-check" hitTest() approach to check whether a movie clip overlaps the registration point of a movie I created using createEmptyMovieClip(). Why isn't it working?

When you create movie clips manually, it is natural to create the graphics around the registration point and then position the movie clip on the Stage. Thus, you may become accustomed to the idea that the registration point is at the center of the graphics.

When using createEmptyMovieClip() , you use the drawing API to create the graphics, and you can create and position the graphics in one step without changing the registration point. For instance, using the makeTriangle() function in Helen Triolo's dynamicButtonMovie.fla sample movie, you can create and position the triangle in one line without changing the registration point. If you change the registration point, three lines are required to achieve the same visual effect. For instance, in the following listing, the result looks the same whether you use lines 12 through 14 or just line 15:

 1: function makeTriangle(x1, y1, x2, y2, x3, y3, zdepth) {  2:     this.createEmptyMovieClip ("triangle", zdepth);  3:     with (triangle){  4:             beginFill (0x0000FF, 50);  5:             moveTo (x1, y1);  6:             lineTo (x2, y2);  7:             lineTo (x3, y3);  8:             lineTo (x1, y1);  9:             endFill(); 10:     } 11: } 12: /* makeTriangle(0, 0, 60, 130, -50, 1); 13: triangle._x = 200; 14: triangle._y = 200; */ 15: makeTriangle(200, 200, 260, 330, 250, 1); 

You can easily forget, when using the more efficient syntax, that the registration point is nowhere near the graphic. Then, if you do something where the registration point is important, you may get surprising results. For instance, the following hit test tells you when myClip overlaps triangle 's registration point; however, myClip will be nowhere near the triangle graphic at that time:

 makeTriangle(200, 200, 260, 330, 250, 1); myClip.hitTest(triangle._x, triangle._y); 



Using Macromedia Studio MX 2004
Special Edition Using Macromedia Studio MX 2004
ISBN: 0789730421
EAN: 2147483647
Year: N/A
Pages: 339

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net