- Home /
The question is answered, right answer was accepted
can someone translate this to java I've tried a thousand time but couldn't do it
public int maxHealth = 100;
public int curHealth = 100;
public float healthBarLength ;
// Use this for initialization
void Start () {
healthBarLength = Screen.width / 2;
}
// Update is called once per frame
void Update () {
AddjustCurrentHealth(0);
}
void OnGUI() {
GUI.Box(new Rect(10, 10, healthBarLength, 20), curHealth + "/" + maxHealth);
}
public void AddjustCurrentHealth(int adj) {
curHealth += adj;
if(curHealth < 0)
curHealth = 0 ;
if(curHealth > maxHealth)
curHealth = maxHealth;
if(maxHealth < 1)
maxHealth = 1 ;
healthBarLength = (Screen.width / 2) * (curHealth / (float)maxHealth);
}}
I agree with @rasheedqw. If you post your attempt, we will be glad to help you fix up any problems. Here is a link to the syntax differences between the two languages:
http://answers.unity3d.com/questions/12911/what-are-the-syntax-differences-in-c-and-javascrip.html
@robertbu and @amphoterik are right: you should be more specific and tell us what's the problem. But you're lucky: I suspected that the problem is the (float) typecasting and posted an answer with some alternatives.
This is pretty much Java, with a few changes. I believe you mean JavaScript?
sorry this is the part that didnt work public void AddjustCurrentHealth(int adj) { curHealth += adj;
if(curHealth < 0)
curHealth = 0 ;
if(curHealth > maxHealth)
curHealth = maxHealth;
if(maxHealth < 1)
maxHealth = 1 ;
healthBarLength = (Screen.width / 2) * (curHealth / (float)maxHealth);
}}
im new to unity and translated most of it but i kept getting the error
: The best overload for the method 'Playerhealthbar.AddjustCurrentHealth()' is not compatible with the argument list '(int)'.
wasnt sure it i wrote it wrong or what
Answer by aldonaletto · Jul 26, 2013 at 02:41 PM
I suppose that you're having a hard time trying to find the JS equivalent to (float) - well, it doesn't exist! It's used in this C# code to force a float division (it would otherwise be an integer operation, probably resulting in 0 or 1). JS does value type conversion automatically when needed, but this doesn't help in this case because the compiler can't suspect that you want a float division. A simple solution is to declare a temporary float variable and assign the divisor or dividend to it, then use the float in the division - like this:
public var maxHealth: int = 100;
public var curHealth: int = 100;
public var healthBarLength: float;
function Start() {
healthBarLength = Screen.width / 2;
}
function Update() {
AddjustCurrentHealth(0);
}
function OnGUI() {
GUI.Box(Rect(10, 10, healthBarLength, 20), curHealth + "/" + maxHealth);
}
function AddjustCurrentHealth(adj: int) {
curHealth += adj;
if(curHealth < 0)
curHealth = 0 ;
if(curHealth > maxHealth)
curHealth = maxHealth;
if(maxHealth < 1)
maxHealth = 1 ;
// create a float version of maxHealth and use it in the division:
var maxHealthF: float = maxHealth;
healthBarLength = (Screen.width / 2) * (curHealth / maxHealthF);
}
A yet simpler solution would be to rewrite the expression like this:
healthBarLength = 0.5 * Screen.width * curHealth / maxHealthF;
The magic here is done by the 0.5 float constant and the operand order: Screen.width (an int) is multiplied by the float 0.5, resulting in a float value which forces float operations in the next multiplication and, finally, in the division.
Another way to force float conversion of int values is to multiply the value by 1.0 - this doesn't modify the value, but convert it to float:
healthBarLength = (Screen.width / 2) * (curHealth / (1.0 * maxHealthF));
What about parseFloat and parseInt?
(float)maxHealth => parseFloat( maxHealth )
(int)someVar => parseInt( someVar )
(still an excellent answer, upvoted)
@alucardj, parseFloat doesn't require a string argument? The .NET parseFloat method expects a string, but maybe Unity JS has its own implementation (can't test this right now).
Wish I had a technical answer for you, can only say that I've been using this since I started converting C# to uJS, with no problems and the expected result. Here's a test :
#pragma strict
var myFloat : float = 1.725;
function Start()
{
Debug.Log( myFloat );
// 1.725
// UnityEngine.Debug:Log(Object)
// ParseIntAndFloat:Start() (at Assets/ParseIntAndFloat/ParseIntAndFloat.js:7)
Debug.Log( parseInt( myFloat ) );
// 1
// UnityEngine.Debug:Log(Object)
// ParseIntAndFloat:Start() (at Assets/ParseIntAndFloat/ParseIntAndFloat.js:12)
var currHealth : int = 12;
var maxHealth : int = 18;
Debug.Log( currHealth / maxHealth );
// 0
// UnityEngine.Debug:Log(Object)
// ParseIntAndFloat:Start() (at Assets/ParseIntAndFloat/ParseIntAndFloat.js:21)
Debug.Log( parseFloat( currHealth ) / parseFloat( maxHealth ) );
// 0.6666667
// UnityEngine.Debug:Log(Object)
// ParseIntAndFloat:Start() (at Assets/ParseIntAndFloat/ParseIntAndFloat.js:26)
}
That's a good thing to learn - I used to think that parseFloat/Int only accepted string arguments. Thanks for the hint!
thanks i really appreciate the help will try to be more clear on the error next time
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Help Trying to translate cs to js problem with one block of code 2 Answers
Health Help D: 2 Answers