Gabe
Gabe

Reputation: 13

Creating a button in scratch

I am trying to make a multiple choice quiz in Scratch, but I am having trouble with coding the buttons. I have created the buttons, but I do not know how to get them so the users can click on them to choose answers. How can I get them to work? Here is a screenshot of the code and the actual quiz.

Upvotes: 0

Views: 5273

Answers (1)

Mithical
Mithical

Reputation: 624

You're currently using the TOUCHING SPRITE block. This block is for when the sprite itself is touching another sprite.

If you go into the code for each button, you'll find a hat block: WHEN THIS SPRITE CLICKED.

WHEN THIS SPRITE CLICKED

You can take this block, and have it set a variable with which block was clicked.

So in each of the button sprites you'd have something like this:

WHEN THIS SPRITE CLICKED  
SET (ANSWERSELECTED) TO [A]

WHEN THIS SPRITE CLICKED SET (ANSWERSELECTED) TO A

Then in your person sprite, where you currently have TOUCHING SPRITE 2 etc, you can have a variable that contains the answer selected. You'll want to clear the variable each time a new question is asked, e.g., set it to blank.

So you'll end up with something like this:

enter image description here

That's probably the simplest way.

IIRC, if you don't want to put scripts in the button sprites, you can also hack something together with something like IF MOUSE DOWN AND MOUSE X = (here you'd need to hack together the location of the button, probably using a AND block and using the X location of the button on both ends (which you can find using the mouse, under the player shows the current X and Y positions of the mouse)) AND MOUSE Y = (same as with X) SAY CORRECT FOR 3 SECS, but that's more difficult.

enter image description here

(That will require that the mouse be on the exact center of the button, though, so if you want to do it that way you'll probably want to account for being slightly off the center.)

Upvotes: 2

Related Questions