Mods, if this causes a crapstorm, or is otherwise inappropriate for this forum, I apologize in advance.


First, bookmark this link: http://livedocs.adobe.com/flash/9.0/...riptLangRefV3/

Second, look at the blue bar at the top of the forum. There is a search function. Use it.

I keep seeing the same bad questions coming up again and again. I do not mean that these are stupid questions - they are bad questions. They are bad because the question assumes certain limitations of previous flash versions, and asks for a solution to a problem that does not need to be solved. Rejoice! AS3 is not AS2 and the myriad wacky workarounds to AS2's shortcomings do not need to be used anymore. Without further ado, the biggest questions you do not need to ask, and the answers to the ones you should.

1. How can I access something in another MovieClip by name?
Why it's bad:
This question assumes many things which are no longer necessary. Namely that everything should be a MovieClip, that instancenames are important, and that "paths" are necessary to get at objects.

What you should have asked:
How do I access a property of a class outside of that class?

Answer:
Easy as pie, once you understand the difference between variable/property names and
instancenames. Let's write some code:
Code:
public class MyClassA extends Sprite {
  public var avalue:Number;

  public function MyClassA():void{
    graphics.beginFill(0xff0000);
    graphics.drawRect(0, 0, 40, 40);
    graphics.endFill();
  }

}
Code:
public class MyClassB extends Sprite {
  public var a1:MyClassA;
  public var a2:MyClassA;

  public function MyClassB(){
    a1 = new MyClassA();
    a1.x = 20; //See?  EASY.
    a1.y = 10;
    a1.avalue = 1;
    a1.addEventListener(MouseEvent.CLICK, tattle);
    addChild(a1);

    a2 = new MyClassA();
    a2.x = 50;
    a2.y = 100;
    a2.avalue = 2;
    a2.addEventListener(MouseEvent.CLICK, tattle);
    addChild(a2);
  }

  public function tattle(event:MouseEvent):void{
    trace("you clicked on: "+(MyClassA(event.target).avalue));
  }
}
The important things to realize here are 1) I never set or used any instancenames. Because they are not necessary. I DID use variables, because variables are actual references to objects.
2) Creating and using classes allows you to use the type checking system to ensure that the object you are dealing with is what you think it is.
3) I used Sprite rather than MovieClip. Sprites are simpler, and should be used when you do not need to add actions to a frame, or have multiple frames. Sprites are also not dynamic, allowing you to check for accessing non-existant properties at compile time.

2. What the heck is "Error #1009: Cannot access a property or method of a null object reference." ?

Why it's bad:
Because the error tells you what it means right there - your code is accessing something on a null object.

What you should have asked:
How can I determine what is null when I get Error 1009?

Answer:
This error occurs when you have a situation like this:
Code:
a:Sprite = null;

a.x = 50;
because you cannot set the x property of nothing. So, when you get Error 1009, look at the line that the error points out (use "go to source"). Everything that proceeds a dot (".") or array access ("[something]") could potentially be the culprit. You should ensure that each such object cannot be null. One way would be to insert trace statements outputting each just before the offending line in your code. One of them will show up null, then you can fix that.
Code:
a:Sprite = new Sprite();
b:Sprite = new Sprite();

function maybeGoBoom(c:Sprite):void{
  trace("a = "+a); //remove these after determining what is null.
  trace("c = "+c);
  a.x = c.x;
}

maybeGoBoom(b); //okay!
maybeGoBoom(null); //not okay!
3. What happened to "_root"?

Why it's bad:
As bad questions go, this one's subtle. First, the livedocs explain that "_root" became "root", and "_parent" became "parent", etc. Look there first before asking. Second, you should almost never actually need to use root. Using root and display heirarchy based access in general is a holdover from AS2 when that was the easiest/best way to do it.

What you should have asked:
How should I structure my AS3 project?

Answer:
In AS3, the displayList can be (and usually is) completely separate from the logic used
inside classes. Classes deal with the logical and conceptual structure of your projects.
There are surprisingly few behavioral reasons to reach back up the display tree. Reaching back up just to go back down is a common bad practice. Instead, it's simpler to pass around actual references to actual objects. Often the parent serves double duty as the visual container and behavioral manager of a group of children. These duties should be kept separate. This example touches on the next question, so let's go on.

4. I have a bunch of stuff sequentially named, but parent["thingy"+i] in a loop doesn't work

Why it's bad:
That's not even a question. At least phrase things as a question. Besides which, names are a terrible way to group and manipulate objects because they're very fragile.

What you should have asked:
I have a bunch of instances which I would like to handle as a group. What's a good way to do this?

Answer:
Arrays are pretty neat. They let you keep multiple objects together in sequence. Read and love:
Code:
public class Thing extends Sprite{
  public vy:Number;  //current up/down velocity
  public incr:Number; //increment at each timeslice

  public function Thing(){
    vy = 0;
    incr = 1;
  }

  public function applyGravity():void{
    y += vy;
    if (y > stage.stageHeight){
      vy *= -1; //bounce!
    }else{
      vy += incr;
    }
  }
}
Code:
public class BounceExample extends Sprite{
  public var howmany:int = 3;
  public var things:Array;
  public var timer:Timer;

  public function BounceExample():void{
    things = new Array();
    var t:Thing;
    for (var i:int = 0; i < howmany; i++){
     t = new Thing();
     things.push(t);
     addChild(t);
    }

    addEventListener(Event.ADDED_TO_STAGE, init);
  }

  public function init(event:Event = null):void{
   for (var i:int = 0; i < things.length; i++){
     t.x = Math.floor(Math.random() * stage.stageWidth);
     t.y = Math.floor(Math.random() * (stage.stageHeight / 2));
   }

   timer = new Timer(50);
   timer.addEventListener(TimerEvent.TIMER, step);
   timer.start();
  }

  public function step():void{
    for (var i:int = 0; i < things.length; i ++){
      things[i].applyGravity();
    }
  }
}
The above code is untested, and probably has at least one bug in it. The point is, the things array is faster to execute and easier to use than accessing properties by name (which are not even set in the example code).

5. I'm trying to do <complex project>, kinda like <website>, but it's not working.
What could be wrong?
.

Why it's a bad question:
This sort of question is far too vague to even begin to answer. Even if I knew what you were trying to do, you should solve problems on a much smaller scale. Include sample code if you have it. ALWAYS include an error message (not just the fact there was one) if an error is given.

What you should have asked:
This code <code sample here> is giving this error <copy and paste error> under such-and-such circumstances. Could someone give me advice?

Answer:
Sure, that's what we're here for.