I need this for my latest game. It needs to go up every second. Please help
Increase (Score) by (1)
Wait milliseconds (1000)
1000 milliseconds = 1 second by the way
Is score a verable? I tried using score as one, also what object
Set text (Score)
Increase block (score)
And set text to score
That’s the whole code for you
in the “set text” block, change the text “score” to the variable score
Thank you two! Do you two want credit?
No, it’s okay. I’m glad you learned something!
Did it work for you?
No I don’t, but thanks
Yes! Thank you for helping
Aww You people beat me to it
While the others’ way works fine, if you ever need it to stop for some reason it can’t. A better way to do it which would allow for stopping (so you can add a pause button, have it stop when the game ends, etc.) would be to use a recursive custom block. Here’s a quick tutorial for that.
(This isn’t the best code. I’ll make a note at the bottom with better code)
This is the custom ability. The check once if blocks both check if self stop = 0. They do that both before the wait and after the wait.
Why is stop being equal to 0 meaning update score? (Default value of variables)
Variables, by default, equal zero. Why does this matter for the above code? Because that means that by default stop will not equal anything other than zero. There is no need to set to zero until you change it.
The first block besides check one if is a set text. It changes your character (in my case Sloth)'s appearance to be the value of score.
Setting text to a variable
Whenever you see a set text block with a variable in the first parameter, the set text block means set text to (value of variable). So for example, if in the score code above, score was equal to one, it’d set text to 1. Since variables can only ever be numbers, setting text to a variable will only ever set text to a number. For example, if you set a variable to another variable, then set text to it, it would show you a number, not the name of the variable.
The second block waits for 1000 milliseconds. 1000 milliseconds is the same as one second. So it waits for one second, then continues the code.
The third block increases the score variable by one.
The fourth block, check once if, makes sure that stop is still equal to 0. Inside of it is the same ability that we are editing. This means that it will go through the code again, starting at the first check once if. As long as stop stays equal to 0, this code will have a similar effect to having used repeat forever. But in this project, we want you to be able to tap the screen to toggle the score counting.
The first block, check if else, checks if score on = 0, and if it isn’t, moves on to the code in else. If score on is equal to 0, it completely ignores the code in the else. (Looking back, a better name for the variable would have been score off. Why? Because most of the time 1 means yes and 0 means no. This project is the opposite.)
Let’s ignore the else for now.
The first block sets self stop to one. This means that when score on = 0, tapping the screeen will stop the score from incrementing. Do you know why? It’s the fact that variables are 0 by default again.
The second block waits one second. This makes sure that the wait in the ability is over. After the wait is done, it moves on to the next block.
The third block is why we needed the wait. It sets stop to zero.
The fourth block sets score on to 1. This means that score is off, and that tapping the screen again would go to the else.
If you’re thinking “Wait, wouldn’t setting stop to zero start the ability again?”, then read on. If you aren’t, still read on.
No, it wouldn’t. By the time stop gets set to zero, the ability has stopped. To start it again you’d need to
- Have stop equal zero
- Call the custom block again.
We already have one of those. Now we need to call the custom block again. We want it to happen when the iPad is tapped, but putting it in a when is tapped would make it run whenever we tap, not just when we tap and the score isn’t being incremented. The solution to this is the check if else block we are currently coding in. Now let’s look at the else.
Here’s the image again. Now we are paying attention to everything underneath the else.
The first block is a check once if. It checks if score on = 1. (Which means that the score is off.) If it does, it sets score on to zero. (Which means that the score is on.)
THE SECOND BLOCK DOES NOT GO THERE
The score ability does not go inside of this check once if.
The actual code is this:
To restart the counting, we need a new rule.
You will notice that this rule is above the one we were just coding. .
Why does the order of the rules matter?
When you tap 5e screen, both rules go off. But not at the exact same time. The rule we just added requires a variable to be 1, but the one we had added before sets it to 0. The reason we have to have them in this order is so that it starts the score keeping again before it sets score on to 0.
The ability is what we made earlier.
This is the result.
Code to improve this
Looking at our code above, it can be improved.
This method will change the two wait 1000 milliseconds blocks to better pieces of code.
This is inside the custom block. It replaces the wait 1000 milliseconds with 100 wait ten milliseconds. Except each time it checks to make sure stop = 0. This allows us to do the next improvement.
This block replaces the wait one second with eight wait 0s. Why? Waiting zero milliseconds does nothing, right? Actually, it waits one frame. A frame is how long it takes for a single block to run. Waiting zero milliseconds takes a frame but does nothing. This is good. But why eight? Because eight is close to how long it takes one go of the score ability. (Slightly higher) if stop is changed from 0.
Let me know if there’s anything you didn’t understand about this.
How to make a day and night cycle
It really is descriptive and helpful.
How did you highlight it?