2015-12-15 109 views
-2

不工作,我有我的,我在其他類添加EventLisitener到精靈像這樣的主類:添加事件監聽到精靈在其他類AS3

public function Main() { 
    if (stage) init(); 
    else addEventListener(Event.ADDED_TO_STAGE, init); 

    addChild(Menu.menuBackground); 
    addChild(Menu.startButton); 
    //Adds event Listener to Menu's Sprite startButton. 
    Menu.startButton.addEventListener(MouseEvent.CLICK, startGame); 
} 

private function init(e:Event = null):void { 
    removeEventListener(Event.ADDED_TO_STAGE, init); 
} 

這一切工作非常愉快,但後來我試着以同樣的方式再做一遍。但是,點擊這些精靈不會做任何事情。這是完整的主類代碼。與其他三個Sprite類的代碼一起。

全部主營:

package { 

    //Other Files 
    import Menu; 
    import CrossHair; 
    import Birds; 

    import flash.display.Bitmap; 
    import flash.display.Sprite; 
    import flash.events.Event; 
    import flash.events.MouseEvent; 
    import flash.ui.Mouse; 

    public class Main extends Sprite { 

     //Game values 
     public static var gameWidth:int = 750; 
     public static var gameHeight:int = 750; 

     [Embed (source = "lib/background.png")] 
     public var backgroundClass:Class; 
     public var background:Bitmap = new backgroundClass(); 

     public function Main() { 
      if (stage) init(); 
      else addEventListener(Event.ADDED_TO_STAGE, init); 

      addChild(Menu.menuBackground); 
      addChild(Menu.startButton); 
      Menu.startButton.addEventListener(MouseEvent.CLICK, startGame); 
     } 

     private function init(e:Event = null):void { 
      removeEventListener(Event.ADDED_TO_STAGE, init); 
     } 

     //Function starts game 
     public function startGame(Evt:Event):void { 
      Mouse.hide(); //Hides Mouse 

      removeChild(Menu.startButton); //Get rid of startButton 
      Menu.startButton.removeEventListener(MouseEvent.CLICK, startGame); 
      reRender(); 

      //Add eventListiners 
      addEventListener(Event.ENTER_FRAME, update); 
      Birds.bird.addEventListener(MouseEvent.CLICK, Birds.shot); 
      EnterShopButton.shopButton.addEventListener(MouseEvent.CLICK, enterShop); 
     } 

     public function reRender():void { 
      addChild(background); //Add background 
      addChild(Birds.bird); //Add birds. 
      addChild(EnterShopButton.shopButton); //Add UpgradeMenuButton 
      addChild(CrossHair.crossHair); //Add crosshair 
     } 

     public function enterShop():void { 
      stage.removeChildren(); //Removes all children from stage. 
     } 

     public function update(evt:Event):void { 
      Birds.update(); 

      CrossHair.crossHair.x = mouseX - (CrossHair.crossHairImg.width/2); 
      CrossHair.crossHair.y = mouseY - (CrossHair.crossHairImg.height/2); 
     } 


    } 

} 

事件Lisitners:

package { 

    //Other files 
    import flash.events.Event; 
    import Main; 

    import flash.display.Sprite; 
    import flash.display.Bitmap; 

    public class Menu extends Sprite { 

     //create menbu background Bitmap 
     [Embed (source = "lib/menubackground.png")] 
     public static var menuBackgroundClass:Class; 
     public static var menuBackground:Bitmap = new menuBackgroundClass(); 

     //create startButton Bitmap 
     [Embed (source = "lib/menustartbutton.png")] 
     public static var startButtonClass:Class; 
     public static var startButtonImg:Bitmap = new startButtonClass(); 
     public static var startButton:Sprite = new Sprite(); 

     //Set startButton's values 
     startButton.addChild(startButtonImg); 
     startButton.x = (Main.gameWidth/2) - (startButtonImg.width/2); 
     startButton.y = (Main.gameHeight/2) - (startButtonImg.height/2); 
    } 
} 

package { 

    //Other files 
    import Main; 

    import flash.display.Sprite; 
    import flash.display.Bitmap; 
    import flash.events.MouseEvent; 

    public class Birds extends Sprite { 

     public static var xSpeed:int = 10; 
     public static var ySpeed:int = 10; 
     public static var dead:Boolean = false; 

     //Create bird Sprite 
     [Embed (source = "lib/bird.png")] 
     public static var birdClass:Class; 
     [Embed (source = "lib/birdead.png")] 
     public static var deadBirdClass:Class; 
     public static var birdImg:Bitmap = new birdClass(); 
     public static var deadBirdImg:Bitmap = new deadBirdClass(); 
     public static var bird:Sprite = new Sprite(); 

     //Sets Sprite's values 
     bird.addChild(birdImg); 
     bird.buttonMode = true; 
     bird.x = 0; 
     bird.y = 0; 

     public static function update():void { 
      bird.x += Math.random() * xSpeed; 
      bird.y += Math.random() * ySpeed; 
      if (!dead) { 
       if (bird.x >= (Main.gameWidth - birdImg.width) || bird.x <= 0) { 
        xSpeed = xSpeed * -1; 
       } 
       if (bird.y >= (Main.gameHeight - birdImg.height) || bird.y <= 0) { 
        ySpeed = ySpeed * -1; 
       } 
      } else { 
       if (bird.y > (Main.gameHeight - deadBirdImg.height)) { 
        resetBird(); 
       } 
      } 
     } 

     public static function shot(evt:MouseEvent):void { 
      if (!dead) { 
       bird.removeChild(birdImg); 
       bird.addChild(deadBirdImg); 
       dead = true; 
       xSpeed = 0; 
       ySpeed = 50; 
      } 
     } 

     public static function resetBird():void { 
      bird.removeChild(deadBirdImg); 
      bird.addChild(birdImg); 
      dead = false; 
      bird.x = 0 
      bird.y = 0; 
      xSpeed = 10; 
      ySpeed = 10; 
     } 

    } 

} 


package { 

    //Other Files 
    import Main; 

    import flash.display.Sprite; 
    import flash.display.Bitmap; 
    import flash.events.Event; 
    import flash.events.MouseEvent; 

    public class EnterShopButton extends Sprite{ 

     //Create crossHair 
     [Embed (source = "lib/shopbutton.png")] 
     public static var shopButtonClass:Class; 
     public static var shopButtonImg:Bitmap = new shopButtonClass(); 
     public static var shopButton:Sprite = new Sprite(); 

     //Set CrossHair's values 
     shopButton.addChild(shopButtonImg); 
     shopButton.buttonMode = true; 
     shopButton.x = Main.gameWidth - shopButtonImg.width; 
     shopButton.y = Main.gameHeight - shopButtonImg.height; 

    } 

} 
+0

看到您的其他問題(http://stackoverflow.com/questions/34256441/as3-debugger-stops-responding-while-trying-to-load-image-into- sprite-using-loade),似乎你並沒有真正接受答案。當你繼續做同樣的事情時,不要指望人們繼續幫助你,並期待它突然發揮作用。 我建議你根據你的其他問題的答案修改你的代碼,如果這給你任何問題,隨時張貼他們,人們會很樂意幫助你。 祝你好運! –

回答

0

同樣的問題,同樣的答案。你以前的問題的答案是一樣的,但你仍然堅持做同樣的事情,並陷入完全相同的問題。

停止嘗試在靜態範圍內運行代碼! addChild不能在該範圍和任何其他類型的代碼中工作。

不知道爲什麼我被拒絕了。這裏沒有太多可以回答的地方,因爲同一個用戶的問題在另一篇文章中得到了回答。

這真的不難,用戶想要在類定義範圍運行代碼。在類定義範圍內,你可以實例化變量yes,但這是你所能做的。用戶想要在該範圍內運行代碼和邏輯,並且這完全被編譯器忽略。在他之前的問題中已經指出了這一點,他甚至給出了建議,停止在該範圍內運行代碼以避免進一步的問題。這是非常簡單的理解:

[Embed (source = "lib/menubackground.png")] 
    public static var menuBackgroundClass:Class; 
    public static var menuBackground:Bitmap = new menuBackgroundClass(); 
    //instantiation is allowed at that scope and will work 

    //create startButton Bitmap 
    [Embed (source = "lib/menustartbutton.png")] 
    public static var startButtonClass:Class; 
    public static var startButtonImg:Bitmap = new startButtonClass(); 
    //instantiation is allowed at that scope and will work 
    public static var startButton:Sprite = new Sprite(); 
    //instantiation is allowed at that scope and will work 

    //COMPILER WILL IGNORE AT THAT SCOPE ANY CODE THAT IS NOT INSTANTIATION 
    //SO ALL FOLLOWING LINES ARE IGNORED AND PRODUCE NO RESULT 
    //EVEN TRACING WOULD NOT WORK HERE 
    // (Main.gameWidth/2) WILL NOT BE CALCULATED AND DOES NOT PRODUCE ANY VALUE 
    // startButton.x WILL NOT BE SET AND WILL STAY AT DEFAULT VALUE 0 
    // FINALLY startButton WILL NOT addChild ANYTHING. 
    //ANY FORM OF CODE LOGIC IS IGNORED AT THAT SCOPE AND WILL NEVER PRODUCE ANY RESULT 
    startButton.addChild(startButtonImg); 
    startButton.x = (Main.gameWidth/2) - (startButtonImg.width/2); 
    startButton.y = (Main.gameHeight/2) - (startButtonImg.height/2); 
+0

請在回答之前進行編譯和檢查。如果你要閱讀我的答案,你會發現在編譯startButton的時候完美的工作。 x和y屬性完美地工作。這些不是我的問題。這些變量的靜態狀態沒有影響,你可以在編譯時看到。請重新閱讀我的問題,並正確回答。 –