2

I am trying to write a scratch code in which on a button press a particular sprite will be displayed and on another button press another sprite will be displayed, the first one should disappears.

I want to know the basic feature of the scratch that should be used here.

Petter Friberg
  • 21,252
  • 9
  • 60
  • 109
gpuguy
  • 4,607
  • 17
  • 67
  • 125

2 Answers2

7

There are several different ways to do this. There are ways to do it with fewer scripts or fewer sprites, and using different methods. Here are two ways, showing two different methods, and a bonus at the end.

Let's start with the simplest way.

4 sprites, 8 scripts, 16 blocks. Uses broadcasts.

In this, we'll use four sprites:

Scratch | Gobo | ShowGoboButton | ShowScratchButton

We have a Scratch sprite, a Gobo sprite, a ShowGoboButton sprite, and a ShowScratchButton sprite.

Each of these sprites has scripts in it.

For the two sprites that are showing and disappearing when buttons are pressed - Gobo and Scratch - we need to have them show and disappear when an event happens - when the button is clicked. So something needs to happen when the button is clicked. How do we do that? With the WHEN THIS SPRITE CLICKED block. Using that in conjunction with another block, the BROADCAST blocks, we can have it do something when it's pressed.

So this is what the ShowGoboButton sprite has in its scripts:

WHEN THIS SPRITE CLICKED, BROADCAST "ShowGobo"

When the button is clicked, it broadcasts a message... but at the moment, it's broadcasting into the void. Nobody is listening for the broadcast. The Gobo (and Scratch) sprites need to do listen for the broadcast, and then show or hide depending on which it hears.

This is what Gobo is hiding in the scripts:

WHEN GREEN FLAG CLICKED, SHOW | WHEN I RECEIVE "ShowGobo", SHOW | WHEN I RECEIVE "ShowScratch", HIDE

The WHEN GREEN FLAG CLICKED script makes the sprite show when the project is started. The other two, which both start with the WHEN I RECEIVE hat block, are how it listens for the broadcast. It listens for both messages, and if it's the right one, it shows. If it's the wrong one, it hides.

From those, extrapolate to the other two sprites - it's exactly the same code, but in reverse.

That's the simplest way, and an effective way, and the way a beginner would be best off doing. However, if you're at a slightly higher level, then using variables would be better.

4 sprites, 4 scripts, 26 blocks. Uses variables.

Instead of a broadcast, now we'll set a variable. Variable can be found in the Data section when looking for blocks.

So wait - what is a variable, exactly?
A variable is like a box, that temporarily holds what you put in it. You can put any string in a variable - numbers, letters, a mixture...

So our variable is going to be named "WhichSpriteShows", and we're going to use this box to store the data for which sprite should show.

Getting into the code, look back at the broadcasts. Instead of having the button broadcast, now we'll have it set a variable.

So this is what the GoboButton is doing now:

WHEN THIS SPRITE CLICKED, SET "WhichSpriteShows" TO "Gobo"

The button no longer broadcasts a message; now, it just changes what's in the box.

And so now, instead of having Gobo listen for a broadcast, we have Gobo paying attention to what the variable is set to - what's in the box.

This is what Gobo has inside:

WHEN GREEN FLAG CLICKED, SET "WhichSpriteShows" TO "0", SHOW, FOREVER{IF "WhichSpriteShows"="Gobo" THEN SHOW, ELSE, IF "WhichSpriteShows"="Scratch", then HIDE}

Woah, woah, woah. What's with the FOREVER, you may ask. Why are we setting WhichSpriteShows to 0 at first? Why not just hide the sprite if the variable is set to anything besides "Gobo" by sticking a plain HIDE block in the ELSE part of the IF..., ELSE block instead of performing the seemingly redundant check to see if it's set to "Scratch"?

The FOREVER is necessary, so that the script is always listening to see what the current state of the variable is.

The decision to set "WhichSpriteShows" to "0" at the beginning, as well as the extra check to see if it's set to a specific ELSE is so that both sprites will show at the beginning, when the green flag is clicked. If you don't want that, you can modify the code accordingly :)

As with the broadcasts, extrapolate for the Scratch sprites.


Bonus

Now... if you want to get really fancy, and use fewer sprites, it's possible to do it with only two sprites total. The magic of clones. I'm not going to explain too much here, because everything should be relatively self explanatory, and if you're doing the most complicated way I'm telling you about, then you must either are already a Scratcher or want to break stuff.

2 sprites, 3 scripts, 61 blocks. Uses clones.

To understand how this works, you need to see how the stage is set up:

Scratch cat on the left, Gobo on the right, button to show Scratch cat on top, button for Gobo on bottom

The scripts for the button sprite are relatively simple:

enter image description here

And the sprite's scripts aren't too crazy either:

enter image description here

If you want, you can play around with the project I used to make this over here.

Mithical
  • 603
  • 1
  • 17
  • 28
-1

I am not exactly sure what you mean by a button press.

In case you want to show one sprite on key press "a" and hide the other sprite and do the opposite in case of key press "b" then this is pretty simple.

Here below you see the script for the sprite that must be shown on "b" keypress. The script for the sprite that must be shown on "a" keypress looks exactly the same except that "show" and "hide" blocks must be switched everywhere.

enter image description here

JanVdA
  • 338
  • 3
  • 15
  • 3
    I downvoted this because, while it does provide a way to hide/show a single sprite, the OP requested in the question to have a single sprite appear and a second sprite disappear. This doesn't do that :/ – Mithical May 28 '18 at 09:52
  • I have tested the code in scratch and it worked as described: pressing "a" did appear one sprite and made the other sprite at the same time dissappear while pressing "b" did just the opposite. Is it because I didn't show a screenshot of the code of the 2nd sprite allthough I clearly described in the text above it ? – JanVdA May 28 '18 at 11:10
  • @JanVdA I think it is because you didn't show the second sprite's code. (Don't worry, I upvoted. – VFDan Feb 23 '19 at 15:36