0

So I'm working on a game and I'm trying to make a Level Selector right now. However I only want players to be able to play on levels that they've beaten. So I have a .js file labeled "ELD.js" and it contains:

    var Levels = 0;

I don't really care how(in aspects of programing type). But I want to be able to change the "Levels" data value every time I win a level. Each level has it's own individual html file and all of the level files are in the same file as the "ELD.js" file.

I've looked for an answer to this but none of them seem to match what I need.

And while I'm on this subject (correct me if I'm wrong) but I can simply test for the "Levels" data value using this right?

    <script type="text/javascript" src="ELD.js"></script>
    <script>
    if(Levels =< X)
    {
         }
    </script>

X simply stands for an undefined number that I will choose later.

4
  • Each level is a different HTML file? So how are you transitioning between levels? Commented Apr 6, 2017 at 20:27
  • If I understand the question correctly, you'd need a ELD.js file for every player. You should use a database instead to keep track of each users progress. Commented Apr 6, 2017 at 20:31
  • @jeroen that makes sense. However the game isn't an online game so I don't see why that would be necessary. Commented Apr 6, 2017 at 20:33
  • @AnanthRao Every time the player reaches the finish line. the script runs a wondow.location string. Commented Apr 6, 2017 at 20:55

1 Answer 1

2

You can build your javascript files as a class and assign that variable to the class for example:

class GameLevel {
    constructor (level) {
        this.level = level;
    }

    get_level () {
        return this.level;
    }

    set_level (level) {
        this.level = level;
    }
}

Then throughout the game you have your object:

var cur_level = 1;
var level = new GameLevel (cur_level);

Then after a game is beaten you simply call

level.set_level (++cur_level)

I believe you can even just say level.level = cur_level + 1;

EDIT

Also to pass you because you want to pass your javascript data to another html file there is also a way to that as well. You can use this logic:

  1. Open the new level in a new tab
  2. Pass the data
  3. Close the current window and focus on the new opened window

This is like a weird way to do it but I belive it can work :) to do this you run the following code:

var newLevel        = window.open (<URL TO HTML FILE>, '_blank');

// passing thejavascript data
newLevel.new_level  = level.get_level ;  // This is the data to pass or you can use the variable you had declared

// focus on new window
newLevel.focus ();

// close current window
window.close ();

Edit

and then in all your html file you can get the value of your new level data using window.new_level When the window is opened it will have the value you passed set.

Sign up to request clarification or add additional context in comments.

4 Comments

so would I place the "class" coding inside of a separate .js file and then call it using <script scr="">?
Check the edit below, it will show you how to open a new window in a tab and passing your javascript variable :) its quite awesome actually
And yes you can have you class be in a seperate file so you can use is in all the other html files.
sure, Please note that using window.open may be considered as a popup so you will have to allow popup in your browser for that specific site. Good luck and have fun :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.