Microsoft MakeCode

Problem: Previous Sprites still on screen

We did a dice roll program for the random number stuff. Several of my students had the sprites appear, seeming to be atop one another. Eventually all of the “dots” of the die were on the screen after many rolls. I just created a program where I wanted to swap between one sprite and another and the other sprite’s edges are showing.
What am I doing wrong? Other students work didn’t have the problem, so I am baffled…

1 Like

@grandmadeb could you share your program?

Thank you. This is the student’s work. Only his code did this, but everyone else’s had no trouble… https://makecode.com/_7C4hEfRu2Lq6

Ah, they aren’t destroying the old sprite before they create the new one. My guess is that either the other students did have a destroy block, or they drew images that didn’t have any transparency so each new one hides the last one.

To fix it, I would do one of two things:

  1. Only create one sprite and set the image every time you roll. Here’s an example: https://makecode.com/_X8cDMF2Ut6yz
  2. Destroy the previous sprite before creating a new one. Here’s an example: https://makecode.com/_daXiiuF32VXY
1 Like

Okay, I think I am getting this, now. The example code did not delete the previous sprite because the sprites were both of the same size and did not have any transparency. The students who had no problems were the ones who also used sprites that did not have any transparency and completey covered the same areas with their dice.
This student and the others who had trouble ran into a problem because they had transparent areas in their sprites. So the easy fix is to fill in white.
The larger question, I guess, is — Is there a way to clear other sprites off the board?
And ooops, sorry to be trouble again! =D

That’s what I do in example 2 above!

I think the code in my first example is simpler, though. Instead of creating new sprites, just have one sprite and swap its image to a new one each time you roll. Then you don’t have to destroy sprites at all!

OKay, so to continue in my ignorance, what is the purpose of the set my_sprite to sprite kind of player, in addition to all of the other stuff? Does it impact anything? Because I was telling my kiddos we had to have (obviously I was WRONG) different variables for the sprites. (I love eating crow!) But many of the games they shared with me worked on their screen but broke when they shared them with me with all the same variable… I am afraid I am messing this all up. (I miss regular languages!)

So the important thing to know is that “set mySprite to sprite of kind player” has a side effect; even if you reassign the variable the sprite will continue to exist until it gets destroyed.

For example, consider this program:

Here I created two sprites. However, I only have a reference to the second one (the strawberry) because I used the same variable for both. The first one (the duck) will still exist in the game and won’t go away until it gets destroyed.

Now let’s say I want to destroy the duck. I can’t use the variable, because I reassigned it. That’s why it’s important to use separate variables for sprites.

However, let’s say I want to create a hundred ducks. I can’t create a hundred variables, so this is where SpriteKinds come in. By giving ducks a unique SpriteKind, I can get a reference to them by using events:

In this example, I made it so that the ducks are of kind Player and the strawberry is of kind Enemy. I don’t have a separate variable for each duck, but I can reference them in the overlaps event because I gave them all the same SpriteKind. I just need to make sure I drag out the “sprite” variable from the event instead of using “mySprite”.

As a rule, all variables that you can drag out of events are referring to the specific sprites that are involved in that event. In other words, this overlap event will run 100 times, once for each duck and each time it runs “sprite” will be pointing to a different duck.

1 Like

OKay, this is super helpful. So, to kill off all of the dice, I would destroy mySprite, and whoever it was, they would be history. Then I would set mySprite to the next dice. I can do this because I only need one dice on the screen. If I wanted a dice and a coin and a spinner, I have to have different kinds of sprites (that’s just the limitation of arcade.) One gets to be a player. Can I add new kinds, or is that troublesome?

You can add new kinds! It’s at the bottom of the kind dropdown

Is it fair to say that “sprite of kind player” behaves like a pointer to one of the ducks or strawberries or what-have-you?
Big help, @richard , thanks so much!

You’ve got it perfectly. Sprites are objects and persist in memory until they are deallocated (or destroyed; the term varies based on programming language and background). The variables that you use in MakeCode Arcade to manipulate sprites are reference variables (or pointers).

1 Like