OK,理解爲什麼你的代碼不能正常工作,讓我們來看看它:
main.addEventListener(MouseEvent.CLICK, addMain);
function addMain(e: MouseEvent)
{
// here you are creating a local var "movieClip"
// inside your "addMain" function, so it's only accessible here
var movieClip:bm = new bm();
movieClip.x = 640;
movieClip.y = 570;
this.addChild(movieClip);
main.removeEventListener(MouseEvent.CLICK, addMain);
// I don't really know why you do this !
deletemain.removeEventListener(MouseEvent.CLICK, addMain);
}
deletemain.addEventListener(MouseEvent.CLICK, deleteMain);
function deleteMain(e: MouseEvent)
{
// here, you are creating a local var "movieClip"
// by casting (converting) your "deletemain" button (e.target) to an "bm" object
// event.target should be e.target
var movieClip: bm = bm(event.target);
// here your are removing the "deletemain" button, once
this.removeChild(movieClip);
// and twice
removeChild(movieClip);
movieClip = null;
deletemain.removeEventListener(MouseEvent.CLICK, deleteMain);
}
所以,做你想做什麼:創建一個影片剪輯,並刪除它使用兩個按鈕,你必須聲明你「的動畫片段」作爲一個全局變量,爲你的兩個監聽功能可訪問:
// declare a global "bm" object
var movieClip:bm;
btn_add.addEventListener(MouseEvent.CLICK, btn_add_onPress);
function btn_add_onPress(e: MouseEvent): void
{
// create the "movieClip" object
movieClip = new bm();
addChild(movieClip);
// remove the click event listener for the btn_add
btn_add.removeEventListener(MouseEvent.CLICK, btn_add_onPress);
}
btn_delete.addEventListener(MouseEvent.CLICK, btn_delete_onPress);
function btn_delete_onPress(e:MouseEvent) : void
{
// remove the "movieClip" object
removeChild(movieClip);
movieClip = null;
// remove the click event listener for the btn_delete
btn_delete.removeEventListener(MouseEvent.CLICK, btn_delete_onPress);
}
希望能有所幫助。
如果你有錯誤,你應該顯然發佈它們。他們提供有關如何擺脫它們的有用信息,畢竟,這就是您接收它們的原因。 – null
好吧,我不知道,第一次來這裏。 –
但不是你第一次解釋的東西。始終提供您擁有的所有相關信息。如果你不理解他們,那沒關係,可以成爲問題的一部分。我沒有降低你的問題,但是有可能是有人這樣做的。如果您包含所有相關信息,則社區可以更好地收到您的問題,並且很可能會得到回答。順便說一句,歡迎來到SO! – null