Reputation: 2332
I am trying to remove a Movie Clip I created dynamically, when exporting I get the error
1120: Access of undefined property player_mc
function addplayer(id:String):MovieClip {
var mcObj:Object=null;
mcObj=getDefinitionByName(id.toString());
return (new mcObj()) as MovieClip;
}
// this creates the mc
function startplayer():void {
var player_mc:MovieClip = addplayer("s"+station.value);
addChild(player_mc)
}
// this is supposed to remove it
function stopplayer():void {
//the following line causes the error
removeChild(player_mc);
}
As you can see I am using addChild for a Movie Clip in my library, this can be library items with the class name s1, s2, s3...
I tried using removechild(getchildbyname(?????)); with no success. how do I simply remove a Movie Clip that does not exist at export?
Upvotes: 1
Views: 2262
Reputation: 507
A few options exist. Like others stated, creating the variable at class level would work. Another way would be to assign a name to the clip after it is made.
function startplayer():void {
player_mc = addplayer("s"+station.value);
player_mc.name = "playerMC";
addChild(player_mc)
removeChild(this.getChildByName("playerMC"));
}
Upvotes: 0
Reputation: 4434
if you don't want to declare player_mc
as a global variable and if it's always the last child added you may use removeChildAt(numChildren - 1)
Upvotes: 2
Reputation: 8433
your player_mc variable is defined locally, meaning it will disappear when the function startplayer() is done.
You can create a class variable outside of the functions:
private var _player_mc : MovieClip;
and create it like this within your function:
_player_mc = addplayer("s"+station.value);
to remove it, simply use:
removeChild(_player_mc);
Upvotes: 0
Reputation: 2941
Your stoporch
function is referencing a variable player_mc
that is not in scope. It was defined as a local in startplayer
.
You either need to save the reference somewhere that stoporch
can see it, or set the name
property when you add it, and then use getChildByName
when you remove it.
Upvotes: 0
Reputation: 2786
Try to declare player_mc as "global variable" on top of your code and not inside the function startplayer(). Than it should be accessible inside stoporch()
var player_mc:MovieClip;
function addplayer(id:String):MovieClip {
var mcObj:Object=null;
mcObj=getDefinitionByName(id.toString());
return (new mcObj()) as MovieClip;
}
//this creates the mc
function startplayer():void {
player_mc = addplayer("s"+station.value);
addChild(player_mc)
}
//this is supposed to remove it
function stoporch():void {
//the following line causes the error
removeChild(player_mc);
}
Upvotes: 0