【问题标题】:Processing Code does not work处理代码不起作用
【发布时间】:2013-03-30 13:08:54
【问题描述】:

我今天开始使用 Processing 进行编程,并编写了一个创建 10 个随机矩形的小程序 现在我想让它们在鼠标悬停时消失,但我的实际代码不起作用

我会欣赏一些小费...

import java.awt.Rectangle; Rectangle rect[] = new Rectangle[10]; int xpos[] = new int[10]; int ypos[] = new int[10]; int size = 25; boolean visible[] = new boolean[10]; void setup() { size(640,480); frameRate(60); smooth(); background(0); stroke(255); fill(255); textAlign(CENTER); textSize(200); text("Catch", width/2, 280); textSize(100); text("them", width/2, 380); // 10 Random positions for the rectangles for (int i=0; i < 10; i++) { xpos[i] = int(random (615)); ypos[i] = int(random (455)); visible[i] = true; } for (int i=0; i < 10; i++) { rect[i] = new Rectangle(xpos[i],ypos[i],size,size); } } void draw() { for (int i=0; i < 10; i++) { if (visible[i] == true){ fill(255,0,0); rect(rect[i].x,rect[i].y,rect[i].width,rect[i].height);} else if (rect[i].contains(mouseX,mouseY)){ visible[i] = false; } }}

【问题讨论】:

    标签: boolean processing collision rectangles


    【解决方案1】:

    为什么是else if?它的写法是,如果visible[i] == false,它只会检查鼠标是否在一个矩形上。它们都是可见的,因此永远不会被执行。

    还要看到效果,你必须在你的draw方法的顶部调用background(0);。否则,您永远不会清除屏幕以查看结果。

    您还应该考虑清理缩进和大括号 {} 以确保以一致的方式格式化代码。这样会更容易阅读。

    【讨论】:

    • 非常感谢 ^^ 我只添加了 background(0) 行,它工作得很好
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多