简体   繁体   中英

Trouble making text disappear in code using Javascript

I'm simply trying to get the text to disappear when the mouse hovers over a sprite, and it won't do that. I've tried many things - putting text(A/B).visible = true; in an else statement. I've moved my text code just about everywhere in my program. It just won't disappear.

var alien = createSprite(100, 200);
alien.setAnimation("alienYellow_jump_1");
var bubble = createSprite(225, 250);
bubble.setAnimation("comictextbubble.png_1");
var button = createSprite(105, 275);
button.setAnimation("play_1");
function draw() {
  background("lightblue");
  if (mouseIsOver(alien)){
    alien.rotation = randomNumber(10, -10);
  }
  button.visible = false;
  if (mouseIsOver(button)){
    button.visible = true;
  }
  if (mousePressedOver(button)){
    bubble.visible = false;
  }
  drawSprites();
  stroke("black");
  textSize(20);
  var textA = text("Find the secret button to reveal your card!", 10, 207);
  var textB = text("It's your birthday!", 115, 150);
  if (mouseIsOver(button)){
    textB.visible = false;
    textA.visible = false;
  }
  console.log(textA.visible);
  console.log(textB.visible);
}```

Did you try if you move your mouse over the sprite, the fill color for the text becomes transparent, if you set the 4th parameter of the fill function to 255, then it's transparent.

    function draw(){

     var Red = 255;
    var Blue = 255;
    var Green = 255;
    var Alpha = 0;
    fill(Red,Green,Blue,Alpha);
    text("Move mouse over me to disappear",200,200);
    if(mouseX>175&&mouseX<225&&mouseY>175&&mouseY<225)
        {
    Alpha=255;
        }

    }

I would assume that's because the top statement is setting the visibility to true when the mouse is over the sprite, preventing the bottom statement from working properly. Try adding an else statement, like this:

      if (mouseIsOver(button)){
    button.visible = true;
  } else if (mousePressedOver(button)){
    bubble.visible = false;
  }

If that doesn't work, make sure that the visibility function is actually making the sprite invisible.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM