Microsoft MakeCode

How do I make only a certain amount of sprites show up from an array?

I have an array that places food into a game. There is a tile in 8 different spots, and I have an array place four different food sprites at random on those tiles. When I load my game food is placed on those 8 tiles, and each time it seems to be placing one of four different food sprites at random (which is what I want). The problem is that when I restart my game it is placing extra food sprites in the middle of the screen.

Here is where I want the food sprites to go (the food sprite tile says “F1”):

Here is the code and the array that places one of four different food sprites:

Here is what happens when I load my game (the extra food sprites are all stacked up in the middle of the screen):

Does anyone know why the extra sprites are showing up in the middle of the screen?

If so, do you know how I could stop this from happening?


Ah, this is an interesting one! So essentially the blue “sprite [image] of kind Food” block:


actually creates a sprite, and when a sprite is created it is automatically drawn to the screen. Since the “set list to” block is inside the for loop, it creates four sprites each time the loop runs. However, if you move the array out of the loop, you’ll see that there will only be one sprite of each food image. What you actually want to do is make an array of images, and then create a new sprite in the “place on tile block”, like this:


Hope that helps!

1 Like

Thank you! That really helped!