vendredi 22 juillet 2016

Else statement works even when If is told to return

I'm not quite sure why, but I am unable to get this script to behave itself. I have an if statement followed by an else statement, nested within another if statement with its own else statement, and because the way the script is, the else statement is triggering, no matter what I do. I've tried exiting the function but it doesn't work. I have below, two versions of the script, one version using a switch statement, and one version using just if/else statements. They are in a string of identical functions.

What I want, is, if cost + memorybar.Value is greater than 100, it tells the user that, and turns the toggle back off, then exits the function. What is happening is, it turns the toggle off, then runs the else statement anyway, even when in the switch statement. Now the toggle works in reverse somehow. I've seen similar issues but even the return statement has been unable to solve this problem.

cost = 50;
    switch (power5.isOn) {
    case true:
        if (cost + memorybar.value > 100) {
            error.text = "Exeeds maximum memory";
            power5.isOn = false;
            break;
        } else {
            memorybar.value = memorybar.value + cost;
            break;
        }
    case false:
        memorybar.value = memorybar.value + cost;
        break;
    }



    if (power5.isOn == true) {
        if (cost + memorybar.value > 100) {
            error.text = "Exeeds maximum memory";
            power5.isOn = false;
            return;
        } else
            memorybar.value = memorybar.value + cost;
        return;
    }
    else
        memorybar.value = memorybar.value - cost;

Aucun commentaire:

Enregistrer un commentaire