Tag Archives: hide

with() Statement in Actionscript 3

Often times, you will find yourself assigning many properties to one particular movie clip. Let’s say you have a movie clip instance called data_txt. Adding properties to it could look like this:

data_txt.text = "flashbanneronline.com!";
data_txt.x = 256;
data_txt.y = 145;
data_txt.alpha = 0.3;

Notice how you are repeating the word theContent. A better, cleaner method for referencing one particular object would be to use the with() statement.

The with() statement calls an object once and allows you to access all of the object’s properties and methods with only one reference. For example, using the with() statement, the above lines of code can be written as the following:

with(data_txt)
{

text = "flashbanneronline.com!";
x =256;
y =145;
alpha = 0.3;

}

I refer to data_txt only once! The with() statement may not be versatile enough to carry event handlers, but it does the work of referencing almost any class or object for you.

The following is an example of it working with the top level class Mouse:

with(Mouse)
{

hide();

}

In the above code snippet, I used a top level class and called its hide method.

You can also call nested objects inside the original object. Here is a diagram to better help you visualize what I am referring to:

The code for calling the secondObject object using with() would be:


with(firstObject)
{

secondObject.x = 110;

}


Here, I used a direct path to gain access to the properties of the nested secondObject object.