错误1118:使用静态类型flash.display隐式强制值:显示对象可能不相关的类型flash.display:MovieClip

时间:2014-02-09 02:29:48

标签: actionscript-3 flash

建立一个自上而下的射击游戏,并在修复了一些错误之后,让一切正常工作,我只剩下一个了。

错误1118:使用静态类型flash.display隐式强制值:显示对象可能不相关的类型flash.display:MovieClip。

这是加载所有对象的Level.as

package {
import flash.display.Sprite;
import flash.display.MovieClip;
import flash.display.Stage;
import flash.events.Event;
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;
import flash.events.MouseEvent;
import flash.events.TimerEvent;
import flash.filters.BlurFilter;
import flash.utils.Timer;
import flash.text.TextField;
import flash.media.Sound;
import flash.media.SoundChannel;
import flash.media.SoundMixer;
import flash.utils.*;
import flash.display.Shape;
import flash.display.DisplayObject

/** 
 * 
 */
public class Level extends Sprite
{

    //these booleans will check which keys are down
    public var leftIsPressed:Boolean = false;
    public var rightIsPressed:Boolean = false;
    public var upIsPressed:Boolean = false;
    public var downIsPressed:Boolean = false;
    //how fast the character will be able to go
    public var speed:Number = 5;
    public var vx:Number = 0;
    public var vy:Number = 0;
    //how much time before allowed to shoot again
    public var cTime:int = 0;
    //the time it has to reach in order to be allowed to shoot (in frames)
    public var cLimit:int = 12;
    //whether or not the user is allowed to shoot
    public var shootAllow:Boolean = true;
    //how much time before another enemy is made
    public var enemyTime:int = 0;
    //how much time needed to make an enemy
    //it should be more than the shooting rate
    //or else killing all of the enemies would
    //be impossible :O
    public var enemyLimit:int = 16;
    //the spaceAuto's score
    public var score:int = 0;
    //this movieclip will hold all of the bullets
    public var bulletContainer:MovieClip = new MovieClip();
    //whether or not the game is over
    public var gameOver:Boolean = false;


    public var SpaceAuto:spaceAuto = new spaceAuto;

    // publiek toegangkelijke verwijzing naar deze class
    public static var instance:Level;

    public var particleContainer:MovieClip = new MovieClip();

    // constructor code
    public function Level()
    {
        instance = this;
        SpaceAuto.x = 135;
        SpaceAuto.y = 350;
        addChild(this.SpaceAuto);
        addChild(this.bulletContainer);
        Project.instance.stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler);
        Project.instance.stage.addEventListener(KeyboardEvent.KEY_UP, keyUpHandler);
        Project.instance.stage.addEventListener(Event.ENTER_FRAME, enterFrameHandler);
        Project.instance.stage.addEventListener(Event.ENTER_FRAME, generateParticles);

        //checking if there already is another particlecontainer there
        if(!contains(particleContainer))
        {
            //this movieclip will hold all of the particles
            addChild(this.particleContainer);
        }


    }

    function keyDownHandler(e:KeyboardEvent):void {
        switch(e.keyCode) {
            case Keyboard.LEFT : leftIsPressed = true; break;
            case Keyboard.RIGHT : rightIsPressed = true; break;
            case Keyboard.UP : upIsPressed = true; break;
            case Keyboard.DOWN : downIsPressed = true; break;
        }

        if(e.keyCode == 32 && shootAllow)
        {
            //making it so the user can't shoot for a bit
            shootAllow = false;
            //declaring a variable to be a new Bullet
            var newBullet:Bullet = new Bullet();
            //changing the bullet's coordinates
            newBullet.x = SpaceAuto.x + SpaceAuto.width/2 - newBullet.width/2;
            newBullet.y = SpaceAuto.y;
            //then we add the bullet to stage
            bulletContainer.addChild(newBullet);
        }

    }

    function keyUpHandler(e:KeyboardEvent):void {
        switch(e.keyCode) {
            case Keyboard.LEFT : leftIsPressed = false; break;
            case Keyboard.RIGHT : rightIsPressed = false; break;
            case Keyboard.UP : upIsPressed = false; break;
            case Keyboard.DOWN : downIsPressed = false; break;
        }
    }

    function enterFrameHandler(e:Event):void {
        vx = -int(leftIsPressed)*speed + int(rightIsPressed)*speed;
        vy = -int(upIsPressed)*speed + int(downIsPressed)*speed;
        SpaceAuto.x += vx;
        SpaceAuto.y += vy;

        if(cTime < cLimit)
        {
            cTime ++;
            trace("++")
        } 
        else 
        {
            //if it has, then allow the user to shoot
            shootAllow = true;
            //and reset cTime
            cTime = 0;
        }

        //adding enemies to stage
        if(enemyTime < enemyLimit)
        {
            //if time hasn't reached the limit, then just increment
            enemyTime ++;
        } 
        else 
        {
            //defining a variable which will hold the new enemy
            var newEnemy = new Enemy();
            //making the enemy offstage when it is created
            newEnemy.y = -1 * newEnemy.height;
            //making the enemy's x coordinates random
            //the "int" function will act the same as Math.floor but a bit faster
            newEnemy.x = int(Math.random()*(stage.stageWidth - newEnemy.width));
            //then add the enemy to stage
            addChild(newEnemy);
            //and reset the enemyTime
            enemyTime = 0;
        }
        //updating the score text
        txtScore.text = 'Score: '+score;

        if (gameOver == true)
        {
            loadScreenThree();
        }

    }

    function generateParticles(event:Event):void
    {
        //so we don't do it every frame, we'll do it randomly
        if(Math.random()*10 < 2){
            //creating a new shape
            var mcParticle:Shape = new Shape(); 
            //making random dimensions (only ranges from 1-5 px)
            var dimensions:int = int(Math.random()*5)+1;
            //add color to the shape
            mcParticle.graphics.beginFill(0x999999/*The color for shape*/,1/*The alpha for the shape*/);
            //turning the shape into a square
            mcParticle.graphics.drawRect(dimensions,dimensions,dimensions,dimensions);
            //change the coordinates of the particle
            mcParticle.x = int(Math.random()*stage.stageWidth);
            mcParticle.y = -10;
            //adding the particle to stage
            particleContainer.addChild(mcParticle);
        }

        //making all of the particles move down stage
        for(var i:int=particleContainer.numChildren - 1; i>=0; i--){
            //getting a certain particle
            var theParticle:DisplayObject = particleContainer.getChildAt(i);
            //it'll go half the speed of the character
            theParticle.y += speed*.5;
            //checking if the particle is offstage
            if(theParticle.y >= 400){
                //remove it
                particleContainer.removeChild(theParticle);
            }
        }
    }


    /**
     * eventhandler voor als je op de tweede knop klikt
     */
    private function loadScreenThree():void
    {
        // eerst opruimen!
        cleanListeners();

        // dan naar ander scherm
        Project.instance.switchScreen( "derde" );
    }

    private function cleanListeners():void
    {
        stage.removeEventListener(Event.ENTER_FRAME, generateParticles);
        stage.removeEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler);
        stage.removeEventListener(KeyboardEvent.KEY_UP, keyUpHandler);
        stage.removeEventListener(Event.ENTER_FRAME, enterFrameHandler);
    }

}}

