Build a Game Project: Feed-A-Star-Mole

# Gone Interval

## Brian Holt

SQLite Cloud

### Check out a free preview of the full Build a Game Project: Feed-A-Star-Mole course

The "Gone Interval" Lesson is part of the full, Build a Game Project: Feed-A-Star-Mole course featured in this preview video. Here's what you'd learn in this lesson:

Brian codes a function that causes the moles to be gone for a stretch of time.

Preview
Close

### Transcript from the "Gone Interval" Lesson

[00:00:00]
>> Brian Holt: So the next case is going to be leaving. What happens if a mole is leaving?
>> Brian Holt: So mole.next is gonna be, how long should a mole be gone?
>> Brian Holt: Well, I think in my case, I did 2 to 20 seconds, right? You don't want it to show up immediately again, because that would not be a very fun game.

[00:00:23]
But you don't want it to be gone for much more than about 20 seconds or the game gets boring pretty quickly. So let's write another function up here called function getGoneInterval, and we're gonna return Date.now.
>> Brian Holt: Plus and then we want some sort of random interval, right? We don't want to show up at the exact same time every single time.

[00:00:48]
We want to have them kind of be spaced out and a little unpredictable, right? So what we're gonna do is we're gonna say Math.floor(Math.random() *, and like I said I said 2 to 20 seconds. So we're going to do times 18000, right? 18000 would be 18 seconds, right?

[00:01:17]
+ 2000. So this is doing this math first, which is Math.random() *18, which, if you want to see what that looks like it's gonna be Math.random() *18000. So notice that's coming back with various numbers between 0 and 18000, okay?
>> Brian Holt: And then if I do Math.floor,
>> Brian Holt: That's going to round a number down.

[00:02:07]
Put that in there.
>> Brian Holt: Make it a little bigger. So notice that now here we get nice integers, right?
>> Brian Holt: So this is gonna be some number between 0 and 18000. And then we're just adding 2000 to it to make sure that we're always gonna be gone for at least two seconds.

[00:02:35]
>> Brian Holt: So a little bit of convoluted math there but, basic gist of get gone interval returns a random number between 2000 and 20000.

### Learn Straight from the Experts Who Shape the Modern Web

• In-depth Courses