首页 > 解决方案 > 如何停止 if 语句

问题描述

我在这方面真的很糟糕,我试图对人们进出房间进行“模拟”。当 mouseX > width/2 时“新人进来”,当 mouseX > width/2 时,程序应该删除一个人。

它“有效”,但它永远不会停止添加/减去“人”。我不知道如何让它每次鼠标越过画布时只增加或减少一次。

这是代码:

String comesIn = "In";
String goesOut = "Out";

int n=0;

void setup() 
  {
  size(640, 360);
  }

void draw()
{
background(255);
line(320, 0, 320,360 );

    if(mouseX > width/2)
    {
      println("in");
      text(comesIn, (width/2)+(width/4), height/2);
      fill(50);
      n++;
      println("people = "+n);
    }
    if(mouseX < width/2)
    {
      println("out");
      text(goesOut, width/4, height/2);
      fill(50);
      n--;
      println("people = "+n);
    } 
}

谢谢你

标签: javascriptif-statementprocessing

解决方案


你可以做这样的事情;

String prevItem = "";

int n=0;

void setup() 
  {
    size(640, 360);
 }

 void draw()
  {
    background(255);
    line(320, 0, 320,360 );

if(mouseX > width/2 && prevItem !== "right")
{
  println("in");
  text(comesIn, (width/2)+(width/4), height/2);
  fill(50);
  n++;
  println("people = "+n);
  prevItem = "right";
}
if(mouseX < width/2  && prevItem !== "left")
{
  println("out");
  text(goesOut, width/4, height/2);
  fill(50);
  n--;
  println("people = "+n);
  prevItem = "left";
} 
}

推荐阅读