2011-02-11 27 views
1

我想刪除我動態創建的影片剪輯,當出口出現錯誤刪除動態添加的影片剪輯使用removeChild之(未定義的屬性錯誤)

1120:未定義的屬性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); 
} 

正如你可以看到我使用addChild在我的庫中影片剪輯,這可能是庫項目與類名S1,S2,S3 ...

我嘗試使用removechild(getchildbyname(?????));沒有成功。我如何簡單地刪除導出時不存在的影片剪輯?

回答

2

如果您不想聲明player_mc作爲全局變量,並且它是總是最後一個孩子可以使用removeChildAt(numChildren - 1)

0

嘗試將player_mc聲明爲代碼頂部的「全局變量」,而不是在函數startplayer()中聲明。比它應該是內部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); 
} 
0

stoporch函數引用變量player_mc不在範圍之內。它被定義爲startplayer中的本地。

你要麼需要保存在某個地方的引用,stoporch可以看到它,或者在您添加它,然後用getChildByName,當你刪除它的name財產。

0

您的player_mc變量在本地定義,這意味着當函數startplayer()完成時它將消失。

您可以創建功能類變量外:

private var _player_mc : MovieClip; 

,並在函數中這樣創造的:

_player_mc = addplayer("s"+station.value); 

刪除它,只需使用:

removeChild(_player_mc); 
0

有幾個選項。像其他人所說的那樣,在課堂上創建變量是可行的。另一種方法是在製作後爲剪輯分配名稱。

function startplayer():void { 
    player_mc = addplayer("s"+station.value); 
    player_mc.name = "playerMC"; 
    addChild(player_mc) 
    removeChild(this.getChildByName("playerMC")); 
}