Presentation is loading. Please wait.

Presentation is loading. Please wait.

TRIGGERS Triggers tell a script to start executing There are four types of triggers: When green flag is clicked When I am clicked When is pressed When.

Similar presentations


Presentation on theme: "TRIGGERS Triggers tell a script to start executing There are four types of triggers: When green flag is clicked When I am clicked When is pressed When."— Presentation transcript:

1 TRIGGERS Triggers tell a script to start executing There are four types of triggers: When green flag is clicked When I am clicked When is pressed When I receive a message more on messages later

2 TRIGGERS Exercise: Rewrite your square drawing script to draw a square whenever the space bar is pressed. Clear the screen before each new square.

3 THREADS You can build multiple scripts in the script area for any sprite (or the stage) All these scripts run at the same time (assuming their triggers occur) Each script is called a “thread”

4 THREADS Exercise 1: Write scripts to allow the user to move a sprite around the stage with the arrow keys. Exercise 2: Write scripts to have your sprite draw a red square when the 'r' key is pressed and a blue square when the 'b' key is pressed

5 SPRITE APPEARANCES Each sprite can have multiple costumes to change its appearance This allows us to make a sprite look different at different times but act the same Costumes can be added/changed in the “Costumes” tab above the script area

6 SPRITE APPEARANCES If a sprite has multiple costumes, you can use the and blocks to change which one is shown Sprites can also disappear and reappear using the and blocks

7 SPRITE APPEARANCES Exercise: Use the ballerina sprite (under “People” folder) and make her dance as follows: When the up arrow is pressed, change to the “jump” costume and move slightly up, then come back down When the down arrow is pressed, change to the “crouch” costume and move slightly down, then come back up

8 TIMING Sometimes things happen too fast. We can slow them down with Exercise: Fix your ballerina program to make the jumps and crouches noticeable.

9 TIMING Exercise: Write a BYOB program that involves two sprites. When the space bar is pressed, the sprites should “jump” one at a time. The second sprite should not leave the ground until the first has landed.

10 MESSAGES We can use the block to time things to happen at certain intervals But what if the timing changes? To make one sprite’s action trigger another, we can use messages Using, a sprite can send a message that will be heard by all other sprites Other sprites can respond to the message by using a trigger Each message must have a unique name

11 MESSAGES Exercise: Write a BYOB program involving two sprites having a conversation. The first sprite should say "Hello" for a few seconds. Then, the second sprite should say "How are you?" for a few seconds, to which the first sprite should respond "Fine, thanks!" The program should continue to work even if the times are changed.

12 VARIABLES Thought exercise: How can we make sprites move at different “speeds”? Variables allow us to store data and modify or retrieve it later Check out the Variables category Look around for built-in variables What shape are variable blocks?

13 VARIABLES When we click “Make a variable” we get a dialog box The name can be anything you want “For all sprites” means all sprites will be able to see and edit the variable Why might this be useful? Why might it be dangerous? “For this sprite only” means only the current sprite can see and edit it

14 VARIABLES Exercise: Write a BYOB program that has a ball bouncing around the stage. The up and down arrows should increase and decrease the speed at which the ball moves. Use the to handle bouncing.

15 INPUT You can ask the user for input using The response is stored in Note that is just a built-in variable Often, you’ll be storing the input in a variable for later use

16 INPUT Exercise 1: Write a script to do the following: Ask the user for a number between 1 and 10 Draw that many squares Exercise 2: Write a script to do the following: Ask the user for a number between 1 and 10 Ask the user for a number between 1 and 255 Draw the first number of squares with the pen color set to the second number


Download ppt "TRIGGERS Triggers tell a script to start executing There are four types of triggers: When green flag is clicked When I am clicked When is pressed When."

Similar presentations


Ads by Google