Is this a bug, or am I doing something wrong?
in
Programming Questions
•
1 year ago
I'm just playing around building a simple game that shows and image and erases it as you drag your cursor over the image. To do this I'm using an array of rectangle objects that each monitor if they've been dragged over, and if they have been, turn white thus covering the background image. So I draw the blocks, set them to transparent fill(0,0); then change the fill to 255 when it is dragged over. This works fine when I set the blocks to black, then change them to white on drag, however if they are set to transparent then change to white the array or something goes CRAZY. Might be easier to see in code:
- PImage b;
- int canvasx = 500;
- int canvasy = 500;
- int cellsize = 50;
- int arraynumx = floor((canvasx/cellsize));
- int arraynumy = floor((canvasy/cellsize));
- sensor[] [] toucher = new sensor[cellsize] [cellsize];
- int counter = 0;
- void setup() {
- b = loadImage("dk.jpg");
- size(canvasx, canvasy);
- background(b);
- noStroke();
- for (int j = 0; j < cellsize-1; j ++) {
- for (int i = 0; i < cellsize-1; i ++) {
- toucher[i] [j] = new sensor(i*cellsize, j*cellsize);
- }
- }
- }
- void draw() {
- for (int j = 0; j < cellsize-1; j ++) {
- for (int i = 0; i < cellsize-1; i ++) {
- toucher[i] [j].display();
- }
- }
- }
- void mouseDragged() {
- for (int j = 0; j < cellsize-1; j ++) {
- for (int i = 0; i < cellsize-1; i ++) {
- toucher[i] [j].check();
- }
- }
- }
- class sensor {
- float x;
- float y;
- float touch = 0;
- sensor(float tempX, float tempY) {
- x = tempX;
- y = tempY;
- }
- void check() {
- float areax1 = x;
- float areax2 = x + (cellsize);
- float areay1 = y;
- float areay2 = y + (cellsize);
- if ((mouseX > areax1) && (mouseX < areax2) && (mouseY > areay1) && (mouseY < areay2) && (touch == 0)) {
- println("GOOD JOB!");
- touch = 1;
- counter += 1;
- }
- }
- void display() {
- if (touch == 0){
- fill(0,0);
- //The line above is the problem. Change it to fill(0); and it works perfectly...
- rect(x,y,x+(cellsize),y+(cellsize));
- } else {
- fill(255, 255);
- rect(x,y,x+(cellsize),y+(cellsize));
- }
- }
- }
1