Dynamic Identifiers In ActionScript 3.0
Posted in ActionScript, Flex, How-To on June 13th, 2007 by Kalen GibbonsI came across a problem in Flex the other day where I was trying to dynamically identify an image with ActionScript. I’ll spare you the programmatic details, all you need to know is that Coldfusion returns a string that corresponds to the name of an image inside my Flex application. The image name is then used to modify the image object with the corresponding ID.
Now, I know I could have accomplished my goal by using a Switch statement but the number of images in the application would have made the code tedious to write and hard to maintain. So after much searching I found how to dynamically identify objects in my flex application.
Here is an example of how to change the source of an image, where the image’s ID is produced dynamically.
// if the object is in the same component, use THIS scope
this[imageName].source = “images/newImage.jpg“;
// or if the object is part of a sub-component, use the component’s ID as the scope
subComponentID[imageName].source = “images/newImage.jpg“;
I hope this simple example is enough to clarify how to dynamically refer to an ID of an object in ActionScript 3.0; I did a lot of googling before I figured out the syntax for this – even though it’s rather simple. So I hope this is enough to save some of you a bit of searching.
Tags: identifiers