1 Replies - 770 Views - Last Post: 28 March 2013 - 01:35 PM Rate Topic: -----

#1 TrewSx  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 50
  • Joined: 07-June 12

OnGUI and Event.current

Posted 28 March 2013 - 11:58 AM

Hello i am trying to reduce healt and mana while pressing 'H'. I know that for each event in GUI i need to use Event.current instead of Input.GetKeyDown since this is GUI event. Here is the code, when i am pressing H nothing is happening it should reduce hp and sp but it does not. Anyone could tell me why is that please ?

#pragma strict


var curHp 	 : float = 100.0;
var maxHp	 : float = 100.0;

var curSp	 : float = 50.0;
var maxSp 	 : float = 50.0; 


var hpBarTexture : Texture2D;
var spBarTexture : Texture2D;

var hpBarlength  : float;
var percentOfHp  : float;


var spBarLength : float;
var percentOfSp : float;


private var _hpSpWindowRect				:    Rect = new Rect(10,Screen.height / 2 + 150,190,70);
private var _hpSpWindowID				:    int   = 1;


function OnGUI () {
	var e = Event.current;
	
	if (e.type == EventType.KeyDown && e.keyCode == KeyCode.H) {
	ReduceHelathAndMana();
}	
	_hpSpWindowRect = GUI.Window(_hpSpWindowID, _hpSpWindowRect, HpSpWindow, "Health points  /  Spell points");

}


function ReduceHelathAndMana() {
			
	percentOfHp = curHp/maxHp;
	hpBarlength = percentOfHp*100;

	percentOfSp = curSp/maxSp;
	spBarLength = percentOfSp*100;
}

function HpSpWindow (_hpSpWindowID) {
	
		
	if (curHp > 0) 
	{
		GUI.Box (new Rect (20,20,150,20),hpBarTexture);
	}
	
	if (curSp > 0) 
	{
		GUI.Box (new Rect (20,40,150,20),spBarTexture);
	}	
	GUI.DragWindow();
	
}



Is This A Good Question/Topic? 0
  • +

Replies To: OnGUI and Event.current

#2 TrewSx  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 50
  • Joined: 07-June 12

Re: OnGUI and Event.current

Posted 28 March 2013 - 01:35 PM

Solved

#pragma strict


var curHp 	 : float = 100.0;
var maxHp	 : float = 100.0;

var curSp	 : float = 50.0;
var maxSp 	 : float = 50.0; 


var hpBarTexture : Texture2D;
var spBarTexture : Texture2D;

var hpBarlength  : float;
var percentOfHp  : float;


var spBarLength : float;
var percentOfSp : float;


private var _hpSpWindowRect				:    Rect = new Rect(10,Screen.height / 2 + 150,190,70);
private var _hpSpWindowID				:    int   = 1;


function OnGUI () {
	var e = Event.current;
	
	if (e.type == EventType.KeyDown && e.keyCode == KeyCode.H) 
	ReduceHelathAndMana();
	
	_hpSpWindowRect = GUI.Window(_hpSpWindowID, _hpSpWindowRect, HpSpWindow, "Health points  /  Spell points");

}


function ReduceHelathAndMana() {
	
	//curHp -= 10*Time.deltaTime;	
	curHp -= 10;
	percentOfHp = curHp/maxHp;
	hpBarlength = percentOfHp*100;
	
	//curSp -= 10*Time.deltaTime;
	curSp -= 10;
	percentOfSp = curSp/maxSp;
	spBarLength = percentOfSp*100;
}

function HpSpWindow (_hpSpWindowID) {
	
		
	if (curHp > 0) 
	{
		GUI.DrawTexture (new Rect (20,20,hpBarlength,20),hpBarTexture);
	}
	
	if (curSp > 0) 
	{
		GUI.DrawTexture (new Rect (20,40,spBarLength,20),spBarTexture);
	}	
	GUI.DragWindow();
	
}

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1