jump to navigation

The with() Statement in Actionscript 3 September 22, 2010

Posted by Tournas Dimitrios in Actionscript.

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

theContent_txt.text = "the business is mine";
theContent_txt.x = 456;
theContent_txt.y = 345;
theContent_txt.alpha = 0.6;

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:


text = "the business is mine!";
x =456;
y =345;
alpha = 0.6;


I refer to theContent_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:




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:


secondObject.x = 340;


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

So, there you have it. This is my quick, short take on using the with() statement. Beyond simply providing aesthetic value to your code, it helps you to easily group together and recognize object assignments.


No comments yet — be the first.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s