2015-03-31 45 views
0

我正在爲我的班級製作一款類似於Space Invaders/Galaga的遊戲。我是新來的ActionScript和編碼,我已經創建了一個敵人類,我已經產生了它,然後沿x方向移動。我只是想知道如何讓我的敵人在我的舞臺達到700x500的末端後下降,然後繼續前往另一邊,我假設在我的敵人階級中植入一條if語句,只是不確定如何去做,任何幫助都會做,非常感謝的人。在舞臺上保留物品-Flash

敵對階級

package 
{ 
    import flash.display.MovieClip; 
    public class Enemy extends MovieClip 
    { 
     public function Enemy() 
     { 

      x = 60; 
      y = 30; 
     } 

     public function moveDownABit():void 
     { 


     } 

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

     public function moveDown():void 
     { 

     } 

     public function moveLeft():void 
     { 

     } 

     public function moveUp():void 
     { 

     } 
    } 
} 

遊戲

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; 
     var gameWidth:int = 0; 
     var gameHeight:int = 0; 

     public function SpaceVigilanteGame() 
     { 
      enemy = new Enemy(); 
      addChild(enemy); 
      avatar = new Avatar(); 
      addChild(avatar); 

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


      gameTimer = new Timer(25); 
      gameTimer.addEventListener(TimerEvent.TIMER, moveEnemy); 
      gameTimer.start(); 
     } 
     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(); 
       } 
     } 

    } 
} 

回答

0

這是很基本的東西。 假設您有4個功能在你的敵對階級,moveDown, moveUp, moveRight and moveLeft.

我們需要了解該地區的寬度和高度的敵人會在走動,或者通過硬編碼(會咬你的屁股後面,可能)或做它更加動態的是這樣的:

var gameWidth:int = stage.stageWidth; 
var gameHeight:int = stage.stageHeight; 

stage.stageWidth或其高度對應會給你SWF影片中的像素大小,縮放等等。

您已在文檔類中設置Timer,每80毫秒功能moveEnemy將會執行。另一種方法是使用Event.ENTER_FRAME並每幀執行一個函數。

現在想想看,每次你的功能被調用時,敵人都會下移一點。正如你所說,應該有某種...檢查機制。但是我們不應該把它放在敵人類中,敵人不需要知道何時停止,這是Document類的問題。理想情況下,你應該能夠在各種類別中使用敵人,對吧?因此,當文檔類可以做到這一點時,給予敵人界限只需要很多工作。

解決方案非常簡單,您需要在每次移動敵人時檢查敵人的位置與舞臺的尺寸。

本質:

private var gameWidth:int = 0; 
private var gameHeight:int = 0; 

public function SpaceVigilanteGame(){ 
//...your init code for enemy 
gameWidth = stage.stageWidth; 
gameHeight = stage.stageHeight; 
//...your timer code 
} 

public function moveEnemy(timerEvent:TimerEvent):void{ 
    //lets first check if we can go to the right 
    if(enemy.x+enemy.width+2<=gameWidth){ 
     //so, we take the right outermost border of your enemy by taking its x-position and adding its width. To check if a step right would be out of bounds, we add the amount it WOULD move 
     //as an example, your enemy has the x-position 100, and is 200 pixel wide, is 100+200+2 smaller or equal to 700 (your stage width)? Yes it is, thus you can move it 
     enemy.moveRight(); 
    } 
    else if(enemy.y+enemy.height+2<=gameHeight){ 
     //but what happens if we can't go right? we jump to this additional if condition that will check if the enemy can move down 
     //same thing as above, but instead we use y-position and the heights 
     enemy.moveDown(); 
    } 
    else if(enemy.x-2>=0){ 
     //if we can't move right or down, try left instead 
     //this time we only need the x-position of the enemy, because we need to look at the leftmost border 
     enemy.moveLeft(); 
    } 
    else if(enemy.y-2>=0){ 
     //same deal with going up 
     enemy.moveUp(); 
    } 
} 

這就是未經測試的代碼,但我希望你得到這個例子的一般要點。這裏的問題是,雖然敵人會總是試圖首先移動。當你從x:0,y:0開始時,他會向右移動,直到達到最後,然後向下移動直到達到目的,然後永遠向左和向右移動。


運動101:

  • 如果你想移動到右側x = x + 1
  • 如果你想向左移動x = x - 1
  • 如果你想向上移動y = y - 1
  • 如果你想下移y = y + 1
+0

我得到一個錯誤1061:調用可能未定義的方法moveRight通過與敵人的靜態類型的引用 – 2015-03-31 06:54:13

+0

那麼,你的Enemy類有一個叫'moveRight'的函數嗎?我懷疑它,因爲我沒有在我的答案中寫出函數,只是函數名;) – DodgerThud 2015-03-31 06:55:28

+0

讓我確定我正確地做了這件事,對於我的無知感到抱歉,幾年前我有一個Flash課程,並且避風港之後我們並沒有觸及它,甚至在那之後,我們沒有做太多的腳本編寫工作,並且我們使用了我們的時間線,但是我會回到我的Enemy.as中,並使用名爲moveRight,moveDown,moveLeft和moveUp的函數?如果是這樣,我是否需要應用任何值?此外,我與私有變量有衝突問題。 – 2015-03-31 07:27:05