Is there arguments that work most of
the time in AS3? I want a code setup
that will works most of the time.
I don't think I understand your question. Can you give an example?
EDIT
Ok, now that I better understand your questions, here are my 2 cents.
FIRST EXAMPLE
In your first example you are showing how both "this" and "ball2" are Objects which either implement IEventDispatcher or inherit from EventDispatcher. In the first case, which can be rewritten like this:
this.addEventListener(Event.ENTER_FRAME, onLoop, false, 0, true);
You are saying, whenever there is an Event.ENTER_FRAME event fired off by this, run the function onLoop. As pointed out in the other answer, the last three arguments are optional, but I always include them since the default for useWeakReference is false and I like to set it to true. The addEventListener function can only be run on Objects which have the addEventListener method (ie. Classes that extend EventDispatcher, or implement IEventDispatcher.)
Whether you choose to use the first method or the second is really dependent on use-case and preference. Let me give you an example.
package {
class BallHolder extends MovieClip {
private var _balls:Array;
public function BallHolder():void {
this.addEventListener(Event.ENTER_FRAME, _updateBalls, false, 0, true);
_balls = [];
var ball:Ball;
for (var i:Number = 0; i < 10; i++) {
ball = new Ball();
_balls.push(ball);
ball.x = i * ball.width;
ball.y = 100;
addChild(ball);
}
}
private function _updateBalls($evt:Event):void {
// lets move the balls randomly
var _x:Number = 0;
var _y:Number = 0;
for (var i:Number = 0; i < _balls.length; i++) {
_x = Math.round(Math.random() * 10) - 5;
_y = Math.round(Math.random() * 10) - 5;
_balls[i].x += _x;
_balls[y].y += _y;
}
}
}
}
package {
class Ball extends MovieClip {
public function Ball():void {
this.graphics.beginFill(0x00ffff, 1);
this.graphics.drawCircle(25, 25, 25);
this.graphics.endFill();
}
}
}
In the above example, the BallHolder loops through 10 times, creating a new Ball instance, adding it to the DisplayList and pushing it onto an array called _balls. Right after it creates each ball, it places it at (the index * the ball width, 100), so if the balls are 50 pixels in diameter (like the example) then they are placed at (0, 100), (50, 100), (100, 100), etc.
In addition, the BallHolder also has an ENTER_FRAME event handler that loops through each of the balls and updates their positions randomly.
Compare that to this:
package {
class BallHolder extends MovieClip {
private var _balls:Array;
public function BallHolder():void {
_balls = [];
var ball:Ball;
for (var i:Number = 0; i < 10; i++) {
ball = new Ball(i);
_balls.push(ball);
addChild(ball);
}
}
}
}
package {
class Ball extends MovieClip {
public function Ball($index:Number):void {
this.graphics.beginFill(0x00ffff, 1);
this.graphics.drawCircle(25, 25, 25);
this.graphics.endFill();
this.addEventListener(Event.ENTER_FRAME, _updatePosition, false, 0, true);
setPosition($index);
}
public function setPosition($index:Number):void {
y = 100;
x = $index * width;
}
private function _updatePosition($evt:Event):void {
// lets move the ball randomly
x += Math.round(Math.random() * 10) - 5;
y += Math.round(Math.random() * 10) - 5;
x += _x;
}
}
}
Now, all the BallHolder does is create instances of Ball and store them in an array. The Ball does all of the work as far as setting its position and updating it's position.
SECOND EXAMPLE
Still not sure what you mean here. It is quite common to have if/else if/else and switch constructs in event hanlder functions. Do you have a specific question about doing that?
THIRD EXAMPLE
Personally I always use
var flag:Boolean = true;
var str:String = "foo"
and of course
var arr:Array = [];
For the first two, they are built-in classes (or data types) and don't have constructors. Saying something like
var str:String = String("foo");
and
var flag:Boolean = Boolean(true);
are just ways of casting, which is unnecessary in this case, because you are casting them to the same type.
In addition, using [] for arrays is faster than new Array() because the Array constructor takes a single optional argument to create an array of given size. The [] construct does this for you without extra overhead.