vendredi 13 décembre 2019

Can I reduce these lines of code make double key press to switch between two of if/else if options

I would like to reduce this to the fewest possible lines of code.

This using C# code in Unity, but the question is more about basic refactoring. There is no timer yet, I'm simply trying to write code print timer started/timer stopped to the Unity console.

void Update()
    {
        pressSpacebar();
    }

void pressSpacebar()
    {
        if (Input.GetKeyDown("space"))
        {
            if(!timerIsRunning)
            {
                StartTimer();
            }
            else if (timerIsRunning)
            {
                StopTimer();
            }
        }
    }

    bool timerIsRunning;

    void StartTimer()
    {
        timerIsRunning = true;
        print("Timer Running");
//to check if bool changed (and not just the text)
        print(timerIsRunning);
    }

    void StopTimer()
    {
        timerIsRunning = false;
        print("Timer Stopped");
        print(timerIsRunning);

    }

Aucun commentaire:

Enregistrer un commentaire