# Help required: Two questions

**URL:** https://forum.makecode.com/t/help-required-two-questions/5732
**Category:** Help
**Created:** [January 25, 2021, 5:42am UTC](https://forum.makecode.com/t/help-required-two-questions/5732 "2021-01-25T05:42:45Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![omnisImperium](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/omnisimperium/32/2728_2.png) [@omnisImperium](https://forum.makecode.com/u/omnisImperium)
#### Post date: [January 25, 2021, 5:42am UTC](https://forum.makecode.com/t/help-required-two-questions/5732/1 "2021-01-25T05:42:45Z")

</div>

I have two questions

1: [https://makecode.com/\_Wme8d6MwDEkc](https://makecode.com/_Wme8d6MwDEkc). This is the link to a game I am working on. I have stumbled across an issue in this. As soon as the player leaves the main menu and enters the game, the entire game freezes. I know that this is because of the code that spawns enemies (yes the mySprite/Burgers are the enemies) is causing this issue, but I am unable to understand how to fix this in a quick manner.

2: Is the number entered for the vy / vx velocity of a sprite pixels per second? For example, if I enter 200 as the vx velocity of a projectile, will it travel 200 pixels per second or some other amount per second?

---

<div class="post-metadata">

### Author: ![UnsignedArduino](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/unsignedarduino/32/592_2.png) [@UnsignedArduino](https://forum.makecode.com/u/UnsignedArduino)
#### Post date: [January 25, 2021, 4:00pm UTC](https://forum.makecode.com/t/help-required-two-questions/5732/2 "2021-01-25T16:00:09Z")

</div>

1. This is the problematic code:

```typescript
sprites.onCreated(SpriteKind.Enemy, function (sprite) {
    while (GameEnded == false) {
        if (spriteutils.distanceBetween(player1, sprite) <= 40) {
            sprite.follow(player1, 30)
    }
})

```

The `sprites.onCreated` function will block until the code ends. Since this is a forever loop, it will pause eternally until it finishes, which is never. So put it in a `timer.background`:

```typescript
sprites.onCreated(SpriteKind.Enemy, function (sprite) {
    timer.background(function() {
        while (!GameEnded) {
            if (spriteutils.distanceBetween(player1, sprite) <= 40) {
                sprite.follow(player1, 30)
            } else {
                sprite.follow(null)
            }
            pause(100)
        }
    })
})

```

And also, it seems like you want for the burgers to stop following once a certain distance away, so passing in `null` instead of a sprite will make it stop following it. And you also need to `pause` so that the game loop can update.

---

<div class="post-metadata">

### Author: ![UnsignedArduino](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/unsignedarduino/32/592_2.png) [@UnsignedArduino](https://forum.makecode.com/u/UnsignedArduino)
#### Post date: [January 25, 2021, 4:00pm UTC](https://forum.makecode.com/t/help-required-two-questions/5732/3 "2021-01-25T16:00:13Z")

</div>

1. It’s px/sec. (Pixels per second - what else would make sense 😉 )

---

<div class="post-metadata">

### Author: ![DahbixLP](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/dahbixlp/32/4760_2.png) [@DahbixLP](https://forum.makecode.com/u/DahbixLP)
#### Post date: [January 25, 2021, 4:23pm UTC](https://forum.makecode.com/t/help-required-two-questions/5732/4 "2021-01-25T16:23:30Z")

</div>

1: I am not sure how to fix it but it must be a lot of lag! I have 0 fps… So probably too many enemies spawn!  
2: Yes! It is pixel per second! As you can see it says pix/s (Pixel per second)  
 ![image](https://us1.discourse-cdn.com/flex020/uploads/makecode/original/2X/4/478ed5bedc4d5f13578b2ecd04e93e929954dfbb.png)

---

<div class="post-metadata">

### Author: ![felixtsu](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/felixtsu/32/12024_2.png) [@felixtsu](https://forum.makecode.com/u/felixtsu)
#### Post date: [January 25, 2021, 4:24pm UTC](https://forum.makecode.com/t/help-required-two-questions/5732/5 "2021-01-25T16:24:20Z")

</div>

![image](https://us1.discourse-cdn.com/flex020/uploads/makecode/original/2X/2/27e0aeee0d3c50c5f34b020e2af47deaaf69d654.png)

Guessing what you want is,

 ![image](https://us1.discourse-cdn.com/flex020/uploads/makecode/original/2X/9/9fbbbcd24e4a34b975c9cdd62f170c2daa00cdd7.png)

[https://gameprogrammingpatterns.com/game-loop.html](https://gameprogrammingpatterns.com/game-loop.html)

 ![image](https://us1.discourse-cdn.com/flex020/uploads/makecode/original/2X/6/65cf33309250975e4f6023de2ea33d74ca0a229e.png)  
Put detecting code in ‘update game’ of every game tick
