Don't understand why hasStarted works

Okay so I really don’t understand why the hasStarted thing works in our code.

So I’m declaring in the Start method that hasStarted is false.
In the Update method I’m saying by

 void Update()
     {
        if (hasStarted == false)
         {
             LockBalltoPaddle();
             LaunchOnMouseClick();
         }
     }

that because hasStarted is false do LockBallToPaddle() and LaunchOnMouseClick() as well. So it’s trying to do both of them but it always locks the ball to the paddle so it won’t launch. Why don’t I need something like this?

 void Update()
     {
         if (hasStarted == false)
        {
            LockBalltoPaddle();  
        }
       else LaunchOnMouseClick();
    }

I know that we’re declaring in the LaunchOnMouseClick method that hasStarted is now true, but why does it make the code work? It might be just that I’m tired but I can’t figure it out and it makes me crazy…

Thanks in advance.

EDIT: So to wrap my question up we tell the game what to do if hasStarted is false but we never tell it what to do if it’s true, we just declare it is true in LaunchOnMouseClick() yet it still launches the ball… I don’t get it.

Hi,

Welcome to our community! :slight_smile:

Our idea is the following: When the game starts, we want to have the ball move along with the paddle. And the ball should continue following the paddle until the player launches the ball.

In programming, we often use an if-statement to express a restriction. Here the restriction is: (hasStarted == false). By hasStarted, we mean: As long as the ball was not launched.

In the method where we launch the ball, there is supposed to be a line of code which sets hasStarted to true, so the if-block in the Update method will not get executed anymore.

The computer is not intelligent. It cannot think, and it does not understand our ideas. For this reason, we have to provide clear instructions such as: Do this, do that; if this, then that. And so on.

If you still find the code hard to grasp, take a pen and a sheet of paper and draw a diagram which visualises the logic/concept (not the code!). Rick is trying to solve a problem: Make the ball follow the paddle when the Play button was pressed, and make the ball move by physics engine after the right mouse button was pressed. Since the computer does not know anything about “ball”, “paddle”, “after” or “physics engine”, we need a more abstract solution.

Once you grasped Rick’s idea, I’m sure the code will make more sense. The code is just a way to express the concept in a way the computer can “understand”.

Did this help?


See also:

1 Like

This may be too far back, but I’m confused. When we say IF, doesn’t it assume that it is true to do, opposed to false?

Hi @DarkDekuLord,

The question that is asked in the parentheses is always: “Is (condition) true?” The answer is either true (to confirm that the condition is true) or false.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms