Discuss Scratch

Papa_B
New to Scratch
4 posts

Waiting for events?

I'm new at this, but have been programming for (well) decades. That may be part of my problem. I'm here looking to help my granddaughter learn Scratch. I wrote a small program with two nested loops. The outer one was supposed to wait until the space key was pressed before running the inner one.

When I started it without the green flag hat block, the collection ran as expected. When I added the flag block, it doesn't seem to pause when I wait for the space key, but does pause when I wait for N seconds.

I'm confused. Is it running so fast that it looks like the space key is always pressed? Or is it something else?

Great environment, BTW. I know we're gonna have fun doing this.

Papa_B

drmcw
Scratcher
1000+ posts

Waiting for events?

Welcome. Can you share the project to see what the scripts are?

10 !
ScratchVaders or Galaga?
Maybe Eliza can help you decide?
Papa_B
New to Scratch
4 posts

Waiting for events?

I just shared it. It's called Circles. Start it by clicking on Pico. If you replace the “click on sprite” hat block with the green flag one, you'll see the effect I am puzzled by.

Thanks.
DadOfMrLog
Scratcher
1000+ posts

Waiting for events?

I think the basic problem you have is because of the script on the stage which executes on green flag.

This is performing a loop through the list, and you're using the same global variables to do the counting as in the sprite.

This means when you do the wait in the sprite loop, the stage loop continues through and finishes (since it's not waiting - in fact it's not doing anything!) So then your global variable N ends up pointing beyond the end of the list when the next loop of the sprite starts up…

If you have “when sprite clicked”, then the stage loop doesn't execute, so it all works fine.

Last edited by DadOfMrLog (Feb. 19, 2014 19:34:24)



Alternate account: TheLogFather –– HowTos and useful custom blocks (see studio). Examples below…


- String manipulation - - - X to power of Y - - - Clone point to clone - Detect New Scratcher - Speed tests studio -

Papa_B
New to Scratch
4 posts

Waiting for events?

So, it's an issue related to the place where the script runs?

If that's the case, then I presume I need to do something like separate off the sprite loop and activate by sending a message to it and then waiting for the sprite to send a message back that it's done. Is that more or less on the right track?
DadOfMrLog
Scratcher
1000+ posts

Waiting for events?

It really depends what you're trying to achieve. At the moment the script on the stage seems to be pointless.

If you want to allow such things to run at the same time then you should use variables that are local to a sprite (or have lots of global vars with different names, so they don't stomp on each other like this!)

Alternatively, if you don't want them to run at the same time, then yes, use broadcast-and-waits to ensure things get run in the order you want.



Alternate account: TheLogFather –– HowTos and useful custom blocks (see studio). Examples below…


- String manipulation - - - X to power of Y - - - Clone point to clone - Detect New Scratcher - Speed tests studio -

jmarioman
Scratcher
17 posts

Waiting for events?

So what you are saying is instead of waiting 2 seconds you want it to wait until space is pressed?

If so, wait until <key space pressed> in the control and sensing tab instead of your wait 2 seconds.
DadOfMrLog
Scratcher
1000+ posts

Waiting for events?

DadOfMrLog wrote:

It really depends what you're trying to achieve. At the moment the script on the stage seems to be pointless.
What I mean is that the script you currently have on the stage doesn't actually do anything useful - in fact, it only causes trouble… I'm wondering if you meant it to be there at all…?

It looks to me like you started writing, in the stage, the same script that's currently in the sprite, and you got as far as the part where you wanted to start drawing the sides of the polygon. But it was at that point you suddenly noticed that the stage doesn't have any pen blocks (apart from “clear”), nor any way to move the pen around the screen… so you then moved into the sprite and recreated the script there. (I recognise that sequence of events ‘cos I’ve done exactly that several times myself, even fairly recently! )

However, you've left the original half-written script in the stage. And it's the use of the same global variable in there which is interfering with the script in the sprite when everything is started from green flag click. (So having it wait for space doesn't work, because once space is pressed the global variable N has already been taken to its final value by the loop in the stage. This means the next loop in the sprite script can't get the right data from the list because N is wrong - it's already 22 - so it ends up just looping around its repeat loop immediately, or, if you also add a “wait until not key space pressed”, then it waits for you to press space 21 times, without showing anything, before it ends with “that's it”.)

I do find it slightly bizarre that the stage can't do any pen drawing (given that it's effectively where all pen drawing ends up, from all sprites). But I guess it's essentially because the stage has no motion blocks, so there's no way to move the pen around…

Last edited by DadOfMrLog (Feb. 20, 2014 03:26:46)



Alternate account: TheLogFather –– HowTos and useful custom blocks (see studio). Examples below…


- String manipulation - - - X to power of Y - - - Clone point to clone - Detect New Scratcher - Speed tests studio -

Papa_B
New to Scratch
4 posts

Waiting for events?

Thanks everyone for all the help and advice. I've learned to manage the scripts better so I only have single copies now. Also, I have learned to make the values local to the script, though I wonder why that isn't the default for newbies.

I'm working on a newer version that uses broadcast to separate out the various components and which also uses keystrokes to control the color of the sides of the figure.

Powered by DjangoBB