0
votes

I keep getting the following error when I fire a Shot I made in Flash CS6 AS 3.0

1118: Implicit coercion of a value with static type Object to a possibly unrelated type 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
3
Can you please read the line number in the error you receive, and then update your question to only include the relevant code (i.e. the function containing the troublesome code).Marty
Does Class Shot extend MovieClip/Sprite?Nicolas Siver
Shot extends Sprite, and the error is on line 90user3390593
Line 90 is "stage.removeChild(e.currentTarget);"user3390593

3 Answers

0
votes

Line 90 is stage.removeChild(e.currentTarget);

With this, you need to cast e.currentTarget to a DisplayObject:

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

Just a guess: you've started your ENTER_FRAME loop, (which will remove 'b') before you've said addChild(b). Try putting 'b' on the display list before you try to remove it!!

0
votes

Aha! I think I may have found your problem, though I am unsure of how it relates to the error code...

Take a look at your Shot class (which I have found here https://stackguides.com/questions/22244959/why-does-this-not-shoot). Every frame, you tell the Shot class to test whether it is in the boundaries of the stage still, and if so, to remove itself from the stage. In line 90, you also tell the stage to determine if the bullet is still in bounds, and if so, to remove it again.

You have tried to remove the bullet from the stage twice!

Again, I stress, this seems to be unrelated to the error code, but hopefully it points you in the right direction.