2012-01-05 73 views
0

我想刪除我在屏幕上創建的框。我有一個框導出爲 框和導出爲播放器的船,它們都是影片剪輯。這是代碼:刪除鼠標單擊as3上的框

package { 
    import flash.display.MovieClip; 
    import flash.events.Event; 
    import flash.events.MouseEvent; 

    public class Main extends MovieClip { 

     //playermovevar 
     private var _player:MovieClip; 
     private var _playerSpeed:Number=5; 
     private var _destinationX:int; 
     private var _destinationY:int; 



     //boxaddvar 
     private var boxAmount:Number=0; 
     private var boxLimit:Number=20; 
     private var _root:Object; 

     public function Main() { 
      createPlayer(); 

      //playermovlisten 
      stage.addEventListener(Event.ENTER_FRAME, enterFrameHandler); 
      stage.addEventListener(MouseEvent.MOUSE_DOWN, mouseHandlerdown); 

      //boxaddlisten 
      addEventListener(Event.ENTER_FRAME, eFrame); 

      _box.addEventListener(MouseEvent.CLICK, boxclick); 


     } 

     //playermoving 
     private function createPlayer():void { 
      _destinationX=stage.stageWidth/2; 
      _destinationY=stage.stageHeight/2; 

      _player = new Player(); 
      _player.x=stage.stageWidth/2; 
      _player.y=stage.stageHeight/2; 
      stage.addChild(_player); 
     } 


     private function enterFrameHandler(event:Event):void { 
      _player.x += (_destinationX - _player.x)/_playerSpeed; 
      _player.y += (_destinationY - _player.y)/_playerSpeed; 
     } 


     private function mouseHandlerdown(event:MouseEvent):void { 
      _destinationX=event.stageX; 
      _destinationY=event.stageY; 
      addEventListener(MouseEvent.MOUSE_UP, mouseHandlerup); 

      rotatePlayer(); 
     } 
     private function mouseHandlerup(event:MouseEvent):void { 

     } 

     private function rotatePlayer():void { 
      var radians:Number=Math.atan2(_destinationY-_player.y,_destinationX-_player.x); 
      var degrees:Number = radians/(Math.PI/180) + 90; 
      _player.rotation=degrees; 
     } 
     //boxadding 
     private function eFrame(event:Event):void { 
      if (boxAmount<=boxLimit) { 
       boxAmount++; 

       var _box:Box=new Box ; 

       _box.y=Math.random()*stage.stageHeight; 

       _box.x=Math.random()*stage.stageWidth; 

       addChild(_box); 


      } else if (boxAmount >= boxLimit) { 
       removeEventListener(Event.ENTER_FRAME, eFrame); 
      } else { 
       addEventListener(Event.ENTER_FRAME, eFrame); 
      } 
     } 
     function boxclick(event:MouseEvent):void { 
      removeChild(_box); 
     } 
    } 

它給我這個錯誤:

1120:未定義的屬性_box的訪問。 _box.addEventListener(MouseEvent.CLICK,boxclick);

1120:訪問未定義的屬性_box。 removeChild之(_box);

有人知道最新錯了嗎?

感謝

回答

0

不知道什麼樣的結果你想,但試試這個,隨便問任何問題。

package { 
    import flash.display.MovieClip; 
    import flash.events.Event; 
    import flash.events.MouseEvent; 

    public class Main extends MovieClip { 

     //playermovevar 
     private var _player:Player; 
     private var _playerSpeed:Number=5; 
     private var _destinationX:int; 
     private var _destinationY:int; 



     //boxaddvar 
     private var boxAmount:Number=0; 
     private var boxLimit:Number=20; 
     private var _root:Object; 

     private var _box:Box; 
     private var arrayOfBoxes:Array = new Array(boxLimit); 

     public function Main() { 
      createPlayer(); 

      //playermovlisten 
      stage.addEventListener(Event.ENTER_FRAME, enterFrameHandler, false, 0, true); 
      stage.addEventListener(MouseEvent.MOUSE_DOWN, mouseHandlerdown, false, 0, true); 

      //boxaddlisten 
      addEventListener(Event.ENTER_FRAME, eFrame, false, 0, true); 




     } 

     //playermoving 
     private function createPlayer():void { 
      _destinationX=stage.stageWidth/2; 
      _destinationY=stage.stageHeight/2; 

      _player = new Player(); 
      _player.x=stage.stageWidth/2; 
      _player.y=stage.stageHeight/2; 
      stage.addChild(_player); 
     } 


     private function enterFrameHandler(event:Event):void { 
      _player.x += (_destinationX - _player.x)/_playerSpeed; 
      _player.y += (_destinationY - _player.y)/_playerSpeed; 
     } 


     private function mouseHandlerdown(event:MouseEvent):void { 
      _destinationX=event.stageX; 
      _destinationY=event.stageY; 
      addEventListener(MouseEvent.MOUSE_UP, mouseHandlerup, false, 0, true); 

      rotatePlayer(); 
     } 
     private function mouseHandlerup(event:MouseEvent):void { 

     } 

     private function rotatePlayer():void { 
      var radians:Number=Math.atan2(_destinationY-_player.y,_destinationX-_player.x); 
      var degrees:Number = radians/(Math.PI/180) + 90; 
      _player.rotation=degrees; 
     } 
     //boxadding 
     private function eFrame(event:Event):void { 
      if (boxAmount<=boxLimit) { 
       boxAmount++; 

       _box =new Box(); 

       _box.y=Math.random()*stage.stageHeight; 

       _box.x=Math.random()*stage.stageWidth; 

       _box.addEventListener(MouseEvent.CLICK, boxclick, false, 0, true); 

       arrayOfBoxes.push(_box); 

       addChild(_box); 


      } else if (boxAmount >= boxLimit) { 
       removeEventListener(Event.ENTER_FRAME, eFrame); 
      } else { 
       addEventListener(Event.ENTER_FRAME, eFrame, false, 0, true); 
      } 
     } 
     function boxclick(evt:MouseEvent):void { 
      removeChild(evt.currentTarget as Box); 
     } 
    } 
} 
+0

運作良好,但只有2件事(1)我必須點擊其他地方之前,我點擊另一個盒子。 (2)(我想我忘了說這個)當我點擊它需要重新出現在舞臺上的其他地方的盒子。感謝雖然它幫助很大 – thor625 2012-01-05 19:43:04

+0

在這種情況下,不要刪除框,只要移動它。函數boxclick(evt:MouseEvent):void { \t \t \t //removeChild (evt.currentTarget as Box); \t \t \t \t \t \t _box = evt.currentTarget如框; \t \t \t \t \t \t _box.y =的Math.random()* stage.stageHeight; _box.x = Math.random()* stage.stageWidth; } – crooksy88 2012-01-05 20:13:46

+0

我認爲這是有效的,但我寧願它刪除並重新添加到其他地方而不是移動它 – thor625 2012-01-05 20:34:38

0
  1. 你忘了關}爲包。

  2. 您的_box變量不存在爲全局屬性,但您嘗試全局訪問它。

:)

[編輯]

酒店距離頂級

_box.addEventListener(MouseEvent.CLICK, boxclick); 

刪除此行,並把它之後

var _box:Box=new Box ; 

然後改變boxclick功能:

function boxclick(event:MouseEvent):void { 
    var _box:Box = event.currentTarget as Box; // here is your box again 

    // make with _box what you want here. Like, 

    removeChild(_box); 
} 
+0

如何使它成爲全球性的,並仍然使其參考框 – thor625 2012-01-05 19:27:59

+0

看看我的文章。您的情況不需要全球化。 – Fabricio 2012-01-05 19:43:08