Text before some sprites but after others

My student is trying to make text show up on top of his background sprite but before his balloon sprites so when he pushes the up arrow and his balloons go up the text shows up…any help. We can get it behind all sprites or in front of all sprites but not between them

1 Like

Terri,

Keep in mind that with JavaScript and GameLab, the order of the functions called matters. If you want the text to be in the foreground, call it after the drawSprites function. If you want a layered effect, just takes some logic to figure out what is called/drawn first.

Hope that helps!
Brad

Hello,
I am having the same issue as above but your response is not clear to me. I am making a basic static scene and would like the text to appear after the background but before the “pump” sprite. If I follow your advise regarding which is “called or drawn first” and place the text after the background but before the “pump” sprite the text is still not visible. It is only visible if I place the text after the “draw sprites” block. See attached as example Can you pls clarify how to make this happen?
Thanks for your help.

You can draw each sprite individually using drawSprite (without the s)

drawSprite(background)
text("text", 200, 200)
drawSprite(balloon)

doing so will result in a warning, but it shouldn’t matter.
If you want to not get warnings, you can put the sprite in a group and drawSprites(group)

var backgroundGroup = createGroup()
backgroundGroup.add(background)
drawSprites(backgroundGroup)
text("text", 200, 200)
var balloonGroup = createGroup()
balloonGroup.add(balloon)
drawSprites(balloonGroup)

Thank you for your reply but this does not work either.

Hi Denise,

The technique mentioned above should work- here is an example. You still need to draw the other sprites using drawSprites() and then use the drawSprite(pump) to layer the pumpkin on top. It is beyond the scope of the curriculum but does solve your problem of layering drawn sprites with text.

Michelle

1 Like

Thank you for the clarification. There was no mention of the “var countdraw=0” in the previous email. can you explain the purpose of this ? Also is line 24 the sprite group? I have never seen this as an available option in the toolbox in the past.
Thank you again.

Disregard the var countdraw. I’ve removed it. I was playing around and trying to draw the sprites in layers with a counter but it isn’t possible. drawSprites draws all sprites in a single layer. And the drawSprite(pump) is not in the toolbox. It is beyond the scope of the curriculum.
Michelle

Thank you. So technically there is no way for students to do this. Thanks again.

That just means it is not in the curriculum to learn it. You’ll have to teach it outside of a lesson. You would switch to “show text” and have them type it in. It is a good lesson in emphasizing the variable name for the sprite and how important that is. I think that is the cool part of Web Lab and Game Lab (and I’m sure App Lab - I just don’t know that as well). Even when students push past the curriculum, the tool will often accommodate them.
Michelle

Technically you can define drawSprite and make it a function if you want to “stay in the scope of the curriculum”

function drawSprite(sprite){
    if (!sprite.drawSpriteGroup) {
        var group = createGroup()
        group.add(sprite)
        sprite.drawSpriteGroup = group;
    }
    drawSprites(sprite.drawSpriteGroup)
}

Thank you!! I’ve been wondering how to do this for a while now. A student just came and asked so I Googled “code.org text between two sprites” and got this post. We were so excited when this worked :slight_smile: Thank you!

Thank you! So helpful.

This was so incredibly helpful. THANK YOU!

1 Like