What I initially wanted to do is this:

if (myTargetName == 'a') {
    var myOtherTargetName:String = "b";
    var myOtherTarget:DisplayObject = getChildByName(myOtherTargetName);
}


if (myTargetName == 'c') {
    var myOtherTargetName:String = "d";
    var myOtherTarget:DisplayObject = getChildByName(myOtherTargetName);
}

as you can see, I get a 'Duplicate variable definition' error since im declaring the variables twice. My solution I thought would be to just declare those variables outside the if statements, and change the variable inside the statement, like so:

var myOtherTargetName:String = ""
var myOtherTarget:DisplayObject = None;
if (myTargetName == 'a') {
    myOtherTargetName = "b";
    myOtherTarget = getChildByName(myOtherTargetName);
}


if (myTargetName == 'c') {
    myOtherTargetName = "d";
    myOtherTarget = getChildByName(myOtherTargetName);
}

This gives an error saying "access of undefined property None". Now, I don't want to set

myOtherTarget:DisplayObject

to an actual existing display object which is on the stage just yet, I want to set it to an object on the stage inside the if statements. Is there a way to set

myOtherTarget:DisplayObject

to nothing?

有帮助吗?

解决方案

It is easy, copy+paste it:

var myOtherTarget:DisplayObject = null;

or simply:

var myOtherTarget:DisplayObject;
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top