2015-03-31 58 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