views:

748

answers:

5

I'm a little bit new to Actionscript, but I can't figure this one out. I've done a lot of searching on this topic and haven't found a clear answer. I've tried the following solutions that people posted online but none of them work.

All the following solutions give the error: 1120: Access of undefined property myVariable

Suggestion #1:

try {
     trace(myVariable); }
catch {
     trace("your variable doesn't exist"); }

Suggestion #2:

if (myVariable) {
     trace("your variable exists!!"); }
else {
     trace("it doesn't exist"); }

Suggestion #3:

if ( myVariable == null )
     trace("your variable doesn't exist");

Suggestion #4:

if ( myVariable == undefined )
     trace("your variable doesn't exist");

Like I said, I've found many forums posts and stuff online that give the above suggestions saying they will work, but they all seem to be giving me that same 1120: Access of undefined property myVariable error.

By the way, in case you are wondering why I would need to check if a variable exists or not, I'm planning on passing variables to the SWF in its URL, so I need to make sure the proper variables exist and handle the code properly if they are not passed in.

+5  A: 

LiraNuna's answer is definitely the right way to access loader parameters. However, to answer the question of how to check for the existence of variables (for posterity), this is done with the hasOwnProperty() method, which exists on all objects:

// to check for this.myVariable
if ( this.hasOwnProperty( "myVariable" ) ) {
    trace("myVariable exists");
} else {
    //Variable doesn't exist, so declare it now
    trace("declaring variable now...");
    this.myVariable = "Default Value";
}

trace( this.myVariable );

That should cover your situation. But I don't know of any way to check the existence of a variable the way you're trying to do, by merely making a reference directly to the variable. I believe you must refer to it through its scope.

fenomas
+2  A: 

By the way, in case you are wondering why I would need to check if a variable exists or not, I'm planning on passing variables to the SWF in its URL, so I need to make sure the proper variables exist and handle the code properly if they are not passed in.

Then you are taking the wrong approach. Here's the right way™ to read and validate SWF parameters, along with default value if they don't exist:

private function parameter(name:String, defaultValue:String):String
{
  // Get parameter list
 var paramObj:Object = LoaderInfo(stage.loaderInfo).parameters;

  // Check if parameter exists
 if(paramObj.hasOwnProperty(name) && paramObj[name] != "")
  return paramObj[name];                     
 else
  return defaultValue;
}

Warning! Since this relays on the 'stage' property, Use this code either from the Document Class or after Event.ADDED_TO_STAGE.

LiraNuna
This worked perfectly! Thanks!
Jakobud
A: 

Thanks for the quick reply. Still not really working. The scope of the variable is just on the top/root level of the script. Basically, I start a new flash file, on the first frame I add the following action:

// to check for this.myVariable
if ( this.hasOwnProperty( "myVariable" ) ) {
     trace("myVariable exists");
}
else
{
     //Variable doesn't exist, so declare it now
     trace("declaring variable now...");
     var myVariable = "Default Value";
}

trace(myVariable);

When I run the flash file, I get this output:

myVariable exists
undefined

I was expecting this:

declaring variable now...
Default Value
Jakobud
the problem you're seeing now is a different issue. In AS3, if/else blocks don't scope the variable. Also, if you declare a variable in a function, it "exists" everywhere in the function, regardless of where you declare it. So "myVariable" *does* exist in the "if" block.
Glenn
Right, the code here exposes a somewhat tricky area. I updated the code in my answer to do what you're aiming at in this answer; give that a try.
fenomas
A: 

fenomas is probably most right. (I should probably modify some of my code to use that method). But the method I currently use for classes is:

try {
  if(this["myFunction") {
    this["myFunction"]();
  }
catch(e:Error) {}

which isn't the most elegant since it does throw exceptions.

My reason for using this sort of code is that we're doing a bit of dynamic coding and abusing "includes" in flex to write boilerplate code.

Glenn
A: 

trace(myVariable);

GIulio Grasso