Week 4: Keeping score with variables
Today we’ll create collectable objects that increase our score when we collide with them. We’ll edit the player script and start using number variables for a score system.
Programming concepts we’ll learn:
- Variables
- Data types
1: Adding collectable objects
In this step we’ll add collectable objects to our playable level. Watch the video below for a refresher on tagging objects and reusing scripts.
- Place collectables
- Add script to collectable
- “When created” from events
- “add tag tag name to myself” from sensing
- Change tag to “collectable”
- Add collectable script to all collectables
- In the next step we’ll create a variable for the score.
2: Creating a variable
Next we’ll create a number variable for a scoring system. Watch the video below to learn how to create a new variable.
- Edit player script
- Create a new variable by grabbing “set true/false i” from variables
- Add this block to “when the level starts”
- Change true/false to number
- Click on i and choose new variable, name it “score”
- Grab 0 block from operators
- In the next step we’ll increase this number variable when the player collides with a collectable
3: Increasing the score
Now we’ll increase the score when the player touches a collectable object. Watch the video below to learn how to change the value of a variable.
- Add another “if do” block (from control flow) to “when touched get toucher”
- “Myself has tag” from sensing
- Replace myself with “instance toucher” from variables
- Destroy myself from control flow
- Change myself to toucher
- Change tag to “collectable”
- “Set number score to” from variables
- “+” from operators
- “Number score” from variables
- “0” from operators, change it to “1”
- In the next step we’ll get the score to display on screen
4: Displaying the score on screen
In this step we’ll get the score to display on screen. Watch the video below to learn how to display text in your game.
- “Create new textfield” from draw
- Put this block inside “when level starts
- Type “0” in the textfield
- “Set x position of myself to 0” from transform
- Put this block inside constantly
- Replace “myself” with “instance textfield” from variables
- Replace 0 with “camera x” from looks
- Duplicate these blocks for y position and camera y
- Close script and save
- Play game to see score in corner of camera
- But it doesn’t update! In the next step we’ll get the score to update.
5: Updating the score textfield
Now we’ll get the score textfield to update. Watch the video below to learn how to update the value displayed in a textfield.
- “Set text of instance textfield to” from draw
- Put this block in the collectable collision detection
- “Number score” from variables, put this block in “set text of instance textfield to”, but it won’t go in because number datatype can’t be used where a text datatype is required
- We need to convert it using this block: “create text with” from operators in the text section
- Next week we’ll program a patrolling obstacle.