Tutorials Books Videos Forums

-- online Change the theme! Search!
Rambo ftw!

Customize Theme


Color

Background


Done

The with() Statement

by Kevin Kelly aka bear   | filed under Flash and ActionScript

This is an archived tutorial from the kirupa.com legacy collection. It covers software that may no longer be available, but it is kept online because the ideas still hold up.

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;

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(theContent_txt)
{
  text = "the business is mine!";
  _x =456;
  _y =345;
}

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:

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 = 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.

Thank you for your time. If you have any questions, please post them on the forum.

Kevin Kelly
http://kevink.ca

Just a final word before we wrap up. What you've seen here is freshly baked content without added preservatives, artificial intelligence, ads, and algorithm-driven doodads. A huge thank you to all of you who buy kirupa's books, became a paid subscriber, watch the videos, and/or interact on the forums.

Your support keeps this site going! 😇

The KIRUPA Newsletter

Thought provoking content that lives at the intersection of design 🎨, development 🤖, and business 💰 - delivered weekly to over a bazillion subscribers!

SUBSCRIBE NOW

Creating engaging and entertaining content for designers and developers since 1998.

Follow:

Popular

Loose Ends

:: Copyright KIRUPA 2026 //--