Sprite visible, not visible

I have a student that cannot get the sprite visible property to work correctly. Here is the code.

so I think i know what your going for and put the revised comments below with this your program should work as intended

// it may be wise to relabel these variables, enforcing best practices early is good
var sprite = createSprite(200, 200);
sprite.setAnimation("bg_landscape10_1");
var sprite2 = createSprite(200, 200);
sprite2.setAnimation("bg_underwater_02_1");
// i assume these will be background layers so they need to be drawn first
// putting them in a group like this will make is simple
var backgrounds = createGroup();
backgrounds.add(sprite);
backgrounds.add(sprite2);
// you'll probably want to add more so you'll probably need a foreground group
// to achive a person going swimming or perhaps standing at the beach
var foreground = createGroup();
// add foreground sprites here
function draw() {
  // draws the current background first
  drawSprites(backgrounds);
  // event visible code
  if (keyDown("space")) {
    sprite.visible = false;
    sprite2.visible = true;
  } else {
    textSize(30);
    text("Hold space to go swimming", 20, 200);
    sprite.visible = true;
    sprite2.visible = false;
  }
  // additional foreground sprites if necessary
  drawSprites(foreground);
}
// drawSprites() is not continuos outside of the draw loop

tldr; drawSprites was not in the draw loop and wasn’t set up for possible foreground attributes

Best Varrience

@mweaver,

Yep! @varrience is correct and gave some great ideas and tips! The heart of the issue is moving the drawSprite into the function draw(). To make sure the text appears, it will need to be placed before the text block.

~Michelle