• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Help for Game #2025

Community Beginner ,
May 20, 2018 May 20, 2018

Copy link to clipboard

Copied

ArgumentError: Error #2025: The supplied DisplayObject must be a child of the caller.
    at flash.display::DisplayObjectContainer/removeChild()
    at Code::Main/clearGame()
    at Code::Main/triggerGoToLose()
    at Code::Main/update()

1st link is my as3

2nd link is my fla

https://drive.google.com/open?id=15u9J1HwdenhN1u1t1_Jp0-yg327M1tfq

https://drive.google.com/open?id=1V6G2gHAZsokJGmf1o6raZOi26ZxByRVr

Anyone knows how to fix this?

Also there is a problem, my Log is supposed to spawn 5 of them and my car suppose to spawn 4 of them but when displaying, i see only 1 of each and there is a bug where by a secondary log/car is moving faster than the primary 1 and it's spawning continuously after reaching its destination

package Code {
    import flash.display.*;
    import flash.events.*;
    import flash.ui.*;
   
    public class Main extends MovieClip
    {
        private var life, timeElapsed, totalTimer:Number;
        private var p1speedX, p1speedY:Number;
        private var gotoWin, gotoLose, standingOnLog:Boolean;
        private var logs, cars, homes, logsYPos, carsYPos:Array;

        public function Frogger()
        {
           
        }
       
        //All Start Functions
        public function startMenu()
        {
            stop();
            btnStartGame.addEventListener(MouseEvent.CLICK, gotoStartGame);
        }
       
        public function startWin()
        {
            btnBack.addEventListener(MouseEvent.CLICK, gotoMenu);
        }
       
        public function startLose()
        {
            btnBack.addEventListener(MouseEvent.CLICK, gotoMenu);
        }
       
        public function startGame()
        {           
            timeElapsed = 0;
            totalTimer = 99;
            life = 3;
            p1speedX = 0;
            p1speedY = 0;
            gotoWin = false;
            gotoLose = false;
            standingOnLog = false;
            cars = new Array();
            logs = new Array();
            homes = new Array();
            logsYPos = new Array(142,190,240,291,342);
            carsYPos = new Array(547,608,670,730);
           
            setupGame();
           
            //Spawn Cars
            //Row 1
            for (var i=1; i<=2; i++)
            {
                var newCar = new Car();
                newCar.x = -300 * i;
                newCar.y = carsYPos[0];
                newCar.speedX = 5;
                cars.push(newCar);
                addChild(newCar);
            }
           
            //Row 2
            for (var a=1; a<=3; a++)
            {
                var newCar1 = new Car();
                newCar1.x = (170 * a) + 500;
                newCar1.y = carsYPos[1];
                newCar1.speedX = -5;
                cars.push(newCar);
                addChild(newCar);
            }
           
            //Row 3
            for (var b=1; b<=3; b++)
            {
                var newCar2 = new Car();
                newCar2.x = (-220 * b) + 100;
                newCar2.y = carsYPos[2];
                newCar2.speedX = 8;
                cars.push(newCar);
                addChild(newCar);
            }
           
            //Row 4
            for (var c=1; c<=3; c++)
            {
                var newCar3 = new Car();
                newCar3.x = (200 * c) + 350;
                newCar3.y = carsYPos[3];
                newCar3.speedX = -5;
                cars.push(newCar);
                addChild(newCar);
            }
           
            //Spawn Logs
            //Row 1
            for (var d=1; d<=2; d++)
            {
                var newLog = new LogWood();
                newLog.x = -300 * d;
                newLog.y = logsYPos[0];
                newLog.speedX = 5;
                logs.push(newLog);
                addChild(newLog);
                swapChildren(mcP1,newLog);
            }
           
            //Row 2
            for (var e=1; e<=3; e++)
            {
                var newLog1 = new LogWood();
                newLog1.x = (100 * e) + 1075;
                newLog1.y = logsYPos[1];
                newLog1.speedX = -5;
                logs.push(newLog);
                addChild(newLog);
                swapChildren(mcP1,newLog);
            }
           
            //Row 3
            for (var f=1; f<=3; f++)
            {
                var newLog2 = new LogWood();
                newLog2.x = (-100 * f) + 700;
                newLog2.y = logsYPos[2];
                newLog2.speedX = 8;
                logs.push(newLog);
                addChild(newLog);
                swapChildren(mcP1,newLog);
            }
           
            //Row 4
            for (var g=1; g<=3; g++)
            {
                var newLog3 = new LogWood();
                newLog3.x = (250 * g) + 400;
                newLog3.y = logsYPos[3];
                newLog3.speedX = -5;
                logs.push(newLog);
                addChild(newLog);
                swapChildren(mcP1,newLog);
            }
           
            //Row 5
            for (var h=1; h<=3; h++)
            {
                var newLog4 = new LogWood();
                newLog4.x = (350 * i) + 400;
                newLog4.y = logsYPos[4];
                newLog4.speedX = 13;
                logs.push(newLog);
                addChild(newLog);
                swapChildren(mcP1,newLog);
            }
           
            addEventListener(Event.ENTER_FRAME,update);
            stage.addEventListener(KeyboardEvent.KEY_DOWN,keyDownHandler);
            stage.addEventListener(KeyboardEvent.KEY_UP,keyUpHandler);
           
            stage.focus = this;
        }
       
        private function setupGame()
        {
            //Check the classes of the movieclips and push them into the
            //appropriate arrays
            for (var i=0; i< MovieClip(root).numChildren; i++)
            {
                var object = MovieClip(root).getChildAt(i);
               
                if (object is Home)
                {
                    homes.push(object);
                }
            }
        }
       
        //All Goto Functions
        private function gotoStartGame(evt:MouseEvent)
        {
            btnStartGame.removeEventListener(MouseEvent.CLICK, gotoStartGame);
            gotoAndStop("game");
        }

        private function gotoMenu(evt:MouseEvent)
        {
            btnBack.removeEventListener(MouseEvent.CLICK, gotoMenu);
            gotoAndStop("menu");
        }
       
        private function keyDownHandler(evt:KeyboardEvent)
        {
            if (evt.keyCode == Keyboard.A)
            {
                //1st Player Left Key
                p1speedX = -1;
            }
            else if (evt.keyCode == Keyboard.D)
            {
                //1st Player Right Key
                p1speedX = 1;
            }
           
            if (evt.keyCode == Keyboard.W)
            {
                //1st Player forward
                p1speedY = -1;
            }
            else if (evt.keyCode == Keyboard.S)
            {
                //1st Player back Key
                p1speedY = 1;
            }
        }
       
        private function keyUpHandler(evt:KeyboardEvent)
        {
            if ((evt.keyCode == Keyboard.A) || (evt.keyCode == Keyboard.D))
            {
                p1speedX = 0;
            }
            if ((evt.keyCode == Keyboard.W) || (evt.keyCode == Keyboard.S))
            {
                p1speedY = 0;
            }
        }
       
        public function update(evt:Event)
        {
            handleUserInput();
            handleGameLogic();
            handleDraw();
           
            if (gotoWin)
                triggerGoToWin();
            else if (gotoLose)
                triggerGoToLose();
        }
       
        private function handleUserInput()
        {
            //Handle player 1 position
            if (p1speedX > 0)
            {
                if (mcP1.x + 50 < 800)
                    mcP1.x += 50;
               
                p1speedX = 0;
                mcP1.rotation = 90;
                mcP1.play();
            }
            else if (p1speedX < 0)
            {
                if (mcP1.x - 50 > 0)
                    mcP1.x -= 50;   
                p1speedX = 0;
                mcP1.rotation = -90;
                mcP1.play();
            }
           
            if (p1speedY < 0)
            {
                mcP1.y -= 50;
               
                p1speedY = 0;
                mcP1.rotation = 0;
                mcP1.play();
            }
            else if (p1speedY > 0)
            {
                if (mcP1.y + 50 < 600)
                    mcP1.y += 50;   
                p1speedY = 0;
                mcP1.rotation = -180;
                mcP1.play();
            }
        }
       
        private function handleGameLogic()
        {
            timeElapsed++;
           
            //update the cars
            for (var i=cars.length-1; i>= 0; i--)
            {
                cars.x += cars.speedX;
               
                //Check for collision with frog
                if (cars.hitTestPoint(mcP1.x,mcP1.y))
                {
                    life--;
                    resetGame();
                }
               
                if (cars.speedX < 0 && cars.x <= -50)
                {
                    cars.x = 1045;
                }
                else if (cars.speedX > 0 && cars.x >= 1045)
                {
                    cars.x = -50;
                }
            }
           
            //update the logs
            var standingOnLog = false;
            for (var k=logs.length-1; k>= 0; k--)
            {
                logs.x += logs.speedX;
               
                //Check for collision with frog
                if (logs.hitTestPoint(mcP1.x,mcP1.y))
                {
                    standingOnLog = true;
                    mcP1.x += logs.speedX;
                }
               
                if (logs.speedX < 0 && logs.x <= -50)
                {
                    logs.x = 1045;
                }
                else if (logs.speedX > 0 && logs.x >= 1045)
                {
                    logs.x = -50;
                }
            }
           
            //check if frog reaches homes
            for (var j in homes)
            {
                if (homes.hitTestObject(mcP1))
                {
                    homes.gotoAndStop("occupied");
                   
                    mcP1.x = 400;
                    mcP1.y = 565;
                }
            }
           
            //Check if beetle is in water
            if (mcP1.y < 365) //in water zone
            {
                if (!standingOnLog)
                {
                    life--;
                    resetGame();
                }
            }
           
            //Check if timeElapsed or life lost
            if ((totalTimer - Math.floor(timeElapsed/30) <= 0) ||
                (life <= 0))
                gotoLose = true;
           
            //check if all homes are occupied
            var allOccupied = true;
            for (var l in homes)
            {
                if (homes.currentLabel == "empty")
                    allOccupied = false;
            }
            if (allOccupied)
                gotoWin = true;
        }
       
        private function handleDraw()
        {
            //Handle display
            txtTime.text = String(totalTimer - Math.floor(timeElapsed/30));
            txtLife.text = String(life);
        }
       
        private function clearGame()
        {
            //clear away screen items
            for (var i=cars.length-1; i>= 0; i--)
            {
                removeChild(cars);
                cars.splice(i,1);
            }
           
            for (var m=logs.length-1; m>= 0; m--)
            {
                removeChild(logs);
                logs.splice(m,1);
            }
        }
       
        private function triggerGoToWin()
        {
            clearGame();
            removeEventListener(Event.ENTER_FRAME, update);
            gotoAndStop("win");
        }
       
        private function triggerGoToLose()
        {
            clearGame();
            removeEventListener(Event.ENTER_FRAME, update);
            gotoAndStop("lose");
        }
       
        //Misc Functions
        private function resetGame()
        {
            mcP1.x = 501;
            mcP1.y = 725;
        }
    }//end class   
}//end package

TOPICS
ActionScript

Views

297

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , May 21, 2018 May 21, 2018

Hi.

The problems are in the loops for the rows.

Like in this one:

var newCar1 = new Car();

newCar1.x = (170 * a) + 500;

newCar1.y = carsYPos[1];

newCar1.speedX = -5;

cars.push(newCar);

addChild(newCar);

You set a var newCar1 but in the end you push/addChild a newCar var. Fix this in all loops and your code will work.

I hope this helps.

Regards,

JC

Votes

Translate

Translate
Community Expert ,
May 21, 2018 May 21, 2018

Copy link to clipboard

Copied

LATEST

Hi.

The problems are in the loops for the rows.

Like in this one:

var newCar1 = new Car();

newCar1.x = (170 * a) + 500;

newCar1.y = carsYPos[1];

newCar1.speedX = -5;

cars.push(newCar);

addChild(newCar);

You set a var newCar1 but in the end you push/addChild a newCar var. Fix this in all loops and your code will work.

I hope this helps.

Regards,

JC

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines