2015-03-31 82 views
0

我想在我的遊戲中左右移動我的頭像以及停留在舞臺的邊界內(700X500),我收到一個錯誤,指出「public屬性只能在包中使用「我是否將公共函數放在錯誤的位置?隨着鍵盤(左側和右側)在閃存中移動

package 
{ 
    import flash.display.MovieClip; 
    import flash.events.Event; 
    import flash.utils.Timer; 
    import flash.events.TimerEvent; 
    import flash.events.KeyboardEvent; 
    import flash.ui.Keyboard; 
    public class SpaceVigilanteGame extends MovieClip 

    { 
     public var enemy:Enemy; 
     public var avatar:Avatar; 
     public var gameTimer:Timer; 
     public var useMouseControl:Boolean; 
     public var rightKeyIsBeingPressed:Boolean; 
     public var leftKeyIsBeingPressed:Boolean; 
     var gameWidth:int = 0; 
     var gameHeight:int = 0; 

     public function SpaceVigilanteGame() 
     { useMouseControl = false; 
      leftKeyIsBeingPressed = false; 
      rightKeyIsBeingPressed = false; 
      enemy = new Enemy(); 
      addChild(enemy); 
      avatar = new Avatar(); 
      addChild(avatar); 

      if (useMouseControl) 
      { 
       avatar.x = mouseX; 
       avatar.y = mouseY; 
      } 
      else 
      { 
       avatar.x = 50; 
       avatar.y = 400; 
      } 

      gameWidth = stage.stageWidth; 
      gameHeight = stage.stageHeight; 


      gameTimer = new Timer(25); 
      gameTimer.addEventListener(TimerEvent.TIMER, moveEnemy); 
      gameTimer.addEventListener(TimerEvent.TIMER, moveAvatar); 
      gameTimer.start(); 
      stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyPress); 
      stage.addEventListener(KeyboardEvent.KEY_UP, onKeyRelease); 

     public function onKeyPress(keyboardEvent:KeyboardEvent):void 
     { 
      if (keyboardEvent.keyCode == Keyboard.RIGHT) 
      { 
       rightKeyIsBeingPressed = true; 
      } 
     } 
     public function onKeyRelease(keyboardEvent:KeyboardEvent):void 
     { 
      if (keyboardEvent.keyCode == Keyboard.RIGHT) 
      { 
       rightKeyIsBeingPressed = false; 
      } 
     } 
     } 
     public function moveEnemy(timerEvent:TimerEvent):void 
     { 
      //enemy.moveDownABit(); 
      if(enemy.x+enemy.width+2<=gameWidth) 
       { 
        enemy.moveRight(); 
       } 
      else if(enemy.y+enemy.height+2<=gameHeight) 
       { 
        enemy.moveDown(); 
       } 
      else if(enemy.x-2>=0) 
       { 
        enemy.moveLeft(); 
       } 
      else if(enemy.y-2>=0) 
       { 
        enemy.moveUp(); 
       } 

     } 
     public function moveAvatar(timerEvent:TimerEvent):void 
     { 
      if (useMouseControl) 
      { 
       avatar.x = mouseX; 
       avatar.y = mouseY; 
      } 
      else 
      { 
       if (rightKeyIsBeingPressed) 
       { 
        avatar.moveRight(); 
       } 
      } 
     } 



    } 
} 

頭像等級:

package 
{ 
    import flash.display.MovieClip; 
    public class Avatar extends MovieClip 
    { 
     public function Avatar() 
     { 
      x = 50; 
      y = 400; 

     } 

     public function moveRight() 
     { 
      x = x + 2; 
     } 

     public function moveLeft() 
     { 
      x = x - 2; 
     } 
    } 
} 

回答

0

你有兩個功能,這些功能onKeyPress()onKeyRelease()定義爲publicSpaceVigilanteGame()構造函數中,這就是爲什麼你得到了錯誤,所以要避免這種情況,你必須刪除public或者,如果你想把它們公開,我不這麼認爲,把它們從你的類的構造函數中取出來。

希望能有所幫助。

+0

從'onKeyPress'和'onKeyRelease'中刪除'公共',但現在我有一些錯誤。 Line 46,Column 42 \t 1119:通過靜態類型Class的引用訪問可能未定義的屬性KEY_RIGHT。 第47行,第42列\t 1119:通過靜態類型Class的引用訪問可能未定義的屬性KEY_LEFT。 1061:調用可能未定義的方法moveRight通過靜態類型頭像引用。 – 2015-03-31 22:03:52

+0

@Leopold_Stotch但沒有'KeyboardEvent.KEY_RIGHT'和'KeyboardEvent.KEY_LEFT'事件,有'KeyboardEvent.KEY_DOWN'和'KeyboardEvent.KEY_UP',然後您可以驗證鍵(按下或釋放)是否爲' Keyboard.RIGHT'或'Keyboard.LEFT'。 – akmozo 2015-03-31 22:13:49

+0

只有上下活動? – 2015-04-01 00:26:48