Function - Javascript Global Variables Are Resetting

De openkb
Aller à : Navigation, rechercher

Sommaire

Questions

SUMMARY OF WHAT I"M TRYING TO DO: I ve put together a very simple game that uses a do/while function and switch to check what room you are in. If you are in room = 1, the switch selects room1 and runs the room1() function. Inside the room, you can choose which direction to go. If you choose north or east, a door1() or door4() function will run to say "Would you like to open this door?" You can say "Open" and walk into the next room, and your room = 1 will update to a new value.

WHAT IS BROKEN: This is all working beautifully and the functions (while they are somewhat bloated) appear to be working as they should. The one major issue is that my variables are resetting whenever I walk through a door, so I am always in room 1, my compass always equals 0 and none of the doors or rooms equal "visited" (example: room1V = 0).

Basically, I walk through a door and I end up in room 1 again, instead of room 2 or 4.

This is the code, and a walkthrough:

    var room1V = 0; //room1  // these variables tell the computer whether I have  visited  a room before.
    var room2V = 0; //room2
    var room3V = 0; //room3
    var room4V = 0; //room4

    var door1V = 0; //room1 - room2 // these variables tell the computer whether I have used a door before.
    var door2V = 0; //room2 - room3
    var door3V = 0; //room3 - room4
    var door4V = 0; //room4 - room1

    var compass = 0; // which side of the room am I on?
    var room = 1; // what room am I currently in?
    var reply = 1; // this is re-declared as a local variable in each function, and it works fine
    win = 0; // this will eventually tell the room-check do/while to stop

    // This do/while checks what room I am in:
    do {
        quit = 0;
        switch(room) {
            case  1 :
                room1(compass,room1V);
                break;
            case  2 :
                room2(compass,room2V);
                break;
            case  3 :
                room3(compass,room3V);
                break;
            case  4 :
                room4(compass,room4V);
                break;
        }
    } while (win != 1);

Since the default says room = 1, the room1(compass,room1V) function will start.

    function room1(compass,room1V) {
        if (room1V === 1) {
            console.log("You are in room 1 again.");
            document.write("You are in room 1 again." + "<br>");
            var reply = prompt("Where would you like to go? EAST, NORTH?");
            switch(reply.toLowerCase()) {
                case  east :
                    compass = "east"; //because you are trying to open the east door, you will now see room1 from the east. If you make it through, your compass will update to  west  because you will be on the west side of room2.
                    door1(compass,door1V);
                    break;
                case  north :
                    compass = "north";
                    door4(compass,door4V);
                    break;
                default:
                    console.log("Something went wrong.");
                    document.write("Something went wrong." + "<br>");
            }
        } else {
            console.log("You are in room 1.");
            document.write("You are in room 1." + "<br>");
            room1V = 1;
            reply = prompt("Where would you like to go? EAST, NORTH?");
            switch(reply.toLowerCase()) {
                case  east :
                    compass = "east";
                    door1(compass,door1V);
                    break;
                case  north :
                    compass = "north";
                    door4(compass,door4V);
                    break;
                default:
                    console.log("Something went wrong.");
                    document.write("Something went wrong." + "<br>");
            }
        }
    }   // Working

If I go north, the compass will update to say what direction I am viewing the room from, and door4(room,door4V) will run.

    function door4(room,door4V) {
        if (door1V === 1) {
            console.log("You approach door 4 again.");
            document.write("You approach door 4 again." + "<br>");
            var reply = prompt("What would you like to do? OPEN, QUIT?");
            switch(reply.toLowerCase()) {
                case  open :
                    if (room === 4) {
                       room = 1;
                       compass = "north";
                       console.log("You walk through door 4 into room 1...");
                       document.write("You walk through door 4 into room 1..." + "<br>");
                    } else {
                       room = 4;
                       compass = "south";
                       console.log("You walk through door 4 into room 4...");
                       document.write("You walk through door 4 into room 4..." + "<br>");
                       }
                    quit = 1;
                    break;
                case  quit :
                    quit = 1;
                    break;
                default:
                    console.log("Something went wrong.");
                    document.write("Something went wrong." + "<br>");
            }
        } else {
            console.log("You approach door 4.");
            document.write("You approach door 4." + "<br>");
            var reply = prompt("What would you like to do? OPEN, QUIT?");
            switch(reply.toLowerCase()) {
                case  open :
                    if (room === 4) {
                       room = 1;
                       compass = "north";
                       console.log("You walk through door 4 into room 1...");
                       document.write("You walk through door 4 into room 1..." + "<br>");
                    } else {
                       room = 4;
                       compass = "south";
                       console.log("You walk through door 4 into room 4...");
                       document.write("You walk through door 4 into room 4..." + "<br>");
                    }
                    quit = 1;
                    break;
                case  quit :
                    quit = 1;
                    break;
                default:
                      console.log("Something went wrong.");
                      document.write("Something went wrong." + "<br>");
            }
        }
    }       // Working

At this point, the do/while function is supposed to say, "Oh! Since room = 4, you are now in room 4." But that isn t what happens. Room does = 4, but when the do/while re-runs, I am back in room1, and all the variables appear to have reset.

Answers

http://docstore.mik.ua/orelly/webprog/jscript/ch11_02.htm http://docstore.mik.ua/orelly/webprog/jscript/ch11_02.htm

Source

License : cc by-sa 3.0

http://stackoverflow.com/questions/34549525/javascript-global-variables-are-resetting

Related

Outils personnels
Espaces de noms

Variantes
Actions
Navigation
Outils