2014-03-07 45 views
0

我不斷收到以下錯誤,當我火射擊我在Flash CS6由AS 3.0拍攝閃光燈CS6的ActionScript子彈3.0

1118:靜態類型對象的值隱式強制爲可能無關的鍵入flash.display:DisplayObject。

package 
    { 
     import flash.display.MovieClip; 
     import flash.ui.Mouse; 
     import flash.utils.Timer; 
     import flash.events.TimerEvent; 
     import flash.ui.Keyboard; 
     import flash.events.KeyboardEvent; 
     import flash.events.Event; 
     import flash.events.MouseEvent; 
     import flash.media.SoundChannel; 

     public class AvoiderGame extends MovieClip 

     { 

      public var army:Array; 
      public var reishoot:ReiShoot; 
      public var enemy:Enemy; 
      public var avatar:Avatar; 
      public var gameTimer:Timer; 
      public var useMouseControl:Boolean; 
      public var downKey:Boolean; 
      public var bullets:Array = []; 
      public var backgroundMusic:BackgroundMusic; 
      public var enemySound:EnemySound; 
      public var bgmSoundChannel:SoundChannel; 
      public var sfxSoundChannel:SoundChannel; 


      function AvoiderGame() 
      { 
       /*useMouseControl = false; 
       downKey = false; 

       if(useMouseControl) 
       { 
        avatar.x = mouseX; 
        avatar.y = mouseY; 

       } 
       else 
       { 
        avatar.x = 200; 
        avatar.y = 250; 

       } 
       */ 

       backgroundMusic = new BackgroundMusic(); 
       bgmSoundChannel = backgroundMusic.play(); 
       bgmSoundChannel.addEventListener(Event.SOUND_COMPLETE, onBackgroundMusicFinished); 
       enemySound = new EnemySound(); 
       army = new Array(); 
       //Initial Position of the Enemy 
       var newEnemy = new Enemy(2700, 600); 

       army.push(newEnemy); 
       addChild(newEnemy); 

       avatar = new Avatar(); 
       addChild(avatar); 


       avatar.height = 220; 
       avatar.width = 120; 

       avatar.addEventListener(MouseEvent.CLICK, shoot); 
       gameTimer = new Timer(25); 
       gameTimer.addEventListener(TimerEvent.TIMER, onTick); 
       gameTimer.start(); 

       //addEventListener(Event.ADDED_TO_STAGE, onAddToStage); 
      }//End AvoiderGame 

      function shoot(e:MouseEvent):void 
      { 

       var b:Shot = new Shot(); 
       b.addEventListener(Event.ENTER_FRAME, bulletflies); 
       stage.addChild(b); 
       bullets.push(b); 
      } 

      function bulletflies(e:Event):void 
      { 
       e.currentTarget.y -= 5; 
       if(e.currentTarget.y < 0 || e.currentTarget.y > stage.height) 
        { 
         stage.removeChild(e.currentTarget); 
         bullets.splice(bullets.indexOf(e.currentTarget), 1); 
        } 
      } 

      public function onBackgroundMusicFinished(event:Event):void 
      { 
       bgmSoundChannel = backgroundMusic.play(); 
       bgmSoundChannel.addEventListener(Event.SOUND_COMPLETE, onBackgroundMusicFinished); 
      } 

      public function onKeyPress(keyboardEvent:KeyboardEvent):void 
      { 
        if (keyboardEvent.keyCode == Keyboard.DOWN) 
        { 
         downKey = true; 
        } 
      } 

      public function onKeyRelease(keyboardEvent:KeyboardEvent):void 
      { 
        if (keyboardEvent.keyCode == Keyboard.DOWN) 
        { 
         downKey = false; 
        } 
      } 

      public function onAddToStage(event:Event):void 
      { 

       stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyPress); 
       stage.addEventListener(KeyboardEvent.KEY_UP, onKeyRelease); 
      } 



      public function onTick(timerEvent:TimerEvent):void 
      { 
       if (Math.random() < 2800) 
       { 
        var randomY:Number = Math.random() * 2800; 
        var newEnemy:Enemy = new Enemy(width, randomY); 
        army.push(newEnemy); 
        addChild(newEnemy); 
        gameScore.addToValue(1); 
        //sfxSoundChannel = enemySound.play(); 
       }//End if statement 
       /* 
       if(useMouseControl) 
       { 
        avatar.x = mouseX; 
        avatar.y = mouseY; 
       } 

       else 
       { 
        if (downKey) 
        { 
         avatar.moveDown(); 
        } 
       } 
       */ 
       avatar.x = mouseX; 
       avatar.y = mouseY; 



       for each (var enemy:Enemy in army) 
       { 
        enemy.moveDownABit(); 
        if (avatar.hitTestObject(enemy)) 
        { 
         bgmSoundChannel.stop(); 
         gameTimer.stop(); 
         dispatchEvent(new AvatarEvent(AvatarEvent.DEAD)); 
        }//End if statement 
       }//End for loop 
      }//End onTick function 

      public function getFinalScore():Number 
      { 
       return gameScore.currentValue; 
      } 


     }//End AvoiderGame class 
    }//End package 
+0

您能否請您閱讀收到的錯誤中的行號,然後更新您的問題以僅包含相關代碼(即包含麻煩代碼的函數)。 – Marty

+0

'Shot'類是否擴展MovieClip/Sprite? –

+0

Shot擴展Sprite,並且錯誤位於第90行 – user3390593

回答

0

只是一個猜測:你開始你的ENTER_FRAME循環,(這將刪除 'B')你說addChild(b)之前。嘗試將「b」放在顯示列表之前您嘗試刪除它! 90

0

線是stage.removeChild(e.currentTarget);

有了這個,你需要轉換e.currentTargetDisplayObject

stage.removeChild(e.currentTarget as DisplayObject); 
+0

我試過,但它只給了我另一個錯誤,第90行\t 1120:訪問未定義的屬性DisplayObject。 – user3390593

+0

@ user3390593您需要'導入flash.display.DisplayObject'。 – Marty

+0

編譯!它只是不會拍攝Shot對象。它應該「射擊」Shot對象,但它仍然不會 – user3390593

0

啊哈!我想我可能已經發現你的問題,但我不確定它是如何與錯誤代碼相關的...

看看你的Shot班(我在這裏找到了https://stackoverflow.com/questions/22244959/why-does-this-not-shoot)。每一幀,你都會告訴Shot類來測試它是否仍處於舞臺的邊界,如果是,則從舞臺上移開。在第90行中,您還告訴舞臺確定子彈是否仍在邊界,如果是,則再次刪除它。

您試圖從舞臺上刪除兩顆子彈!

同樣,我強調,這似乎與錯誤代碼無關,但希望它指向正確的方向。