- Home /
Question by
Rukey4 · Feb 19, 2013 at 02:45 PM ·
javascriptupdateif-statements
encapsulating if statement in update
This will most probably have a stupidly easy explanation but I can't get my head around this, I have a piece of code to create a simple GUI for holding a players level and experience:
#pragma strict
var currentXP : int = 0;
var maxXP : int = 500;
var XPtext : GUIText;
var Level : int = 1;
function Update ()
{
XPtext.text = "Level " + Level + " XP " + currentXP + " / " + maxXP;
if(currentXP == maxXP)
//if(Input.GetKeyDown("r"))
//currentXP += 10;
{
LevelUpSystem ();
}
}
function LevelUpSystem ()
{
currentXP = 0;
maxXP = maxXP + 50;
Level ++;
}
I want to know how to incorporate this into the update function properly:
//if(Input.GetKeyDown("r"))
//currentXP += 10;
Thanks!
Comment
Answer by DeBunked · Feb 19, 2013 at 03:00 PM
#pragma strict
var currentXP : int = 0;
var maxXP : int = 500;
var XPtext : GUIText;
var Level : int = 1;
function Update ()
{
XPtext.text = "Level " + Level + " XP " + currentXP + " / " + maxXP;
if(currentXP == maxXP)
{
LevelUpSystem ();
}
else if (Input.GetKeyDown(KeyCode.R))
{
currentXP += 10;
}
}
function LevelUpSystem ()
{
currentXP = 0;
maxXP = maxXP + 50;
Level ++;
}
I take it this is what you're looking for? ( i.e. either leveling up or pressing r to get XP).
I think so, but you really shouldn't use
if (currentXP == maxXP)
use this ins$$anonymous$$d
if (currentXP >= maxXP)