the correct actionscript to find if object already exists

Posted by touB on Stack Overflow See other posts from Stack Overflow or by touB
Published on 2010-03-19T19:31:14Z Indexed on 2010/03/19 19:41 UTC
Read the original article Hit count: 162

Filed under:
|
|
|
|

I have a Rect object that I'd like to create and set its properties only once. After that, I want to just modify its properties since it already exists. This is my general idea

if(theRect == undefined){

  Alert.show("creating");

  var theRect:Rect = new Rect();
  //then set properties
  addElement(theRect); //then add it using addElement because addChild() does not work

} else {

  Alert.show("updating");

  //no need to create it since it's already been created
  //just access and change the properties

}

I tried several ways and combinations for the if conditional check:

if(theRect == undefined){
if(theRect == null){
declaring and not declaring `var theRect:Rect;` before the if check
declaring and instantiating to null before the if check `var theRect:Rect = null;` 

but can't get the desired effect. Everytime this code block runs, and depending on which version I've used, it either gives me a "can't access null object" error or the if statement always evaluates to true and creates a new Rect object and I get the "creating" Alert.

What's the right way of creating that Rect but only if doesn't exist?

© Stack Overflow or respective owner

Related posts about actionscript

Related posts about mxml