Microsoft MakeCode

Advice, please? GameJam Practice 2 - Clean - Microsoft MakeCode

Very excited about the MakeCode game jam, and still very much a noob, so I’m playing with some concept ideas.

Experimenting with arrays to add a group of sprites to “clean up.” I want to be able to destroy each of the sprites as the player overlaps them. With the code as-is, all of the sprites are destroyed simultaneously. If I remove the loop block from the sprite overlap block and set it to destroy the “Rubble” variable, one random sprite is destroyed and no others can be.

What am I missing here? Any advice or assistance would be appreciated.

Thank you!

At the end of the on start, the rubble variable will refer to the last rock that was created, as it gets reassigned to the newly created sprite on each step. This is why the random rock is getting destroyed - it’s always the last one that was created.

When you’re in an overlap event, the two sprite variables at the top of the event - sprite and otherSprite - will refer to the specific sprites that are touching. In this case, that means the otherSprite variable will have the rock the player has just stepped on. If you click and drag that variable from the top of the overlap event, you can just destroy that rock and it will get the behavior you want.

2 Likes

Brilliant! Your sage advice has helped once again! I knew it had to be something simple I was missing. Thank you so much!

Also, it’s worth noting this is not a situation where you necessarily need to use arrays; you can get by without them with a few small changes:

That’s not to say it’s not worth learning to use arrays in blocks - there are some examples with sprites you might find interesting here and in the lessons before / after that one - it’s just often worth taking a second look when you’re trying to use them to see if there is a simpler approach, as they do tend to introduce complexity that might not be needed or beneficial for your program when used in blocks / often there is a different, more clear approach.

2 Likes

Holy cows that is so much cleaner! You are a genius.

1 Like

For the original code, I’ve never really understood how variables work when sprites are assigned to the same variable name. In this case 4 lumps of rubble certainly do appear on screen. Does each successive instantiation get stored in the same variable? Will the first three disappear when they are GC’d or is some implicit bit of magic holding onto them?

And if you want to free up the memory used by some sprites, how do you do this in blocks?

The variable only ever stores the last thing it is assigned to. What is keeping them around after any references you directly have to them are gone is this part of sprites.create; to have a way to handle physics or display the sprites on the screen, the game engine needs to have a reference to each sprite that the player has created.

The way to get rid of a sprite would be to call destroy on it, which will indicate to the game engine that it get should get rid of that reference and stop interacting with the sprite. Then, after all other references to the sprite (ongoing events that are keeping a reference, user variables, etc) are cleared, the sprite will finally be garbage collected.