Why does a particular function won't work

edited March 2018 in Questions about Code

Hi,

If I should press the mouse, a card should be turned over with displayVoorkant. I can see the card is switching really fast, but shows not the image that it should. Can anyone tell me what I am doing wrong?

int []x = new int [24];
int [] y = new int[24];
int kaartX = 100;
int kaartY = kaartX;
int [] kw = new int [33];
int marge = 15;
int count = 0;
PImage achterkant;
int show = 0;
int kaartWaarde =0;
String[] kaartNaam = {"kaart0.png", "kaart1.png", "kaart2.png", "kaart3.png", "kaart4.png", "kaart5.png", "kaart6.png", "kaart7.png", "kaart8.png", "kaart9.png", 
  "kaart10.png", "kaart11.png", "kaart12.png", "kaart13.png", "kaart14.png", "kaart15.png", "kaart16.png", "kaart17.png", "kaart18.png", "kaart19.png", 
  "kaart20.png", "kaart21.png", "kaart22.png", "kaart23.png", "kaart24.png", "kaart25.png", "kaart26.png", "kaart27.png", "kaart28.png", "kaart29.png", 
  "kaart30.png", "kaart31.png", "kaart32.png"};



void setup() {
  int myX = 100;
  int myY = 100;
  int myKw = 0;
  size(800, 800);

  for (int i=0; i<24; i++) {
    y[i] = myY;
    x[i] = myX;
   kw[i] = i+1;
    if (myX<500) {
      myX +=115;
    } else if (myX>500) {
      myX=100;
      myY +=125;
    }
  }
}

void draw() {
  background(0);
  mouseOver();
  klik();
}

void mousePressed () {
 displayVoorkant();
}

void mouseOver() {
  for (int i = 0; i<24; i++) {
    achterkant = loadImage(kaartNaam[show]);
    imageMode(CENTER);
    image(achterkant, x[i], y[i], kaartX, kaartY);
    //fill(#E52709); //rood
    if (mouseX > x[i] && mouseX< (x[i]+ kaartX)
      && mouseY > y[i] && mouseY < (y[i]+kaartX)) {
      fill(#9FE509); //groen
      rectMode(CENTER);
      rect(x[i], y[i], kaartX, kaartY);
      println(kaartNaam[i]);
    } else if (mousePressed && mouseX > x[i] && mouseX< (x[i]+ kaartX)
      && mouseY > y[i] && mouseY < (y[i]+kaartX)) {
      displayVoorkant();
    }

    //fill(#E52709); //rood
  }
}

void displayVoorkant() {
  for (int i = 0; i<24; i++) {
    imageMode(CENTER);
    image(achterkant, x[i], y[i], kaartX, kaartY);
    if (mousePressed) {
      show = kaartWaarde;
      println(kaartWaarde);
    }
  }
}


void kaart(int x, int y, int kw) {
  kaartX = x;
  kaartY =y;
  kaartWaarde = kw;
} 


void klik() {
  for (int i = 0; i<24; i++) {
    if (mouseX > x[i] && mouseX< (x[i]+ kaartX)
      && mouseY > y[i] && mouseY < (y[i]+kaartX)) {
      if (mousePressed) {

        displayVoorkant();
        //fill(#72059B);
        rect(x[i], y[i], kaartX, kaartY);
      }
    }
  }
}

Answers

  • edited March 2018 Answer ✓

    Your draw() function calls your mouseOver() function, and your mouseOver() function calls loadImage().

    This is the most wrong thing about your code. It is ABSOLUTELY wrong and ABSOLUTELY the VERY FIRST thing you should fix.

    You should only be loading your images once. Things inside setup() only happen once. Thus, load all your images in setup() (and NO WHERE ELSE)!

  • edited March 2018 Answer ✓

    Draw runs 60 times a second. If you only do something for one frame then it'll just flick.

    You probably need to add some state to the card to say whether it's face up or face down.

  • edited March 2018 Answer ✓

    Line 49, don't load images within draw.

    You are loading 24 images, an expensive operation, every 60th of a second. Load them all in setup, reference them in draw.

Sign In or Register to comment.