ActionScript – this

Published by Leave your thoughts

This ActionScript/Flash Tutorial is like all of my tutorials, quick and to the point. I hope I don’t type too fast for you. 😉

Using “this” in Flash/ActionScript

The “this” command/function is an extremely useful one. It makes it easy to use relative paths (as opposed to absolute).

Here is what the Adobe Dictionary says about “this”:

Keyword; references an object or movie clip instance. When a script executes, this references the movie clip instance that contains the script. When a method is called, this contains a reference to the object that contains the called method.

Inside an on event handler action attached to a button, this refers to the Timeline that contains the button. Inside an onClipEvent event handler action attached to a movie clip, this refers to the Timeline of the movie clip itself.

To put that into English:

If you have a movie clip, and your “on (press)” statement is:

//On Press, the button will disappear.
on (press){
this._visible = 0

This will work no matter how many levels deep into the movie you move this movie clip. “This” calls the path to that movie clip. So if your movie clip is named “mcbutton”, and it’s placed on the root of your movie:

on (press){
// Returns: “_level0.mcbutton”

That is a very useful piece of ActionScript to know, as you will end up using that in every movie you make. basically the same thing as _root. A more extensive definition will come soon.

Another Note about “this”

If the above example was done with a button instead of a movie clip, the trace(); would return “_level0”

-“Flash is by far my favorite computer program.”
-Ashton Sanders

Tags: , , , , ,

Categorised in: Flash ActionScript

This post was written by Ashton