2014-06-30 77 views
0

我有问题。我正在用ActionScript 3.0制作Adobe Flash中的直升机游戏,但现在游戏正常运行,但障碍无法从舞台上移除。障碍仍然在数英里之外。我怎样才能消除障碍?如果你是比赛结束的话,同样的问题,如果你是比赛结束的比赛,但最后产生的障碍,你看到仍然没有被删除。我怎么能在一段时间后让障碍更快?无法从舞台上删除对象

其中有一些荷兰语单词,如'hoeSpelen',用于指示文字'af'用于游戏翻转文本,'tijd'=时间和'balkje'=障碍物。

我希望你能帮助我。

package 
{ 
    import flash.display.MovieClip; 
    import flash.events.MouseEvent; 
    import flash.utils.getTimer; 
    import flash.events.Event; 

    public class iCopter extends MovieClip 
    { 
     private var copter : Copter = null; 
     private var gameover : GameOver = null; 
     private var balkje : Balkje = null; 
     private var tijd : int 
     var score = 0; 
     var highscore = 0; 

     public function onStartButton(event:MouseEvent) 
     { 
      startiCopter() 
     } 

     public function iCopter() 
     { 
      startButton.addEventListener(MouseEvent.CLICK, onStartButton); 

      af.visible = false 
      output.visible = false 
      hscore.visible = false 
     } 

     public function startiCopter() 
     { 
      removeChild(startButton); 
      removeChild(hoeSpelen); 
      removeChild(af); 

      score = 0 

      icopterlogo.visible = false 

      output.visible = true 
      hscore.visible = true 

      copter = new Copter(); 
      copter.x = 100; 
      copter.y = 200; 

      addChild(copter); 

      tijd = getTimer(); 

      addEventListener(Event.ENTER_FRAME, onEnterFrame); 
     } 

     public function onEnterFrame(event:Event) 
     { 
      var now:int = getTimer(); 

      if (now - tijd > 1250) 
      { 
       var balkje = new Balkje(); 
       balkje.x = 350; 
       balkje.y = Math.random() * 150; 

       addChild (balkje); 

       tijd = now 

       score = score + 10; 

       output.text = "score: "+score; 

       if (balkje.x <= -10) //don't work. 
       { //don't work. 
        removeChild (balkje); //don't work. 
       } //don't work. 
      } 

      addEventListener(Event.ENTER_FRAME, botsing); 

     } 

     function botsing (event:Event) 
     { 

      for (var i = 0; i < numChildren; i++) 
      { 
       if (getChildAt(i) is Balkje || getChildAt(i) is Vloer) 
       { 
        var b = getChildAt(i) as MovieClip; 

        if (b.hitTestObject(copter)) 
        { 
         removeChild (copter); 
         removeEventListener(Event.ENTER_FRAME, onEnterFrame); 

         var gameover = new GameOver(); 

         addChild(af); 

         af.visible = true 

         addChild(hoeSpelen); 

         addChild(startButton); 

         if (score > highscore) 
         { 
          highscore = score 
          hscore.text = "highscore: "+highscore; 
         } 
        } 
       } 
      } 
     } 
    } 
} 

这里是为直升机和障碍

直升机的脚本:

muisKlik =鼠标点击
muisDruk = mousePush
muisOmhoog = mouseUp事件

package 
{ 
    import flash.display.MovieClip; 
    import flash.events.MouseEvent; 
    import flash.events.Event; 

    public class Copter extends MovieClip 
    { 
     var vy : Number = 0; 
     var muisKlik : Boolean = false; 

     public function Copter() 
     { 
      vy = 5; 
      addEventListener(Event.ENTER_FRAME, onEnterFrame); 
      addEventListener(Event.ADDED_TO_STAGE, init); 
     } 

     private function init(event:Event) 
     { 
      stage.addEventListener(MouseEvent.MOUSE_DOWN, muisDruk); 
      stage.addEventListener(MouseEvent.MOUSE_UP, muisOmhoog); 
     } 

     public function onEnterFrame(event:Event) 
     { 
      if (muisKlik == true) 
      { 
       y -= vy; 
      } 
      else 
      { 
       y += vy; 
      } 
     } 

     public function muisDruk (event:MouseEvent) 
     { 
      muisKlik = true 
     } 

     public function muisOmhoog (event:MouseEvent) 
     { 
      muisKlik = false 
     } 
    } 
} 

障碍:

package 
{ 
    import flash.display.MovieClip; 
    import flash.events.Event; 

    public class Balkje extends MovieClip 
    { 
     var vx : Number = 1; 

     public function Balkje() 
     { 
      vx = 5; 
      addEventListener(Event.ENTER_FRAME, onEnterFrame); 
     } 

     public function onEnterFrame(event:Event) 
     { 
      x -= vx; 
     } 
    } 
} 
+0

尝试使用removeChild(“myObjectHere”)'当您检测到它已经离开屏幕 – CyanAngel

回答

0

未测试 - 也许充满了错误,我已经有一段时间没有完成的AS3:

当初始化障碍,通过在舞台对象(不知道这是最好的做法)

package { 
import flash.display.MovieClip; 
import flash.events.Event; 

public class Balkje extends MovieClip 
{ 
    var vx : Number = 1; 

    public function Balkje() 
    { 
     vx = 5; 
     if(!stage){ 
      //if stage isn't populated yet, wait for it 
      this.addEventListner(Event.ADDED_TO_STAGE,addedToStage); 
     }else{ 
      init(); 
     } 
    } 

    private function addedToStage(e:Event):void { 
     this.removeEventListener(Event.ADDED_TO_STAGE,addedToStage); 
     init(); 
    } 

    protected function init():void { 
     this.addEventListener(Event.ENTER_FRAME, onEnterFrame); 
    } 

    public function onEnterFrame(event:Event) 
    { 
     x -= vx; 

     //check x 
     if(this.x + this.width <= 0 || this.x >= stage.stageWidth) { 
      if(this.parent) this.parent.remmoveChild(this); 
      removeEventListener(Event.ENTER_FRAME, onEnterFrame); 
     } 
    } 
} 
} 

再次,没有测试(是stage.stageWidth正确得到舞台宽度?),但你明白了。检查对象的x,如果它位于可见阶段之外,请将其从父项中移除(我只是将此.parent放置,因为您没有将舞台添加障碍物)。

另外,你为什么要在ENTER_FRAME调用的函数中添加事件监听器到ENTER_FRAME?

+0

作为displayObject的扩展时,已经定义了一个stage对象。 Flash中的所有displayObjects都会获得一个名为'stage'的属性,一旦将它添加到显示列表中,它就会自动填充。从根本上说,这是所问问题的正确方法。 – BadFeelingAboutThis