0

basically, I have a function that is supposed to go back to the init() function, where the game starts over, but for some reason, the game decides to ignore everything that was created the first time around and ignore the removeChild() parts of the restart button.

I've had to resort to fscommand("quit"); instead, as the game just breaks apart.

is there ANY way of making the intervals, images and (image) buttons work properly from this?

here's the init section:

private function init(e:Event = null):void
        {

            gameBackground.y = -600; //start position for scrolling animation
            gameLogo.x = (stage.stageWidth/2 - gameLogo.width/2 + 50);
            gameLogo.y = 55;
            gameLogo.scaleX = 0.8;
            gameLogo.scaleY = 0.8;

            startBtn.scaleX = 0.4;
            startBtn.scaleY = 0.4;

            startBtn.x = (stage.stageWidth/2 + 125);
            startBtn.y = 500;
            player.x = (stage.stageWidth - player.width) / 2;

            player.y = startBtn.y - 10;
            addChild(gameBackground);
            addChild(player);
            addChild(gameLogo);
            addChild(startBtn);
            addEventListener(MouseEvent.MOUSE_OVER, buttonHover);
            addEventListener(MouseEvent.MOUSE_OUT, buttonHoverOff);
            addEventListener(MouseEvent.CLICK, startgame);
            trace("event listener added");
        }

        public function buttonHover(e:Event):void{
            if (e.target == startBtn)
            {
                startBtn.x = (stage.stageWidth/2 + 128);
                startBtn.y = 504;
                startBtn.scaleX = 0.395;
                startBtn.scaleY = 0.395;
                mouseClick.play();
            }
        }

        public function buttonHoverOff(e:Event):void{
            if (e.target == startBtn)
            {
                startBtn.x = (stage.stageWidth/2 + 125);
                startBtn.y = 500;
                startBtn.scaleX = 0.4;
                startBtn.scaleY = 0.4;
            }
        }

        public function btnMovement():void
        {
            startBtn.y += 10;
        }
        //this function removes all objects for the start menu and goes into the game
        public function startgame(e:Event):void
        {
            if (e.target == startBtn)
            {
                removeEventListener(MouseEvent.MOUSE_OUT, buttonHoverOff);
                mouseHover.play();
                startMove = setInterval(startGameAnimation, 16.6);
                planeTakeOff.play();
                var btnMove:uint = setInterval(btnMovement, 16.6);
                if (startBtn.y > stage.stageHeight + 20) {
                    startBtn.y += 0;
                    startBtn.visible = false;
                    clearInterval(btnMove);
                }
                //game();
                //trace("objects removed.  game starting...");
            }
            trace("remove click found");
        }

here's the restart section:

public function endScreen():void
        {
            //removeEventListener(Event.EXIT_FRAME, endLoad);



            ENDScore.text = "Score: " + String(scoreBar);

            ENDScore.width = 200;
            var endFormating:TextFormat = new TextFormat();
            endFormating.size = 25;
            endFormating.font = "Consolas";
            endFormating.color= 0xFFFFFF;
            ENDScore.x = stage.stageWidth/2 - ENDScore.width/2 + 10;
            ENDScore.y = 415;
            ENDScore.setTextFormat(endFormating);
            endBtn.x = 660;
            endBtn.y = 460;
            endBtn.scaleX = 0.9;
            endBtn.scaleY = 0.9;
            addChild(endScr);
            addChild(ENDScore);
            addChild(endBtn);
            closeDetect();
            //trace("button");
            //trace(scoreBar);
        }

        public function closeDetect(e:Event = null):void {

            trace("button");
            endBtn.addEventListener(MouseEvent.CLICK, closeGame);
        }

        public function closeGame(e:Event):void
        {
            //trace("button found");
            //
                //trace("button clicked");
                ////endBtn.visible = false;
                //removeChild(enemyFighter);
                //removeChild(enemyFighter2);
                //removeChild(enemyFighter3);
                //removeChild(enemyFighter4);
                //removeChild(enemyFighter5);
                //removeChild(lMass1);
                //removeChild(lMass2);
                //removeChild(player);
                ////removeChild(bullet);
                //removeChild(endScr);
                //removeChild(endBtn);
                //removeChild(ENDScore);
                //
                //playerDeath = false;
                //thePlayerDies = 3;
                //scoreBar = 0;
                //removeEventListener(MouseEvent.CLICK, closeGame);
                ////init();
                //if (stage) init();
                //else addEventListener(Event.ADDED_TO_STAGE, init); 
                fscommand("quit");

        }
  • Make it a separate **MovieClip** with linked class, so when you want a new game, you create a new instance of that class and attach to stage, rather then clean-up and reset the existing one. – Organis Apr 08 '19 at 13:22
  • @Organis thanks for the feedback! I did attempt your method, but for some reason, I got the same error again, but in a different area _(the function leading into the restart)_ I decided after two attempts at fixing your method to convert the restart button to an exit button instead. Thank you for the help though! _(I'm new to AS3 as of Oct 2018)_ – KazMack96 Apr 08 '19 at 19:14

0 Answers0