Unit 3 Lesson 17 Questions

I have a student who is trying to put text on his second part of his card. The page changes and the color and gradient are fine, but we can not get the text to show up? I can not figure out what we are doing wrong? Any help would be great…Here is the code:

var envelope_1 = createSprite(200, 200);
function draw() {
if (keyDown(“space”)) {
var gradient_01_1 = createSprite(200, 200);
text(“Congradulations On Your Birth!”, 45, 200);
} else {
envelope_1.x = randomNumber(195, 205);
envelope_1.y = randomNumber(195, 205);

HI @lschlag!

Welcome to the forum!! Usually text issues can be solved by moving the drawSprites() block above where the text will be drawn. In most cases, you only need one drawSprites() in the draw loop so it could go after you draw the background. The other thing I notice is that you created a sprite in the draw loop. It’s hard to tell what the sprite is without a link to the project but I am guessing it is a background gradient that you only want to show up in when the space key is pressed. By creating it using the var in the draw loop, it is essentially redrawing it over text each time through the draw loop and could be covering up the text. You can solve this issue by creating it at the top of the code and setting visible to false. Then, in the if statement, set the visible to true.

See if those tips help. If not, let us know and send a link to the project so we can take a closer look. You can do this by clicking SHARE and “copy link to project”.


It still is not working? How do I send a link to you of the project?

I moved all your sprites and animation to the top of the code and set visible to “false” for the gradient. Then, when the space key is pressed, set visible to “true”. I also moved the drawSprite to above the if statement and removed the second drawSprite in the else statement.

Thank you. Where can I locate the changes that you made to the code?


The “drawSprites” command on line 15 is the last line of code that is executed each time through the draw loop regardless of whether or not the space key is pressed. Therefore, once you hit the space bar, it draws the gradient, then draws the text and then draws the gradient again. You may want to look at where you put the drawSprites. Normally, there would only be one case of that, but you could include one inside each conditional and that could also work depending on what you are trying to do.

Hope this helps, but check back in if you still aren’t able to get it to work.