ActionScript.org Flash, Flex and ActionScript Resources - http://www.actionscript.org/resources
Set Property
http://www.actionscript.org/resources/articles/19/1/Set-Property/Page1.html
Jesse Stratford
Jesse lives and works in Melbourne Australia. He is the Cofounder and a Director of http://ActionScript.org. A Flash enthusiast, teacher, author, freelancer and speaker Jesse enjoys participating in the http://ActionScript.org community and the wider Flash scene when he has time. 
By Jesse Stratford
Published on September 9, 2005
 
Written by: Jesse Stratford [email:jessestratford@actionscript.org]
Difficulty Level: Beginner
Requirements: Flash 4 or higher
Topics Covered: How to use the Set Property action in Flash.
Assumed Knowledge: Instances, Paths

Page 1 of 2
Written by: Jesse Stratford [email:jessestratford@actionscript.org]
Difficulty Level: Beginner
Requirements: Flash 4 or higher
Topics Covered: How to use the Set Property action in Flash.
Assumed Knowledge: Instances, Paths

Download the source file for the above movie here. (Zipped, PC, Flash 5 file).

(Those of you who have read the Get Property tutorial might experience a bit of Deja Vu here as I have duplicated many paragraphs since they are relative to Set Property also).

The Set Property function, like its sister Get Property, is vital for any would-be Actionscript user, no matter their level. Mastering this function will broaden your spectrum for interactivity in no time flat!

Let's get going! Let me start by saying the code examples given here are for Flash 5 (and above) but if you can't convert them into Flash 4 code yourself you need help! The only real difference is that Flash 5 (and above) uses 'setProperty' and Flash 4 uses 'Set Property'. *Shock!*

Also note that because this is a beginner level tutorial I am going to start by using the actual setProperty command. We'll get into infix notation later.

All objects in space have attributes or properties: such as width, height, position, color, etc. Flash is no different, all visual elements within your flash movie have properties. Often it is beneficial to us if we can set the values of such properties, and Actionscript allows for this beautifully.

Take the movie clip above as an example. We want to poke and prod Cheesey (my mouse's instance name). Using Set Property we can make him fat/skinny, transparent, upside-down (rotated), invisible and more! Now Cheesey is under our complete control. *Evil laugh*.

Usage of Set Property takes the following basic form:

[as]setProperty ( target, property, value);[/as]

Where target is the instance name of the object within the Flash movie which we are interested in examining, property indicates the property we wish to set and value is the value we wish to set it to. The target is generally the path to a movie clip (see the paths tutorial for more on paths). The value is almost always a number, in expression form, not a String. I say again, not a string - don't use quotes around numbers! See the variables tutorial if you don't quite understand the difference.

We'll go into more depth over the page...

Page 2 of 2
NB: When I first started scripting in '99 it took me [insert gratuitously huge number here] hours to figures out that the target attribute of the command must have quotes around it if it's a direct path, (like "/movieClipName" or "_root.movieClipName" in Flash 5). Flash 5 (and higher) is a little more lenient in this sense, but try to remember to use quotes when the path is a String.

This table outlines the code you can substitute in for the property attribute, and what it will get you.

setProperty ( target, _x , value);
The X axis position (in pixels) of the target clip. value should be a number; no quotes!
setProperty ( target, _y , value);
The Y axis position (in pixels) of the target clip. value should be a number; no quotes!
setProperty ( target, _width, value);
The width (in pixels) of the target clip. value should be a number; no quotes!
setProperty ( target, _height, value);
The height (in pixels) of the target clip. value should be a number; no quotes!
setProperty ( target, _rotation, value);
The rotation angle (in degrees, -360 through 360) of the target clip. value should be a number; no quotes!
setProperty ( target, _name, value);
The instance name of the target clip.
setProperty ( target, _xscale, value);
The X scale proportion relative to the original clip size, (in percent) of the target clip. value should be a number; no quotes!
setProperty ( target, _yscale, value);
The Y scale proportion relative to the original clip size, (in percent) of the target clip. value should be a number; no quotes!
setProperty ( target, _alpha, value);
The alpha fade level (in percent) of the target clip. value should be a number; no quotes!
setProperty ( target, _visible, value);
The visibility status (true (1) or false (0)) of the target clip.

If you open up the example clip above and view the source on each button you will see it is something like this:

[as]on (release) {
setProperty ("_level0.mouse", _x, xpos);
}[/as]

Ignoring the button part of this (the 'on' command) the important line is the second line which gives an example of the usage of Set Property. _level0.mouse is the full path to my mouse movie clip (which I named 'mouse' using the Instance inspector) and is enclosed in quotes because it's a path to a target. _x means I'm wanting to set the X position of the object. xpos is a variable (which the user enters in the text field next to the button). Note that xpos has no quotes around it because I want to set the property to the value within the xpos variable (in this case, the text field).

Finally, Flash 5 (and above) supports infix Set Property notation. The benefit of this is that it is shorter to code and easier to read most of the time. When using infix notation you simply enter the path to the object followed by a dot and then the property you wish to examine (read the paths tutorial if you're lost). So if I want to set the width of a clip with instance name "John" on the main timeline (_root) to 100 pixels I can enter:

[as]setProperty ( "_root.John", _width, 100);
// OR I could use just
_root.John._width = 100;[/as]

The same goes with all properties noted in the above table. It is a good idea to become familiar with the infix notation for Set Property as it is more in keeping with the structure of Flash 5 code. Note that this method is NOT compatible with Flash 4, so if you have Flash 4, or wish to publish for Flash 4 compatibility, stick to the old method.

That's it really. Once you get into interactive environments such as games and navigations, you wont be able to live without Set Property! If you have questions, please post them on the forums rather than emailing me directly.

Jesse Stratford [email:jessestratford@actionscript.org] is the Co-Master of ActionScript.org and a freelance Flash developer and teacher. He is based in Australia and enjoys all things Flash.

NB: If you have comments or feedback please feel free to email me, but please do not email me Flash questions; the forums are provided for that purpose and you will get a faster answer by posting you question there.

If you have found this tutorial helpful, I hope that you will take 30 seconds to visit The Hunger Site where, with just one click you can make a free donation of food to a starving person in a third-world country. We do not benefit financially from this action; it is purely an act of charity.
This tutorial is protected by International Intellectual Property Rights laws and may not be reproduced or redistributed in full or part, without the prior written consent of the author. Unauthorized reproduction of this tutorial or its contents may result in prosecution. I've worked hard on this tutorial, please don't steal it.