Is there a way to pause the code (wait command?)

csdunit3

#1

I’m not too familiar with JavaScript and am figuring out the weblab coding, but I have a student that wants to cycle through various sprites which would mean drawing them then either covering them with a background or making them not show up problem seems to be that we’ll never see them without slowing down the code with something like a wait and changing world frame rate doesn’t seem to help.
I’m hoping there’s some way to have the program pause for a bit sort of like a wait block in scratch or some other languages.


#2

JavaScript is a single-threaded language, meaning it can only do one thing at a time. This makes “waiting” difficult, because you usually don’t actually want the whole program (or browser eve) to wait and do nothing. The best way to accomplish this kind of timed change is to do one of three things:

  • Use a counter variable to keep track of how long to wait between changes
  • Use World.seconds or World.frameCount to run code after a given number of seconds or frames have occured
  • Use the modulo operator (%) to only run code with World.frameCount or World.seconds is divisible by a given number

Her’s an example program using all three techniques https://studio.code.org/projects/gamelab/tECPkYxmoS9Wa-GorVeI_Q/edit


#3

This is awesome!
Just what I needed.
And I’m learning!..Pretty cool!

Thanks


#4

I have many students that have used Scratch and have used the wait command. Could you explain to me how the two programs are different, I thought they were both JavaScript. Thanks for your time.


#5

@mary.ryan

Maybe @josh can jump in and add more but I see the main difference between scratch and the game lab as this:

Scratch -> Event driven programming. As an example, when the green flag is clicked do these things. All sprites can react to that message and code is spread out among all different sprites.

Game Lab -> Procedural. The code is all in one place, is very linear, and executes one line at a time, then loops back to the top of the draw loop to create movement. All of the events are placed in the draw loop as if/else structures.


#6

Each version of Scratch has actually been written in a different language, with the upcoming Scratch 3 being written in JavaScript and HTML5. That said, there’s a significant difference between using a language that was built with a different language, and using that different language directly. Scratch operates at a higher level of abstraction and allows students to mix and match multiple different programming paradigms, including the blocking “wait” model that you’re talking about. In order to do that they have to fiddle with student’s code under the hood to deal with the contstraints of the underlying language.

In our case, students are using JavaScript directly, so they have to cope with the constraints of the language. Even under those constraints, there are multiple approaches to get the kind of waiting behavior that you’ve describe, some of which require concepts that are more advanced than we get to in an introductory course.


#7

I love your explanations, Josh. Any time you want to come teach my class, you’re welcome.
I’m a huge fan of Scratch as far as getting kids interested in coding and some of the general ideas of coding, but I’ve become a big fan of the GameLab in the idea that it’s a great step up from Scratch. I had another teacher say they’re the same thing. They’re definitely not. GameLab is much more realistic to true coding without being too intimidating. The choice of block coding or text based is a great aspect of it to help with differentiation.


#8

If I’m in the neighborhood I’m always game for a classroom visit :slight_smile:


#9

Are you coming to South Korea next school year? We’d love to have you!


#10

Hmmm, can’t wrap my head around this one. I like Scratch’s “wait one second” :slight_smile:
How would we play a sound for only one second or only once? For example, in a pong game, when the ball hits the paddle, make a bonk sound and then don’t make it again until the ball hits the paddle again. Or play a “you win” statement once. We’re putting it in the draw loop, so it’s playing over and over. Suggestion?


#11

Here is one way to do this: The key is to not loop the sound “false”

if (bunny.isTouching(carrot)) {
score = score+1;
carrot.x = carrot.x=400;
playSound(“sound://default.mp3”, false);
carrot.y = randomNumber(10, 390);

}