[This is a tutorial for my AS3 stat library which can be found here. You can find the full documentation here.]
Typically when stats are involved in a game, there are also levels and experience that alter them. This tutorial will teach you how to implement an experience system using the library. When functions and properties are mentioned, they will be linked to their respective location in the documentation.
So let’s say we have a hero who should gain levels by meeting the required experience per level. First we must set up our stat system for the hero:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
var stats:StatSystem = new StatSystem(); // meter stats stats.addStat(new Stat("health", 15, 15)); stats.addStat(new Stat("mana", 20, 20)); // counter stats stats.addStat(new Stat("speed", 3)); stats.addStat(new Stat("strength", 10)); // level and experience stats.addStat(new Stat("level", 1, 100)); stats.addStat(new Stat("experience", 0, 50)); |
The health, mana, speed, and strength stats should be nothing new if you’ve completed the previous tutorials. The level and experience stats are what we are interested in.
The level stat will hold the player’s current level while the experience stat will hold the player’s current experience towards the next level. As you can see in the above code, the player is currently at level one with a max level of 100. Experience is currently at 0 with the requirement of 50 point in order to reach the next level.
Now we need to add one additional detail to the experience stat in order for this to work – the onFull function:
|
1 |
stats.getStat("experience").onFull = levelUp; |
This property takes a function that is executed once the stat’s value meets the max max value, which just so happens to be very useful in occasions like this.
PRO TIP: There is also an onEmpty property that is executed once a stat is equal to zero. This can be useful for when a player’s health runs out!
Here’s how our levelUp function should look:
|
1 2 3 4 5 6 7 8 9 10 11 |
private function levelUp():void { // increase level by one stats.getStat("level").addValue(1); // reset experience to 0 stats.getStat("experience").setValue(0); // calculate the next level's required experience var value:int = stats.getStat("experience").maxValue * (0.20 * stats.getStat("level").value); // add calculated value to the maxValue stats.getStat("experience").addMaxValue(value); } |
The above code block introduces two new functions – setValue and setMaxValue. Each of which sets the respective variable to the value passed in the parameter as opposed to adding via the addValue/addMaxValue functions.
PRO TIP: The setValue function will not allow a new value less than zero or greater than the max value and will execute the onEmpty and onFull functions respectively. Similarly, the setMaxValue function will not allow a new value less than zero, but has no positive limit. It will also scale the base value if it exceeds the new max value.
We’ll use addValue to reset the current experience to 0 and then just add a level-based amount to the max value. Using this method, the required experience for level2 will be 60, level3 will be 84, level4 134, and so on. However, you can use any formula you wish to calculate experience values. You can even use a preset array or vector for this too:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
private var reqExp:Array = new Array(10, 20, 30, 40, 50); ... private function levelUp():void { // increase level by one stats.getStat("level").addValue(1); // reset experience to 0 stats.getStat("experience").setValue(0); // set required experience to the value at the index of 'level' in 'reqExp' stats.getStat("experience").setMaxValue(reqExp[stats.getStat("level").value]); } |
This concludes the experience and level management tutorial!