- Home /
Limit An XP Bar Length?
My question is almost completely what the title says. I am wondering how I would limit the length of my xp bar to a certain amount. The best example of what I want to do would be Mincraft's xp bar. Any ideas?
static var Kills : int = 0;
static var Money : int = 250;
static var Lives : int = 10;
var BasicTurret : Transform;
var Paused : boolean = false;
static var curExp : int;
var maxExp : int = 50;
var expTexture : Texture;
var level : int;
function LateUpdate()
{
}
function Update()
{
if(Input.GetKeyDown(KeyCode.P)&& !Paused)
{
Paused = true;
Time.timeScale = 0;
}
else if(Input.GetKeyDown(KeyCode.P)&& Paused)
{
Paused = false;
Time.timeScale = 1;
}
if(Input.GetKeyDown(KeyCode.Alpha1))
{
Time.timeScale = 1;
}
if(Input.GetKeyDown(KeyCode.Alpha2))
{
Time.timeScale = 2;
}
if(Input.GetKeyDown(KeyCode.Alpha3))
{
Time.timeScale = 3;
}
if(Lives <= 0)
{
Lives = 0;
Loose();
}
if(curExp > maxExp)
{
curExp = 0;
maxExp = maxExp * 1.5;
level++;
}
}
function OnGUI()
{
GUI.Box(Rect(Screen.width / 2 - 60, 10, 60, 20), "" + Kills);
GUI.Box(Rect(Screen.width / 2, 10, 60, 20), "" + Lives);
GUI.Box(Rect(10, 10 , 60, 20), "$" + Money);
if(GUI.Button(Rect(10, 40, 40, 40), "Basic Turret"))
{
Money -= 350;
}else{
}
if(Paused)
{
if(GUI.Button(Rect(Screen.width / 2 - 30, Screen.height / 2 - 10, 60, 20), "Exit"))
{
Application.LoadLevel(0);
}
if(GUI.Button(Rect(Screen.width / 2 - 30, Screen.height / 2 + 20, 60, 20), "Quit"))
{
Application.Quit();
}
}
GUI.Box(Rect(125, Screen.height - 35, curExp, 20), curExp + "/" + maxExp + " Level: " + level);
}
function Loose()
{
Application.LoadLevel(0);
Time.timeScale = 1;
Kills = 0;
Money = 250;
Lives = 10;
}
Like a GUI element on the screen? Post what you've got so far.
What is the maximum maxExp value? Just add an "if" comparing the atual "maxExp" value with "maxExpTotal" value.
that is the total amount before the bar resets. but how do i limit how big the bar gets? and how do i make it so that while the total exp would be below that, it calculates how many pixels each exp amount would need to be to fill that space?
Answer by Major · Oct 09, 2012 at 02:58 AM
I found the answer to be very simple. All I had to do was edit line 87 to be this:
GUI.Box(Rect(125, Screen.height - 35, (curExp*500)/maxExp, 20), curExp + "/" + maxExp + " Level: " + level);
That is just another way of writing the suggestions from myself and ausiemick. But I'm glad you understand the concept.
Answer by williampigmeu · Oct 08, 2012 at 01:08 AM
There is some code "demo":
if (xpBarLength >= 10) { // "10" can be anything you want level += 1; xpBarLength = 0; }
Answer by rhys_vdw · Oct 08, 2012 at 03:45 AM
You can "normalize" your XP and multiply it by a value, this puts it in a range between 0 and 1. something like this:
var xp : float;
var maxXp : float;
var xpBarFullRect : Rect;
function OnGUI() {
var normalizedXp = xp / maxXp;
var xpBarRect = xpBarFullRect;
xpBarRect.width *= normalizedXp;
GUI.Box(xpBarRect, xp + "/" + maxXp);
}
Fixed an error.
I haven't tested it yet, but I hope you can follow my logic. If it's not clear I'll try to break it down for you.
Answer by ausiemick · Oct 08, 2012 at 07:40 AM
Pretty much what you need todo is normalise the xp (normalisation is turning a range as such as 0 -> 40 to 0 -> 1...
the formula for that is:
function normalise(minVal : float, maxVal : float, currValue : float)
{
return((currValue-minVal) / (maxVal-minVal))
}
Please keep in mind that I never code in javascript + I coded this in a browser.. so I can almost guarentee my code won't work.. but if u can read you should be able to translate my code into javascript (if you had of asked for C# ide been able to write 100% bug free code :P) and also, I havn't tested this, this function is based purely on theory..
say we have a xp scale from 0 -> 100 and curr xp == 54 get currValue and take min value from it 54 - 0 = 54 get the max value and take the min value from it 100 - 0 = 100 then we divide the new curr value by the new max value
54/100
which gives us 0.54
Then what you do is define the screenspace for the box
a--------------------b
c--------------------d
then when you draw the box you would give it this:
float normalisedXP = normalise(0, 100, 54);
GUI.Box(Rect(a.x, a.y, (b-a)*normalisedXP, c-a), "" + XP);
where a, b, c, d are defined Vector2's who correlate to the size of how big you want the xp bar once it's finished