Please update your browser
Your browser version is not supported by this site. CS First may not work properly.
In this add on, you'll code the sprite
to ask if the buyer wants to purchase the gadget.
Then, react to the answer.
First, ask the person watching the ad
if he or she would like to buy the gadget.
Select the android sprite.
From the sensing menu, drag out an ask block.
Write a question in the box.
This example asks,
would you like to buy the SportsWatch9000?
Test this by clicking the block.
The sprite asks,
would you like to buy the SportsWatch9000,
and a box appears at the bottom of the screen.
Someone watching your project would answer the question
in that box.
Type some text in the box and click enter.
A variable named answer
stores anything you typed in the box.
In the sensing menu, click the check mark next to answer
to see it in the top left corner of the stage.
It should say what you typed into the box.
The sprite will act happy if the answer typed is yes.
Making your program check what answer the user typed
is similar to how you checked if a key was pressed
or if the ball sprite was touching the player sprite
in the net sports project in activity three.
From the control menu,
place an if block under the ask block.
To check if the answer is the same as yes,
from the operator's menu, place an equals block
in the conditions section of the if block.
From the sensing menu,
place answer on one side of the if block
and type yes on the other side.
Inside the if,
place the code for what will happen if the answer is yes.
In this example, the sprite switches to the happy costume
and says, great choice.
You're going to love the SportsWatch9000.
Click on the code.
The sprite should say or do
whatever cool thing you programmed.
Click the code and type no this time.
To make something else happen,
if the user answers something other than yes,
use the if else block instead of the if block.
Set the if block aside.
From the control menu,
place the if else block after the ask block.
Drag the equals condition from if the block
to the condition section of the if else block.
Then, drag your reaction code from the if block
to the if section of the new if else block.
The else section of the if else block
tells a program what to do if the condition isn't true,
so if the answer isn't yes.
Place the code for the sad sprite here.
In this example, the sprite switches to the sad costume
and says, oh well.
Maybe next time.
Click the code and type no.
The code inside else should run.
Finally, tell your program when to make this happen.
Using broadcast and receive blocks
help make it easier to read and reuse your code.
Anytime the sprite should ask for a purchase decision,
these blocks will make the code you just wrote run.
From the events menu, drag a broadcast and wait block
to at least one place om the block stack
from the starter code.
From the drop down, select new message
and type purchase decision.
Broadcast and wait tells a program to wait
until the code that receives the message is done running
before the program runs more code.
Broadcast would let the rest of the code run
while the sprite is asking if the audience wants to buy.
From the events menu, place a when I receive block
on top of the code that makes the sprite ask the audience
if they want to buy.
From the drop down, select purchase decision.
Test the code by clicking the flag.
The sprite will ask its question
when the program sends the purchase decision message.
All right, here's the game plan.
Code the sprite to ask the buyer a question.
Then use an if else statement to respond to the answer.