I create a game on the Phaser JS, not the essence. I want to make a table of experience = LVL.

250 experience = +1 lvl
500 experience = +1 lvl

I do not know how to do it right. If I do through if or switch, then while the experience is equal to 250 lvl are added without stopping. How do I add 1 LVL correctly?

In the end, I did this:

if(xp >= 50 && lvl==1){ lvl++; } else if (xp >= 100 && lvl==2){ lvl++; } 

  • And if in the if construct to check not only experience, but also the current level of the player? For example: if(experience >= 250 && lvl < 1) lvl++; - intro94
  • The idea is really cool. And then how? There with 2.3 ... lvl? - Vania Kycher

3 answers 3

the table is the wrong way, if the experience rises every 250, then you can divide the current experience by 250 and get a level, if the growth is not linear, then the formula will of course have to be slightly complicated. That is, to do something like this

 var xp = 2200; var level = 0; level = parseInt(xp / 250); 

will be 8 level. This approach will simplify the task if there are many levels.

  • This is provided that the required amount of experience is always the same. But as practice shows, in games the required amount of experience is different. I'm not talking about games with unlimited levels. :) But what the vehicle asked, we give it to him. He did not ask us how best to write him the logic of the game. :) - intro94
  • You can refine the formula for any adequate step change, for example, read exponentially - pnp2000
  • I agree. But there is also an inadequate change of step. For example, in my example, the last three values: first +700 exp, and then +600. Here without a table. But I agree that basically, the simpler the code, the better (if the situation allows simplifying the code). :) - intro94
  • there is more design question, to be honest, there should be logic in the growth of levels, I expect this as a player, but as a programmer there is a question of support, imagine a table for 100 levels, and if you need more? as I understand, it is still possible to fix something, when there is 100 megabytes of code, this will become almost unreal. - pnp2000
  • I agree with you both as a player and as a programmer. :) I'm afraid to present a table with 100 levels. It is better to come up with a pattern, with increasing requirements, taking into account the current level. About 100 MB of code - and here you are right. I worked for 2 years with the game engine written by third-party programmers (browser in PHP). But recently I could not stand it and started to write my own from scratch, because I’d be very tired of picking something in the current one or finding the cause of other people's bugs. - intro94

You can do something like this (did not check the correctness of the work, but it should work):

 // текущий уровень var Level = 0; // требования к уровню var LevelRequirements = [250, 500, 1000, 1700, 2300] // проверяем, стоит ли повышать уровень function LevelUp() { verify = false; while(verify == false) if(experience >= LevelRequirements[Level]) Level++; else verify = true; } 

At the very beginning we set the level as zero. We create an array with the requirements of experience. The first value in the array is the requirement for the first lvl, etc. In fact, the key of the first value is '0' , so when checking we use the current level. You start the check function in the right place. In the function itself, a variable is created to check whether the maximum possible LVL is reached. Until it is reached, we launch a check for a higher level.

This example was created with a fixed number of levels. If the number of levels is not limited, then it would be reasonable to remake this example using the step to increase the level, rather than a fixed number of experience.

PS: as Grundy mentioned in the comment to another answer - the function can be removed altogether, and the code can be run from it directly. I brought it to the function, because I have no idea what you are doing and how exactly your game works. :)

     var lvlExpTable = { // lvl: min_exp 'lvl1': 200, 'lvl2': 400, 'lvl3': 800, 'lvl4': 1500, 'lvl5': 2600, 'lvl6': 4150, 'lvl7': 7600, 'lvl8': 13650, 'lvl9': 20120, 'lvl10': 28900 // ... и так далее }; function getMinExp() { return lvlExpTable['lvl' + lvl]; } if (lvlExpTable.hasOwnProperty('lvl' + lvl)) { while (experience >= getMinExp()) { lvl++; } } 

    • will not work: lvl This is the number, and the keys in the object are lvl1 , lvl2 , etc. - Grundy
    • @Grundy already fixed - CJ Suspend
    • and what is the meaning of the first if ? And in this case, the function is somehow not particularly needed, especially since all global variables - Grundy
    • one
      Well, while the code is here for the code, if you remove half, nothing will change, and if you use an array instead of an object, it will also look easier - Grundy
    • one
      Well, someone has to make sure that there are fewer bad answers :-) - Grundy