Lesson Four:Using Variables
Lesson FourOpen up the project which you previously created using a character and a ball in lessons one and two. We are now going to create a variable which will count how many times the ball bounces. By learning how to do this we can transfer the use of variables to future projects.
Lesson FourIn this lesson, we will be looking at how we can use variables in our project. Variables can be used to keep scores if you created a game using scratch. They can also be used as ‘page numbers’.Open your previous project in ‘scratch’ and click on the ‘variable’ controls.
Lesson FourClick on the ball and then the ‘make new variable’ control. The following box should appear:Give the variable the name ‘Bounces’. This will create the variable and we can alter this later on to use in this project.Click ‘for this sprite only’. This will then show that the variable will count the amount of times the ball bounces.
Now that you have created your variable, you should find that the following controls are available under the ‘variables’ tab.Lesson FourWe are now going to use these controls in our script for the ball so that for each time the ball bounces, the number will increase by 1.
Lesson FourAdd the following controls to your script:Setting ‘bounces’ to ‘0’ at the start means that the bounces that have previously been counted will be reset.We know on our script that when the sprite reaches ‘x:29, y:-115’, that this is the end of the ‘bounce’. We therefore want to increase the amount of ‘bounces’ by 1.
Lesson FourClick on the ‘start’ icon. Watch how the ‘bounces’ variable increases every time the ball appears to bounce.You can move the variable around by clicking and dragging it to any position on the ‘stage’.
Extension ActivityAdd another variable to your project for the character. You could set this to count either how many times it bounces or each time it reaches a certain point. Change the value so that each time the ball bounces, the value is increased by two.

Lesson four presentation

  • 1.
  • 2.
    Lesson FourOpen upthe project which you previously created using a character and a ball in lessons one and two. We are now going to create a variable which will count how many times the ball bounces. By learning how to do this we can transfer the use of variables to future projects.
  • 3.
    Lesson FourIn thislesson, we will be looking at how we can use variables in our project. Variables can be used to keep scores if you created a game using scratch. They can also be used as ‘page numbers’.Open your previous project in ‘scratch’ and click on the ‘variable’ controls.
  • 4.
    Lesson FourClick onthe ball and then the ‘make new variable’ control. The following box should appear:Give the variable the name ‘Bounces’. This will create the variable and we can alter this later on to use in this project.Click ‘for this sprite only’. This will then show that the variable will count the amount of times the ball bounces.
  • 5.
    Now that youhave created your variable, you should find that the following controls are available under the ‘variables’ tab.Lesson FourWe are now going to use these controls in our script for the ball so that for each time the ball bounces, the number will increase by 1.
  • 6.
    Lesson FourAdd thefollowing controls to your script:Setting ‘bounces’ to ‘0’ at the start means that the bounces that have previously been counted will be reset.We know on our script that when the sprite reaches ‘x:29, y:-115’, that this is the end of the ‘bounce’. We therefore want to increase the amount of ‘bounces’ by 1.
  • 7.
    Lesson FourClick onthe ‘start’ icon. Watch how the ‘bounces’ variable increases every time the ball appears to bounce.You can move the variable around by clicking and dragging it to any position on the ‘stage’.
  • 8.
    Extension ActivityAdd anothervariable to your project for the character. You could set this to count either how many times it bounces or each time it reaches a certain point. Change the value so that each time the ball bounces, the value is increased by two.