2015-10-01 275 views
-1

試圖使actionscript上的按鈕:創建和刪除movieclip對象,但總是有一些錯誤...我需要一個按鈕創建movieclip,另一個必須通過鼠標點擊刪除它。這裏是代碼:actionscript 3添加和刪除movieclip按鈕

import flash.events.MouseEvent; 
import flash.display.MovieClip; 
import flash.text.TextField; 

main.addEventListener(MouseEvent.CLICK, addMain); 
function addMain(e: MouseEvent) { 
    var movieClip: bm = new bm(); 
    movieClip.x = 640; 
    movieClip.y = 570; 
    this.addChild(movieClip); 
    main.removeEventListener(MouseEvent.CLICK, addMain); 
    deletemain.removeEventListener(MouseEvent.CLICK, addMain); 
} 

deletemain.addEventListener(MouseEvent.CLICK, deleteMain); 
function deleteMain(e: MouseEvent) { 
    var movieClip: bm = bm(event.target); 
    this.removeChild(movieClip); 
    removeChild(movieClip); 
    movieClip = null; 
    deletemain.removeEventListener(MouseEvent.CLICK, deleteMain); 
} 
+0

如果你有錯誤,你應該顯然發佈它們。他們提供有關如何擺脫它們的有用信息,畢竟,這就是您接收它們的原因。 – null

+0

好吧,我不知道,第一次來這裏。 –

+0

但不是你第一次解釋的東西。始終提供您擁有的所有相關信息。如果你不理解他們,那沒關係,可以成爲問題的一部分。我沒有降低你的問題,但是有可能是有人這樣做的。如果您包含所有相關信息,則社區可以更好地收到您的問題,並且很可能會得到回答。順便說一句,歡迎來到SO! – null

回答

0

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); 
} 

希望能有所幫助。

+0

優秀的@akmozo !!!非常感謝您的幫助!它難以理解動作... –

+0

@AlexBulava不客氣。請參閱[這裏](http://www.adobe.com/devnet/actionscript/learning.html)瞭解有關學習ActionScript 3的更多信息。 – akmozo