出错的地方是eFrame函数中的Enemy Class。这里循环通过Level.as中的数组bulletContainer运行。一个新的变量,它应该包含在bulletContainer循环中的子弹,这里出现错误

package{
//we have to import certain display objects and events
import Level;
import flash.display.MovieClip;
import flash.events.*;
import flash.display.Sprite;

//this just means that Enemy will act like a MovieClip

public class Enemy extends MovieClip
{
    //VARIABLES
    //this will act as the root of the document
    //so we can easily reference it within the class
    private var _root:Object;
    //how quickly the enemy will move
    private var speed:int = 5;
    //this function will run every time the Bullet is added
    //to the stage

    public function Enemy()
    {
        //adding events to this class
        //functions that will run only when the MC is added
        addEventListener(Event.ADDED, beginClass);
        //functions that will run on enter frame
        addEventListener(Event.ENTER_FRAME, eFrame);
    }

    private function beginClass(event:Event):void
    {
        _root = Sprite(root);
    }

    private function eFrame(event:Event):void
    {
        //moving the bullet up screen
        y += speed;
        //making the bullet be removed if it goes off stage
        if(this.y > 400)
        {
            removeEventListener(Event.ENTER_FRAME, eFrame);
            this.parent.removeChild(this);
        }
        //checking if it is touching any bullets
        //we will have to run a for loop because there will be multiple bullets
        for(var i:int = 0;i<Level.instance.bulletContainer.numChildren;i++)
        {
            //numChildren is just the amount of movieclips within 
            //the bulletContainer.
            trace("LOOOOOOOOOP");
            trace(Level.instance.bulletContainer.getChildAt(i));
            //we define a variable that will be the bullet that we are currently
            //hit testing.
            var bulletTarget:Sprite = Level.instance.bulletContainer.getChildAt(i);

            //now we hit test
            if(hitTestObject(bulletTarget))
            {
                //remove this from the stage if it touches a bullet
                removeEventListener(Event.ENTER_FRAME, eFrame);
                this.parent.removeChild(this);
                //also remove the bullet and its listeners
                Level.instance.bulletContainer.removeChild(bulletTarget);
                //bulletTarget.removeListeners();
                //up the score
                Level.instance.score += 5;
            }
        }

        //hit testing with the user
        if(hitTestObject(Level.instance.SpaceAuto))
        {
            //losing the game
            trace("DOOOOOD");
            removeListeners();
            this.parent.removeChild(this);
            this.removeEventListener(Event.ENTER_FRAME, eFrame);
            Level.instance.gameOver = true;
        }
    }
    public function removeListeners():void
    {
        this.removeEventListener(Event.ENTER_FRAME, eFrame);
    }
}}

我已经尝试过制作雪碧或其他任何东西,但我只是不知道如何解决这个问题,所以它会接受子弹,所以我看看它是否能够击中敌人

var bulletTarget:Sprite = Level.instance.bulletContainer.getChildAt(i);

1 个答案:

答案 0 :(得分:0)

getChildAt将返回DisplayObject类型,hitTestObject需要一个DisplayObject类型的参数。

所以在你的Enemy的eFrame函数中尝试这个。

var bulletTarget:DisplayObject = Level.instance.bulletContainer.getChildAt(i);