2014-03-07 47 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行中,您还告诉舞台确定子弹是否仍在边界,如果是,则再次删除它。

您试图从舞台上删除两颗子弹!

同样,我强调,这似乎与错误代码无关,但希望它指向正确的方向。