Lesson 17 - Switching text three times

My student wants three different pieces of text to show up at different times. We got the first text: Press the ghost and the second text: Nice, after you do that press the up arrow key and the alien will show! The student wants the second text to disappear and only have the third text: Hurray, Love you mom and dad! (alien moves back and forth with arrow keys) when the up arrow key is pressed.

Thanks!

1 Like

Good day @jennifer.hemry,

In regards to your issue of showing text, there’s also an issue of the user triggering the events beforehand completely breaking what the student is trying to present, I’d possibly recommend using a placeholder variable to check the current state of an action that the user has taken in order to progress to show the text on the screen, I’ll link the code that I’ve modified down below

// Define a variable called 'stage' and set it to 0
var stage = 0;

// Set the text size to 23, set the fill color to white, and hide the cursor
textSize(23);
fill(255);
noCursor();

function draw() {
  // Draw any sprites on the canvas
  drawSprites();

  // Move the UFO sprite to the current mouse position
  UFO.x = World.mouseX;
  UFO.y = World.mouseY;

  // If we're in stage 0...
  if (stage === 0) {
    // Display the text "Press the ghost!" at position (25, 300)
    text("Press the ghost!", 25, 300);

    // If the left arrow key is pressed...
    if (keyWentDown("left")) {
      // Move the ghost sprite 5 pixels to the left
      ghost.x -= 5;
    }

    // If the right arrow key is pressed...
    if (keyWentDown("right")) {
      // Move the ghost sprite 5 pixels to the right
      ghost.x += 5;
    }

    // If the mouse is clicked on the ghost sprite...
    if (mousePressedOver(ghost)) {
      // Increase the stage counter by 1
      stage += 1;

      // Hide the ghost sprite
      ghost.visible = false;

      // Show the blowup sprite
      blowup.visible = true;
    }
  }

  // Otherwise, if we're in stage 1...
  else if (stage === 1) {
    // Display some text with line breaks at position (25, 300)
    text("Nice, after you do that\n\
press the up arrow key and the alien\n\
will show!", 25, 300);

    // If the up arrow key is released...
    if (keyWentUp("up")) {
      // Hide the blowup sprite
      blowup.visible = false;

      // Show the alien sprite
      alien.visible = true;

      // Increase the stage counter by 1
      stage += 1;
    }
  }

  // Otherwise, we must be in stage 2...
  else {
    // If the left arrow key is pressed...
    if (keyWentDown("left")) {
      // Move the alien sprite 5 pixels to the left
      alien.x -= 5;
    }

    // If the right arrow key is pressed...
    if (keyWentDown("right")) {
      // Move the alien sprite 5 pixels to the right
      alien.x += 5;
    }

    // Display some text at position (50, 50)
    text("Hurray, Love you mom and dad!", 50, 50);

    // Display some more text with line breaks at position (25, 100)
    text("(alien moves back and forth\n\
with arrow keys)", 25, 100);
  }
}

I hope this helps if not I’m sure others would be willing to provide a better solution!

All the best, Varrience

@jennifer.hemry,

I agree with @varrience on this one. If you have a variable that tracks what stage or what “level” you are on, it should solve the problem. Hopefully their explanation is easy to follow, but if you run into any problems implementing it, please feel free to check back in and we’re happy to help you figure it out!

Mike