user1453532
user1453532

Reputation: 11

actionscript 3 new game btn in main menu not working

I have a problem with my game.
When i played level 1 and i return to my main menu, the button new game doesn't work anymore.
Does anyone know what could be the problem?

this is what i have in my main menu as:

package  
{
import flash.display.MovieClip;
import flash.events.MouseEvent;
import HomeTitel;
import InstBtn;
import NieuwBtn;    

public class Hoofdmenu extends MovieClip 
{
    private var homeTitel:HomeTitel;
    private var instBtn:InstBtn;
    private var nieuwBtn:NieuwBtn;
    private var inst:Instructions;
    private var level1:Level1;

    public function Hoofdmenu():void 
    {
        placeHomeTitel();
        placeInstructionsBtn();
        placeNieuwBtn();
    }

    private function placeHomeTitel():void
    {
        homeTitel = new HomeTitel();
        addChild(homeTitel);
        homeTitel.x = 275;
        homeTitel.y = 20;
    }

    private function placeInstBtn():void
    {
        instBtn = new InstBtn();
        addChild(instBtn);
        instBtn.x = 275;
        instBtn.y = 225;
        instBtn.addEventListener(MouseEvent.CLICK, gotoInstructions);
    }

    private function gotoInstructions(event:MouseEvent)
    {
        inst = new Instructoins();
        addChild(inst);
    }

    private function placeNewBtn():void
    {
        newBtn = new NewBtn();
        addChild(newBtn);
        newBtn.x = 275;
        newBtn.y = 175;
        newBtn.addEventListener(MouseEvent.CLICK, gotoLevel1);
    }

    private function gotoLevel1(event:MouseEvent):void
    {
        level1 = new Level1();
        addChild(level1);
    }

}

}

this is what i have in my level1 as:

package 
{
import flash.display.MovieClip;
import flash.events.MouseEvent;
import flash.events.Event;
import flash.utils.Timer;
import flash.events.TimerEvent;
import L1Achtergrond;
import L1Titel;
import MenuBtn;
import Sun;
import Min;
import GameOver;
import WellDone;
import VolgLevel;
import HoofdmenuBtn;
import Opnieuw;

public class Level1 extends MovieClip
{
    private var back:L1Achtergrond;
    private var titel:L1Titel;
    private var menu:MenuBtn;
    private var sun:Sun;
    private var aantalSun:int = 5;
    private var counter:int;
    private var sunArray:Array = new Array();
    private var timer:Timer;
    private var min:Min;
    private var gameover:GameOver;
    private var welldone:WellDone;
    private var volglevel:VolgLevel;
    private var opn:Opnieuw;
    private var hoofdBtn:HoofdmenuBtn;
    private var level1:Level1;
    private var level2:Level2;
    private var hoofdmenu:Hoofdmenu;

    public function Level1():void
    {
        back = new L1Achtergrond();
        addChild(back);

        placeTitel();

        timer = new Timer(3000,1);
        timer.addEventListener(TimerEvent.TIMER_COMPLETE, startLevel1);
        timer.start();
    }

    private function placeTitel():void
    {
        titel = new L1Titel();
        addChild(titel);
        titel.x = 275;
        titel.y = 150;
    }

    private function startLevel1(event:TimerEvent):void
    {
        for (counter = 0; counter < aantalSun; counter++)
        {
            sun = new Sun();
            sunArray.push(sun);
            addChild(sun);
            sun.addEventListener(MouseEvent.CLICK, checkSun);
        }

        min = new Min();
        addChild(min);
        min.x = 275;
        min.y = 30;
        min.play();
        min.width = 40;
        min.height = 20;

        timer = new Timer(20000,1);
        timer.addEventListener(TimerEvent.TIMER_COMPLETE, gameOver);
        timer.start();

        menu = new MenuBtn();
        addChild(menu);
        menu.x = 510;
        menu.y = 380;
        menu.addEventListener(MouseEvent.CLICK, gotoHoofdmenu);
    }

    private function checkSun(event:MouseEvent):void
    {
        aantalSun--;

        if (aantalSun == 0)
        {
            wellDone();
            timer.stop();
        }
    }

    public function wellDone():void
    {
        removeChild(menu);
        removeChild(min);

        welldone = new WellDone();
        addChild(welldone);
        welldone.x = 275;
        welldone.y = 150;

        volglevel = new VolgLevel();
        addChild(volglevel);
        volglevel.x = 300;
        volglevel.y = 250;
        volglevel.addEventListener(MouseEvent.CLICK, gotoLevel2);

        hoofdBtn = new HoofdmenuBtn();
        addChild(hoofdBtn);
        hoofdBtn.x = 95;
        hoofdBtn.y = 250;
        hoofdBtn.addEventListener(MouseEvent.CLICK, gotoHoofdmenuW);
    }

    private function gameOver(event:TimerEvent):void
    {
        //timer.stop();

        removeChild(min);
        removeChild(menu);

        for (counter = 0; counter < sunArray.length; counter++)
        {
            removeChild(sunArray[counter]);
        }

        gameover = new GameOver();
        addChild(gameover);
        gameover.x = 275;
        gameover.y = 150;

        opn = new Opnieuw();
        addChild(opn);
        opn.x = 300;
        opn.y = 250;
        opn.addEventListener(MouseEvent.CLICK, level1Opn);

        hoofdBtn = new HoofdmenuBtn();
        addChild(hoofdBtn);
        hoofdBtn.x = 95;
        hoofdBtn.y = 250;
        hoofdBtn.addEventListener(MouseEvent.CLICK, gotoHoofdmenuG);
    }

    private function level1Opn(event:MouseEvent):void
    {
        removeChild(gameover);
        removeChild(opn);
        removeChild(hoofdBtn);
        removeChild(back);

        level1 = new Level1();
        addChild(level1);
    }

    private function gotoHoofdmenu(event:MouseEvent):void
    {
        timer.stop();

        removeChild(min);
        removeChild(menu);
        removeChild(back);

        for (counter = 0; counter < sunArray.length; counter++)
        {
            removeChild(sunArray[counter]);
        }
    }

    private function gotoHoofdmenuW(event:MouseEvent):void
    {
        removeChild(back);
        removeChild(welldone);
        removeChild(hoofdBtn);
        removeChild(volglevel);
    }

    private function gotoHoofdmenuG(event:MouseEvent):void
    {
        removeChild(back);
        removeChild(gameover);
        removeChild(hoofdBtn);
        removeChild(opn);
    }

    private function gotoLevel2(event:MouseEvent):void
    {
        removeChild(back);
        removeChild(volglevel);
        removeChild(hoofdBtn);
        removeChild(welldone);

        level2 = new Level2();
        addChild(level2);
    }

}

}

Upvotes: 0

Views: 359

Answers (2)

RasmusWL
RasmusWL

Reputation: 1593

Seems like you never remove level1 from your menu. Even though you remove all children from Level 1, the movieclip will still exist and be on top of your menu.

I would recommend reading though this tutorial, as it will teach you some basic skills about structuring your code, and specific game development features (sound, preloading, saving things in cookies): http://gamedev.michaeljameswilliams.com/2008/09/17/avoider-game-tutorial-1/

Upvotes: 0

strah
strah

Reputation: 6722

I think you should rebuild/redesign the structure of your game.

Now, your code does few strange things:

  • in your Main class: everytime you call function gotoLevel1 you create a new instance of Level1
  • in your Level1 class in the function level1Opn you create another instance of 'Level1' and you add it inside Level1 - quite a mess.

This isn't just small code tweak - you should rebuild it quite significantly.

Upvotes: 1

Related Questions