GoalExplain why objects must be reset when a program starts, and write a start script that resets them.
New words
reset · to put something back the way it was at the startoriginal state · how an object looks and where it is when the program beginsstart script · a script that begins with the green flag and puts objects in their starting statedirection · the way a sprite is pointing; 90 points right
Explanation
After a program has run, its sprites do not go back by themselves. A sprite may stay at the other side of the stage, stay hidden or stay big. If we run the program again, it begins in the wrong state. To prevent this, give each sprite a start script. It begins with when green flag clicked. It sets the position, the size, the costume, the direction and whether the sprite is shown. The start script and the other scripts of a sprite are separate scripts. Test by running the program twice.
Worked examples
Run
Taxi’s x at the start, no start script
Taxi’s x at the start, with a start script
1
-180
-180
2
120
-180
The Taxi moves 50 steps for each right arrow press. It starts at x: -180. After 6 presses it is at x: 120. Without a start script, the second run begins from x: 120, near the edge of the stage. With a start script, every run begins at x: -180.
Block that changes it
Block that resets it
change size by (10)
set size to (100) %
hide
show
next costume
switch costume to [costume1 v]
turn ↻ (15) degrees
point in direction (90)
move (10) steps
go to x: (0) y: (0)
A block that changes a property has a block that puts it back.
The Tortoise has a start script and a key script. The key script makes it bigger and hides it when it touches the Lettuce. Without ‘show’ in the start script, the Tortoise would stay hidden in the next run.
Class activity
Reset the stage (pairs). Draw a stage on paper and cut out a Mouse and a Coin. Write three program cards: ‘Mouse moves 3 squares for each press’, ‘if the Mouse is on the Coin, then hide the Coin’ and ‘Mouse grows’. Play the program until the Coin disappears. Now play again without resetting. What goes wrong? Write a start card that puts everything back, and play again.
Practice
1
A sprite glided to the top of the stage, hid itself and grew by 20. Write the blocks that reset it.
go to its starting x and y, show, and set size to 100 %.
2
Fill the empty block so that the Whale begins at its normal size.
set size to (100) %.
3
Why does a program often work the first time without a start script?
Because the sprites are already where we left them when we built the program. The problem shows only after a run has moved, hidden or resized them.
4
The Bee moves 40 steps for each press. It starts at x: 0 and is pressed 3 times. Where does a second run start without a start script? Where does it start with ‘go to x: 0 y: 0’ in a start script?
After 3 presses the Bee is at x: 120. Without a start script, the second run starts at 120. With the start script, it starts at 0.