Layering Sprites with Shapes

Hello All!

My students and I have run into a problem I can’t solve. We are trying to layer background, sprite, shape, sprite. However, when we call drawSprites() on the second sprite, it redraws the first sprite (already drawn) on top of the shape. I have tried using sprite.depth and I have tried writing separate functions to draw the sprites in hopes of limiting the scope, but no luck. Can anyone help me make this work?

Thanks in advance!!
Amy

image

add the var and sprite animation after first draw sprite. I did this in level 14 and it draws the bell on top of the snake…

Thanks so much for your reply! Yes, it does draw the bell on top of the snake, but the snake is no longer behind the bars - we are trying to get the snake to stay on its original layer instead of moving to the top.

I don’t think this is possible to do with the blocks that you have in this lesson. drawSprites draws every sprite at the same time, so it’s impossible to draw a shape between them.

Once you have the “visible” property, you can do it, though. You’d need to create the first sprite, draw it, then make it invisible. Then draw your shape, then create and draw the second sprite. I have an example here:

There are other ways to do this that don’t feel as much of a workaround, but they rely on blocks that aren’t in the curriculum.

Elizabeth

1 Like

Thank you, Elizabeth! That works perfectly. My kiddos are already flipping back and forth between blocks and code, so they can type it in. I really appreciate the assistance!

1 Like

I’m a few years late to this…

Logically this is blowing my mind. The multiple drawSprites and how it’s interacting with the .visible property…

I can put this idea in a draw loop to get a shape drawn between sprites but I don’t understand why it’s working. For example:

@ackerson.teach,

I agree that it is a little confusing and hard to explain.

I prefer the undocumented “drawSprite()” function (singular) that allows you to draw a single sprite at a time rather than drawing them all. Elizabeth didn’t suggest this in her post, but it does work if you put the name of the sprite inside the ().

Mike