0

/質問/

私はこれを約1週間使用していますが、まだ問題を見つけることができません。trace ステートメントを使用しましたが、これかそれかわかりません。私自身まだまだ未熟者ですので。私はフラッシュ現像を使用しています。ありがとう。

/* これは私が取得しているエラーです。ダンゴが破壊されたら削除し、ゲーム終了後にもう一度削除しようとしています */

TypeError: Error #1009: Cannot access a property or method of a null object reference.
    at mcDango/destoryDango()[J:\catchMeDango\mcDango.as:72]
    at catchMeDango/checkEndGameCondition()[J:\catchMeDango.as:195]
    at catchMeDango/gameLoop()[J:\catchMeDango\catchMeDango.as:171]

---------------------------------------------------------------------------------------

/*これはcatchMeDangoドキュメント用です*/

package  
{
    import flash.display.Loader;
    import flash.display.MovieClip;
    import flash.events.Event;
    import flash.events.KeyboardEvent;
    import flash.events.TimerEvent;
    import flash.net.URLRequest;
    import flash.text.TextField;
    import flash.utils.Timer;

    /**
     * ...
     * @author 
     */
    public class catchMeDango extends MovieClip 
    {
        public var skewerPlayer:MovieClip;

        private var leftKeyIsDown:Boolean;
        private var rightKeyIsDown:Boolean;
        private var upKeyIsDown:Boolean;
        private var downKeyIsDown:Boolean;

        private var aDangoArray:Array;
        private var aBlackDangoArray:Array;

        public var scoreTxt:TextField;
        public var lifeTxt:TextField;
        public var menuEnd:mcEndGameScreen;

        private var nScore:Number;
        private var nLife:Number;
        private var tDangoTimer:Timer;
        private var tBlackDangoTimer:Timer;
        private var menuStart:mcStartGameScreen;
        private var startHow:Loader;

        public function catchMeDango() 
        {   
            //hides the endGameScreen
            menuEnd.hideScreen();

            //Create loader object
            var startLoader:Loader = new Loader();
            //Add event listener to listen the complete event
            startLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, startLoaded);
            //Load our loader object
            startLoader.load(new URLRequest("startGameScreen.swf"));
        }

        private function startHowToPlay(e:Event):void 
        {
            //Create loader object
            startHow = new Loader();
            //Add event listener to listen the complete event
            startHow.contentLoaderInfo.addEventListener(Event.COMPLETE, HowToPlay);
            //Load our loader object
            startHow.load(new URLRequest("howToPlay.swf"));
        }

        private function HowToPlay(e:Event):void 
        {
            //add the how to play movie clip to stage
            addChild(startHow);
            startHow.addEventListener("BEGIN_GAME", playGameAgain);
        }

        private function startLoaded(e:Event):void 
        {
            //Get a reference to the loaded movieclip
            menuStart = e.target.content as mcStartGameScreen;
            //Listen for start game event
            menuStart.addEventListener("START_GAME", playGameAgain);
            menuStart.addEventListener("HOW_TO_PLAY", startHowToPlay);
            //Add to stage
            addChild(menuStart);
        }

        private function playGameAgain(e:Event):void 
        {   
            //I had to remove this function becuase is was causing errors
            //When I click on the "how to play" then go back to the start screen, it hides fine with no issues
            //startHow.visible = false;
            menuStart.visible = false;

            //set keyboard control to false
            leftKeyIsDown = false; 
            rightKeyIsDown = false;
            upKeyIsDown = false;
            downKeyIsDown = false;

            //Initialize variables
            aDangoArray = new Array();
            aBlackDangoArray = new Array();
            nScore = 0;
            nLife = 1;

            skewerPlayer.visible = true;

            menuStart.hideScreen();

            menuEnd.addEventListener("PLAY_AGAIN", playGameAgain);
            menuEnd.hideScreen();

            updateScoreText();
            updateLifeText();

            //trace ('Catch Me Dango Loaded');
            //Set up listerners for when an key is pressed
            stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDown);
            stage.addEventListener(KeyboardEvent.KEY_UP, keyUp);

            //Set up a game loop listener
            stage.addEventListener(Event.ENTER_FRAME, gameLoop)

            //Create a timer object for Dango for every 1 second
            tDangoTimer = new Timer(1000)
            //Listen for timer ticks/intervals
            tDangoTimer.addEventListener(TimerEvent.TIMER, addDango)
            //Start timer object
            tDangoTimer.start();

            //Create a timer object for Black Dango for every 10 seconds
            tBlackDangoTimer = new Timer(10000)
            //Listen for timer ticks/intervals
            tBlackDangoTimer.addEventListener(TimerEvent.TIMER, addBlackDango)
            //start timer object
            tBlackDangoTimer.start();
        }

        //Display Score Text (Starting form: 0)
        private function updateScoreText():void
        {
            scoreTxt.text = "Score: " + nScore;
        }

        //Display Life Text (Starting from: 5)
        private function updateLifeText():void
        {
            lifeTxt.text = "Life: " + nLife;
        }

        private function addBlackDango(e:TimerEvent):void 
        {
            //create new black dango object
            var newBlackDango:mcBlackDango = new mcBlackDango();
            //Add our new black dango 
            stage.addChild(newBlackDango);
            aBlackDangoArray.push(newBlackDango);
            trace(aBlackDangoArray.length);
        }

        private function addDango(e:TimerEvent):void 
        {
            //create new dango(enemy) object
            var newDango:mcDango = new mcDango();
            //Create enemy to the stage
            stage.addChild(newDango);
            //Add our new dango(enemy) to dango(enemy) array collection
            aDangoArray.push(newDango);
            trace(aDangoArray.length);
        }

        private function gameLoop(e:Event):void 
        {
            playerContorl();
            clampPlayerToStage();
            checkDangosOffScreen();
            checkSkewerHitsDango();
            checkBlackDangosOffScreen();
            checkSkwerHitsBlackDango();
            checkEndGameCondition();
        }

        //HELP
        private function checkEndGameCondition():void 
        {
            //Check if the player has 0 life left
            if (nLife == 0) 
            {
                //Stop player movement
                stage.removeEventListener(KeyboardEvent.KEY_UP, keyUp);
                stage.removeEventListener(KeyboardEvent.KEY_DOWN, keyDown);

                //Hide the player
                skewerPlayer.visible = false;

                //Stop spawning dango and black dango
                tDangoTimer.stop();
                tBlackDangoTimer.stop();

                //Clear the dango currently on screen
                for each(var dango:mcDango in aDangoArray)
                {   
                    //this destory function had to be removed as well. because it was destorying the dango twice when it was already removed from the stage.
                    //by commenting out this, it was able to destory and remove without any errors and only doing it once.
                    //Destory the dango the were curretly up to in the for loop
                    dango.destoryDango();
                    trace("black dango removed")
                    //Remove from dango from dango array
                    aDangoArray.splice(0, 1); 
                }

                //Clear the black dango currently on screen
                for each(var blackDango:mcBlackDango in aBlackDangoArray)
                {
                    //Destory the dango tat were curretly up to in the for loop
                    blackDango.destoryBlackDango();
                    //Remove from dango from dango array
                    aBlackDangoArray.splice(0, 1); 
                }

                //Stop the game loop
                if (aDangoArray.length == 0 && aBlackDangoArray.length == 0)
                {
                stage.removeEventListener(Event.ENTER_FRAME, gameLoop);
                }

                //Show end game screen
                menuEnd.showScreen();
            }
        }

        // Checks if the Skewer Hits or makes conatct with the Black Dango
        private function checkSkwerHitsBlackDango():void 
        {
            //Loop through our current Black dango
            for (var i:int = 0; i < aBlackDangoArray.length; i++)
            {
                //Get our current Black Dango in the loop
                var currentBlackDango:mcBlackDango = aBlackDangoArray[i];

                //Test if our skewer is hitting the black dango
                if (skewerPlayer.hitTestObject(currentBlackDango))
                {
                    //remove the current dango from stage
                    currentBlackDango.destoryBlackDango()
                    //Remove the dango from out dango array
                    aBlackDangoArray.splice(i, 1);

                    //Minus one to our score
                    nScore--;
                    updateScoreText();

                    //Minus one to our life
                    nLife--;
                    updateLifeText();
                }   
            }
        }

        // Remove the black dango of the stage and from the array
        private function checkBlackDangosOffScreen():void 
        {
            //loop through all our black dango
            for (var i:int = 0; i < aBlackDangoArray.length; i++)
            {
                //Get our current dango in the loop
                var currentBlackDango:mcBlackDango = aBlackDangoArray[i];
                //When dango starts on the top AND our curent enemy has gone past the bottom side if the stage
                if (currentBlackDango.y > (stage.stageHeight + currentBlackDango.y / 2))
                {
                    //Remove black dango (enemy) from array
                    aBlackDangoArray.splice(i, 1);
                    //Remove black dango (enemy) from stage
                    currentBlackDango.destoryBlackDango();
                }
            }
        }

        // Checks if the Skewer Hits or makes conatct with the Dango
        private function checkSkewerHitsDango():void 
        {
            //Loop through all our current dango
            for (var i:int = 0; i < aDangoArray.length; i++)
            {
                //Get our current dango in the loop
                var currentDango:mcDango = aDangoArray[i];

                //Test if our skewer is hitting dango
                if (skewerPlayer.hitTestObject(currentDango))
                {
                    //remove the current dango from stage
                    currentDango.destoryDango()
                    //Remove the dango from out dango array
                    aDangoArray.splice(i, 1);

                    //Add one to our score
                    nScore++;
                    updateScoreText();
                }

            }

        }

        // Remove the dango of the stage and from the array
        private function checkDangosOffScreen():void 
        {
            //loop through all our dangos(enemies)
            for (var i:int = 0; i < aDangoArray.length; i++)
            {
                //Get our current dango in the loop
                var currentDango:mcDango = aDangoArray[i];

                //When dango starts on the top AND our curent enemy has gone past the bottom side if the stage
                if (currentDango.y > (stage.stageHeight + currentDango.y / 2))
                {
                    //Remove dango (enemy) from array
                    aDangoArray.splice(i, 1);
                    //Remove dango (enemy) from stage
                    currentDango.destoryDango();
                } 
            }   

        }

        private function clampPlayerToStage():void 
        {
            //If our player is to the left of the stage
            if (skewerPlayer.x < skewerPlayer.width/2)
            {

                //Set our player to left of the stage
                skewerPlayer.x = skewerPlayer.width/2;
            } else if (skewerPlayer.x > (stage.stageWidth - (skewerPlayer.width / 2)))

            //If our player is to the right of the stage
            {

                //Set our player to right of the stage
                skewerPlayer.x = stage.stageWidth - (skewerPlayer.width / 2);
            }

            //If our player is to the top(up) of the stage
            if (skewerPlayer.y  < 0)
            {

                //Set our player to the top(up) of the stage
                skewerPlayer.y = 0;
            } else if (skewerPlayer.y > (stage.stageHeight - (skewerPlayer.height / 1)))

            //If our player is to the bottom(down) of the stage
            {

                //Set our player to the bottom(down) of the stage
                skewerPlayer.y = stage.stageHeight - (skewerPlayer.height / 1);
            }
        }

        // right, left, top and bottom Key Functions
        private function playerContorl():void 
        {
            //if our left key is currently down
            if (leftKeyIsDown == true)
            {

                //move our player to the left
                skewerPlayer.x -= 5;
            }
            //if our right key is currently down
            if (rightKeyIsDown) 
            {

                //move our player to the right
                skewerPlayer.x += 5;
            }
            //if our up key is currently down
            if (upKeyIsDown) 
            {

                //move our player to the top
                skewerPlayer.y -= 5;
            }
            //if our down key is currently down
            if (downKeyIsDown) 
            {

                //move our player to the bottom
                skewerPlayer.y += 5;
            }

        }

        private function keyUp(e:KeyboardEvent):void 
        {   
            //if our left key is released
            if (e.keyCode == 37)
            {
                //left key is released  
                leftKeyIsDown = false;
            }
            //if our right key is released
            if (e.keyCode == 39)
            {
                //right key is released
                rightKeyIsDown = false;
            }
            //if our up key is released
            if (e.keyCode == 38)
            {
                //up key is released    
                upKeyIsDown = false;
            }
            //if our down key is released
            if (e.keyCode == 40)
            {
                //down key is released
                downKeyIsDown = false;
            }
        }

        private function keyDown(e:KeyboardEvent):void
        {
            //if our left key is pressed
            if (e.keyCode == 37)
            {
                //left key is pressed   
                leftKeyIsDown = true;
            }
            //if our right key is pressed
            if (e.keyCode == 39)
            {
                //right key is pressed
                rightKeyIsDown = true;
            }   
            //if our up key is pressed
            if (e.keyCode == 38)
            {
                //up key is pressed 
                upKeyIsDown = true;
            }
            //if our down key is pressed
            if (e.keyCode == 40)
            {
                //right key is pressed
                downKeyIsDown = true;
            }   
        }

    }

}

