Make your own free website on Tripod.com

Scope difference in Flash MX and Flash 5
ericlin@ms1.hient.net


Many experienced trouble during shift from Flash 5 fla to Flash MX. Why the flash 5 fla wont work in Flash MX. There are many conditions. One of the commonest condition is that, those movie use MovieClip.prototype.custmfun() to extend the methods of movieClip class. Because the definition of scope for variables changed in Flash MX.


For example: in Frame 1 of the _level0,

MovieClip.prototype.moveLeft=function(){
    _x-=20;
}

This will work in Flash 5, but no in MX. If we call myMc.moveLeft() then the _root._x will move left, not the movieClip itself. In Flash MX, the correct script is:

MovieClip.prototype.moveLeft=function(){
    this._x-=20;
}

If we want to move the movieClip left, we must add "this" to this function.


When we define a function in a movieClip:

moveDown=function(){
    _y+=20;

Usually we define this function in the movieClip and we call this function in the movieClip. Definitely, we are talking about the _y of this movieClip.

However, sometimes it is more complex.

When this function is called by another movieClip. Whose _y will increase by 20 ?  The movieClip that we defined function in ? or the movieClip that calls this function ?


What I am going to talk is the rule for Flash MX. I will mention a little about Flash 5 later.


When we define a function in a movieClip:

moveRight=function(){
    this._x;
}

When this function is called by another movieClip, which will move rightward ?

Here is the keyword "this". It means the caller. It does not matter where we define this function in. Just the caller does matter.

Say:

_root.rMoveRight=_root.mc.moveRight;

_root.rMoveRight(); will make the _root move rightward.

_root.mc.moveRight() will make the mc move rightward.

So, in _root frame 1, we define MovieClip.prototype.moveRight=function(){this._x+=20;}

Then any movieClip can call this method "moveRight", and what moves is the movieClip that calls this funciton.

(Note: this is the same for Mx and Flash 5)


What if we dont have "this" before the property _y ?

When we define a function in the block of MovieClip MC without keyword "this", the _y depends on where we "define" this function. If we define the function in the block of movieclip  Mc, then it is Mc._y; It does not matter how this function is called. @

moveDown1=function(){
    _y+=20;
    // It target  mc._y

For the same expression, if we define that function in _root, then _y is _root._y;

_root.mc.moveDown2=function(){
    _y+=20;
    // it target the _root._y;
}

Now mc has two function, one is moveDown1, the other is moveDown2. They behave different.

What is that _y is fixed. It does not matter who, how or what calls this function.

If we say:

_root.rMoveDown1=_root.MC.moveDown1;

_root.rMoveDown1();

Here we changed the caller of this function to _root, since that function is defined in mc, the result is still making the mc._y moving.

(Note, this is key difference between Mx and Flash 5. In Flash 5, every property or variable mentioned alone is treated as this.property)


@

@