The School

AS3 Question
New Topic Add Reply Haunt!
littleMonsterGames
  • Posts: 3
  • Signup: 28/03/09
AS3 Question Quote 1 year ago
Is there a way, in AS3, to check whether or not a variable is defined?

Thanks.
Woadraiders
  • Posts: 95
  • Signup: 03/07/09
Re: AS3 Question Quote 1 year ago
If the variable is a number, then you know it hasn't been defined if it's value is NaN.

Code:

var i:Number;
if(isNaN(i))
{
trace("Our variable i has not been defined");
}
else
{
trace("The value of i is "+i);
}


For numbers we use the function isNaN, it returns a Boolean.

For other objects like strings, movieclips, sprites, and I think everything else, it will be null if it's undefined.

Code:

var o:MovieClip;
if(o == null)
{
trace("o is null");
}
else
{
trace(o.x);
}
Bestrafe mich.
Du darfst mein Bestrafer sein!
Der Herrgott nimmt.
Der Herrgott gibt.