---------------------------------------------------------------------------------

/*これは mcDango ドキュメント用です */

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

    /**
     * ...
     * @author 
     */
    public class mcDango extends MovieClip 
    {
        private var nSpeed:Number;
        private var nRandom:Number;

        public function mcDango() 
        {
            // listen for when the Dango is added to the stage
            addEventListener(Event.ADDED_TO_STAGE, onAdd);
        }

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

        private function init():void 
        {   //Number of frames you have in Flash
            var nDango:Number = 12;
            //Pick random number between 1 and our number of enemies(dango)
            var nRandom:Number = randomNumber(1, nDango)
            //Setup our player of this enemey(dango) clip to pur random dango
            this.gotoAndStop(nRandom);
            //Setup our enemies(dango) start position
            setupStartPosition();
        }

        private function setupStartPosition():void 
        {
            //Pick a random speed for enemy(dango)
            nSpeed = randomNumber(5, 10);

            //Set your ramdon X postion
            nRandom = randomNumber(9, 775);

            //Start our enemy(dango) on the top side
            this.y = -50;

            //set random pos X
            this.x = nRandom

            //move our enmey(dango)
            startMoving();
        }

        private function startMoving():void 
        {
            addEventListener(Event.ENTER_FRAME, dangoLoop)
        }

        private function dangoLoop(e:Event):void 
        {
            //Test what direction our enemy(dango) is moving in
            //If our enemy(dango) is moving right

            //Move our enemy(dango) down
            this.y += nSpeed
        }

        public function destoryDango():void
        {
            //Remove dango from stage
            parent.removeChild(this);
            trace("the dango has been removed")
            //Remove any event listeners in our enemy object
            removeEventListener(Event.ENTER_FRAME, dangoLoop);
        }

        function randomNumber(low:Number=0, high:Number=1):Number
        {
            return Math.floor(Math.random() * (1+high-low)) + low;  
        }
    }
}
4

2 に答える 2

0

問題は、parent.removeChild メソッドにアクセスしようとしたときのようです。すでにステージから削除していて、親がなくなっている可能性があります。これを試してください:

    public function destoryDango():void
    {
        //Remove dango from stage
        if(parent != null)
            parent.removeChild(this);
        trace("the dango has been removed")
        //Remove any event listeners in our enemy object
        removeEventListener(Event.ENTER_FRAME, dangoLoop);
    }
于 2013-12-05T03:32:25.377 に答える
0

コードをちらりと見ると、問題は次の for-each-in ループにあるように見えます

for each(var dango:mcDango in aDangoArray) {   
//this destory function had to be removed as well. because it was destorying the dango twice when it was already removed from the stage.
//by commenting out this, it was able to destory and remove without any errors and only doing it once.
//Destory the dango the were curretly up to in the for loop
dango.destoryDango();
trace("black dango removed")
//Remove from dango from dango array
aDangoArray.splice(0, 1);
}

を使ってチェック

while(aDangoArray.length > 0) {
var dango = aDangoArray[0]
dango.destoryDango();
trace("black dango removed")
aDangoArray.splice(0, 1);
}
于 2012-07-06T11:43:49.300 に答える