简体   繁体   English

使用 Javascript 使文本在代码中消失时出现问题

[英]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;我已经尝试了很多东西 - 把 text(A/B).visible = true; in an else statement.在 else 语句中。 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 设置为 255,则它是透明的。

    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.我认为这是因为当鼠标悬停在精灵上方时,顶部语句将可见性设置为 true,从而阻止底部语句正常工作。 Try adding an else statement, like this:尝试添加else语句,如下所示:

      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.如果这不起作用,请确保可见性 function 实际上使精灵不可见。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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