2014-01-15 82 views
-1

我無法解決這個錯誤,當我這樣做會導致另一個。我希望能夠擊中關鍵「71」,並將新的movieclip實例添加到舞臺上。有什麼建議麼?我是個新手,所以可能很多錯誤......第108行t 1136:參數數量不正確。預計1

包{

import flash.display.MovieClip; //imports needed 
import flash.display.Sprite; 
import flash.events.MouseEvent; 
import flash.events.Event; 
import flash.text.engine.EastAsianJustifier; 
import flash.events.KeyboardEvent; 
import flash.display.Stage; 
public class myJellyFish extends MovieClip { 

    private var startScaleX:Number; 
    private var startScaleY:Number; 
    //private var cliqued:Number; 
    private var stayonscreenLeft:Number; 
    private var stayonscreenRight:Number; 
    private var stayonscreenTop:Number; 
    private var stayonscreenBottom:Number; 

    private var moveDirection:Number; 
    private var speed:Number; 
    private var turnspeed:Number; 


    public function myJellyFish() { 



     startScaleX = this.scaleX; 
     startScaleY = this.scaleY; 
     stayonscreenBottom = 400; 
     stayonscreenRight = 500; 
     stayonscreenLeft = 5; 
     stayonscreenTop = 5; 
     moveDirection = .5; 
     speed = Math.random()*10; 
     turnspeed = 25; 

     this.addEventListener(MouseEvent.ROLL_OVER, scrolledOver); 
     this.addEventListener(MouseEvent.ROLL_OUT, scrolledOff); 
     this.addEventListener(MouseEvent.CLICK, directionChange); 
     this.addEventListener(Event.ENTER_FRAME, life) 


     trace("custom class be a working"); 
     // constructor code 
    } 

    function myMethod() { 
     trace("method also be a workin'"); 
     } 

    private function scrolledOver(Event:MouseEvent):void{ 
     this.alpha = .5; 
     } 

    private function scrolledOff(Event:MouseEvent):void{ 
     this.alpha = 1; 
     } 
    private function directionChange(e:Event):void{ 
     moveDirection = moveDirection * -1; 
     } 
    private function life(e:Event):void{ 
     if (moveDirection > 0){ 
      Hmovement(); 
      } 
     if (moveDirection < 0){ 
      Vmovement(); 
      } 
     } 
    private function Vmovement():void{ 
     this.y += speed; 

     if(this.y <= stayonscreenBottom){ 
      speed = speed * -1; 
      this.startScaleY * -1; 
      } 
     if(this.y >= stayonscreenTop){ 
      speed = speed * -1; 
      this.startScaleY * -1; 
      } 
     } 
     private function Hmovement():void{ 
      this.x += speed; 

      if(this.x >= stayonscreenRight){ 
       speed = speed * -1; 
       } 
      if(this.x <= stayonscreenLeft){ 
       speed = speed * -1; 
       } 
      } 
     private function generate(e:KeyboardEvent):void{ 

      var movieClip:myJellyFish = new myJellyFish(); 
      addChild(movieClip); 
      movieClip.x = (Math.random() * 200) + 20; 
      movieClip.y = (Math.random()*200) + 20; 
      movieClip.name = "jellyfish"; 
      } 



     public function moreClips (event:KeyboardEvent){   //if that key is "F" it will play the tween 
      trace(event.keyCode); 

      if (event.keyCode == 71){ 
      generate(); 
      } 

     } 

    }//end class 

}//end package 

回答

1

首先,作爲凜說,有調用generate功能時,將是一個參數錯誤。其次,您需要添加一個KeyboardEventref監聽器才能接收鍵盤事件。

監聽鍵盤事件的最簡單方法是將偵聽器添加到Stage(因爲無論鍵盤觸發的位置,KeyboardEvents都會觸發舞臺)。爲了獲得對舞臺的引用,您需要等到您的MovieClip已添加到DisplayList(當您的myJellyFish的實例已添加爲某個子節點時)。

您可以通過偵聽Event.ADDED_TO_STAGE事件來完成此操作。

// Your constructor 
public function myJellyFish() { 
    // ... 
    // Add event listener which will trigger when 
    // the MovieClip has been added to the DisplayList 
    this.addEventListener(Event.ADDED_TO_STAGE, handleAddedToStage); 
} 

protected function handleAddedToStage(e:Event):void { 
    // Remove event listener since it's no longer needed 
    this.removeEventListener(Event.ADDED_TO_STAGE, handleAddedToStage); 

    // You now have a reference to the stage, let's add the KeyboardEvent listener 
    stage.addEventListener(KeyboardEvent.KEY_DOWN, moreClips); 
} 

修改:固定錯字removeEventListener

+0

即時通過:場景1,圖層'操作',幀361,7行\t 1120:訪問未定義的屬性moreClips。現在,如果我加入舞臺的東西,我得到:第54行\t 1119:訪問可能未定義的屬性addedToStage通過靜態類型的引用類。 –

+0

@ user3200134我刪除了'Event.ADDED_TO_STAGE'事件(被Event.addedToStage')修復了一個錯字。這應該解決你的第二個問題。 **對於你的第一個問題; **我寫的代碼應該添加到你上面發佈的同一個類中。因此,應該將事件偵聽器添加到構造函數的其餘代碼中,並且應該將handleAddedToStage函數添加到類中。從錯誤,它看起來好像你已經添加我的代碼在一個框架中... –

0

你產生功能具有1個參數至極爲e:的KeyboardEvent,既然你有功能moreClips其alredy具有的KeyboardEvent,你不」 t需要生成函數的參數。

基本上你現在正在做的是調用沒有參數的generate()函數。你所要做的就是從函數中刪除參數。

private function generate():void{ 
     var movieClip:myJellyFish = new myJellyFish(); 
     addChild(movieClip); 
     movieClip.x = (Math.random() * 200) + 20; 
     movieClip.y = (Math.random()*200) + 20; 
     movieClip.name = "jellyfish"; 
} 
相關問題