javascript - 如何停止 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);
}
}
谢谢你
解决方案
你可以做这样的事情;
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";
}
}
推荐阅读
- java - 字符串索引超出范围:0 在 Spring Boot 中使用 RedirectAttributes 时出错
- formatting - 更改 PyCharm 中 Black 插件的 88 个字符限制
- javascript - 在动态创建的 AJAX 元素上使用 $(window) 进行事件绑定
- http - HTTP Chrome 请求中的 CRLF
- r - 响应多个按钮的闪亮模态对话框
- javascript - API 调用后撤消 preventDefault()
- amazon-web-services - Amazon MQ 无法在 CloudWatch 中发布日志
- sql - 对 sql-server 表中的每一行使用序列
- jquery - Microsoft Edge 中的 :enabled 选择器
- angular - Angular 7加载登录后组件非常缓慢