2013-11-26 82 views
1

基本上我的問題是我得到我的主要類。它有一個變量,是持有我的水平的對象。但是當我嘗試將這個孩子添加到舞臺上時,我得到一個錯誤...我從來沒有發生過這個錯誤,而且我很困惑。我得到的錯誤是這ActionScript 3.0 addChild拋出錯誤

ArgumentError:錯誤#1063:參數計數不匹配水平/ spawn()。預計0,拿到1 在flash.display使用::級DisplayObjectContainer /的addChild() 在main()的

這裏是我的主類:

package { 

    import flash.display.MovieClip; 


    public class Main extends MovieClip { 
     public static var game:Levels = new Levels(); 

     public function Main() { 
      game = new Levels(); 
      addChild(game); //this triggers my error 
      game.y = 352; 
     } 

    } 

    } 

我這裏是我的水平類

package { 

    import flash.display.MovieClip; 
    import flash.events.Event; 
    import flash.events.KeyboardEvent; 


    public class Levels extends MovieClip 
    { 

     var platforms:Array = new Array(); 

     var gravity:Number = 0; 
     var rPress:Boolean = false; 
     var lPress:Boolean = false; 

     var falling:Boolean = false; 
     var jumping:Boolean = false; 

     var velX:Number = 0; 
     var velY:Number = 0; 

     var a:int = 0; 

     public function Levels() 
     { 
      addEventListener(Event.ADDED_TO_STAGE, spawn); 
     } 
     function spawn() 
     { 
      removeEventListener(Event.ADDED_TO_STAGE, spawn); 
      getMap(); 
      this.stage.addEventListener(KeyboardEvent.KEY_DOWN, kPress); 
      this.stage.addEventListener(KeyboardEvent.KEY_UP, kReleased); 
      this.addEventListener(Event.ENTER_FRAME, loop); 
     } 
     function getMap() 
     { 
      platforms.pop(); 
      for (var a:int = 0; a < numChildren; a ++) 
      { 
       if (getChildAt(a) is Terrain) 
       { 
        platforms.push(getChildAt(a).getRect(this)); 
       } 
      } 
     } 
     function kPress(event:KeyboardEvent) 
     { 
      if (event.keyCode == 65) 
      { 
       lPress = true; 
      } 
      if (event.keyCode == 68) 
      { 
       rPress = true; 
      } 
      if (event.keyCode == 32) 
      { 
       if (jumping == false) 
       { 
        gravity = -25; 
        jumping = true; 
       } 

      } 
     } 
     function kReleased(event:KeyboardEvent) 
     { 
      if (event.keyCode == 65) 
      { 
       lPress = false; 
      } 
      if (event.keyCode == 68) 
      { 
       rPress = false; 
      } 
     } 
     function loop(event:Event) 
     { 
      velX = 0; 

      if (rPress == true) 
      { 
       velX = 7; 
      } 
      if (lPress == true) 
      { 
       velX = -7; 
      } 
      velY = gravity; 
      _player.y += velY; 
      gravity += 2; 

      for (a = 0; a < platforms.length; a ++) 
      { 
       if (_player.getRect(this).intersects(platforms[a])) 
       { 
        if (velY > 0) 
        { 
         jumping = false; 
         _player.y = platforms[a].top - _player.height; 
         gravity = 0; 
        } 
        else if (velY < 0) 
        { 

         _player.y = platforms[a].bottom; 
         gravity = 0; 

        } 
       } 
      } 
      _player.x += velX; 
      if (rPress == true) 
      { 

       if (_player.x + _player.width/2 > 336 + x * -1 && x + width > 640) 
       { 

        x -= velX; 
       } 
      } 
      else 
      { 
       if (_player.x + _player.width/2 > 304 + x * -1 && x < 0) 
       { 

        x -= velX; 
       } 
      } 
      for (a = 0; a < platforms.length; a ++) 
      { 
       if (_player.getRect(this).intersects(platforms[a])) 
       { 
        if (velX == 7) 
        { 
         _player.x = platforms[a].x - _player.width; 
         x += velX; 
        } 
        if (velX == -7) 
        { 
          _player.x=platforms[a].x+platforms[a].width; 
         x += velX; 
        } 
       } 

      } 
     } 
    } 
     } 

回答

1

spawn方法對於addedToStage事件的處理程序,因此預計該事件對象作爲參數:

function spawn(event:Event) 
{ 
    removeEventListener(Event.ADDED_TO_STAGE, spawn); 
    getMap(); 
    this.stage.addEventListener(KeyboardEvent.KEY_DOWN, kPress); 
    this.stage.addEventListener(KeyboardEvent.KEY_UP, kReleased); 
    this.addEventListener(Event.ENTER_FRAME, loop); 
} 
+0

我太蠢了........感謝雖然:P – Mark9135

+1

容易犯的錯誤!請記住接受答案,如果它幫助你。 –