Help required: Two questions

I have two questions

1: 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?

1 Like
  1. This is the problematic code:
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:

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.

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

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

2 Likes

Guessing what you want is,

https://gameprogrammingpatterns.com/game-loop.html


Put detecting code in ‘update game’ of every game tick

3 Likes