Groups | Blog | Home
all groups > flash actionscript > september 2005 >

flash actionscript : this.myMovieClip.stop(); vs myMovieClip.stop();



David Stiller
9/7/2005 3:19:41 PM
[quoted text, click to view]

By using the MovieClip class, which happens when you use any instance of
a movie clip symbol -- this is where Flash steps away from the familiar
territory of other languages -- you are, in fact, using classes and OOP. :)


David
stiller (at) quip (dot) net
"Luck is the residue of good design."

ezd_macromedia
9/7/2005 6:39:57 PM
I'm new to flash. I have a pragramming backgroun (C++, PHP, a little
Javascript, etc). I have Studio MX 2004. I have been going along my merry way
using this syntax to control movie clips:
myMovieClip.____();
Now, I tried the target path finder button, and it uses this:
this.myMovieClip.____();
It *seems* as though this is a bit redundant, because flash seems to have an
understood "this" in front of every command, unless told otherwise (for
example, stop() will always stop the timeline that "this" would refer to, so
it's the same as this.stop() right?). Is there any time when the
this.MC.____() syntax must be used? I just want to adopt good coding practices
from the start.
NSurveyor
9/7/2005 6:46:45 PM
this.object and object aren't always going to be the same thing. For instance:

myObj = new Object();
myOtherObj = new Object();
myObj.weeeeee = function(){
trace(this.myOtherObj);
trace(myOtherObj);
}
myObj.weeeeee();

That's because it's myObj's function, so this will refer to myObj

Also, stop(); and this.stop(); are actually two different things. stop() is a
function, and this.stop() is a method. For stop, the difference is
unnoticeable. However, look up gotoAndStop under Global Functions > Timeline
Control and gotoAndStop under Movie > MovieClip > Methods. There is a
difference.. plus I remember Jeckyl addressed some bug with the gotoAndStop
function...
Duke Boyne
9/7/2005 6:53:40 PM
In Flash, "this" refers to the current object or movieClip that your code is
residing in, so in the case of a stop() command written on a particular frame,
generally, "this" is implied by default. However, there are cases when you may
need to declare "this".

stopMe = function(){

}

is different from

this.stopMe = function(){

}

the former is a function that is exposed across the Flash environment (soort
of a _global function), but the latter belongs to the "this" where it was
created.

If you get into Object Oriented Programming in Flash, you may better
understand hoy you can have many references to "this" on a single frame, but
they may point to completely different objects or movieClips.

Since you understand C++ and PHP, I assume that you understand OOP somewhat.
Flash is a little weird on where you can put code because it's so loose.


ezd_macromedia
9/7/2005 7:04:23 PM
I suppose that the 'this' keyword makes sense as you get into classes and OOP,
it just caught me off-guard when it did this.myMovieClip.stop(). it seemed
pretty unnecessary there, and it looks like that's because it is. Thanks for
the info.
NSurveyor
9/7/2005 9:10:49 PM
Originally posted by: Duke Boyne
the former is a function that is exposed across the Flash environment (soort
of a _global function), but the latter belongs to the "this" where it was
created. I am quite sure this is false. Ex, add this code on frame 1:

traceMe = function(x) {
trace(this+':'+x);
}
_global.tracer = function(x) {
trace(this+':'+x);
};
this.tracy = function(x){
trace(this+':'+x);
}

Now, create a new movieclip on the stage. On frame 1 of this movieclip, add:

traceMe('traceMe');
tracer('tracer');
tracy('tracy');
this.traceMe('this.traceMe');
this.tracer('this.tracer');
this.tracy('this.tracy');
_parent.traceMe('_parent.traceMe');
_parent.tracer('_parent.tracer');
_parent.tracy('_parent.tracy');

Test the code, and this it outputted:

undefined:tracer
_level0:_parent.traceMe
_level0:_parent.tracy

The only traces that worked were: tracer(), _parent.traceMe, and
_parent.tracy, AND traceMe and tracy's "this" were both _level0, which proves
that the variable belongs to where it is declared. The variable is only global
when it is defined _global, or close to global, MovieClip.prototype.




AddThis Social Bookmark Button