Loading...
Logo
Processing Forum
Hello...
Basically I'm trying to load images into two classes. Here is my code.
I've managed to display a card using loadImage function. The next bit is how do I load two images that are assigned to the class.

int maxImages = 52;
int imageInd = 0;
PImage[] images = new PImage [maxImages];
Card[] cards;

void setup(){
  size(150, 200);
  for (int i=0; i <images.length; i++){
    images[i] =loadImage(""+i+".png");
    //images2[i]= loadImage(""+i+".png");
  }
  //for (int i=0; i<images.length; i++){
//cards[i] = new Card(x, y, images,[i], i);
    
  }

void draw(){
  image(images[imageInd],0,0);

  
}

class Card {
  // displays the card
  PImage img;
  float xP;
  float yP;
  float size;
  boolean matchedPairs;
  boolean show;
  int imgInd;

  Card (float x, float y, float size, PImage img, int imgInd) {
    matchedPairs = false;
    this.xP = x;
    this.yP = y;
    this.size = size;
    this.img = img;
    this.imgInd = imgInd;
  }

  void pairs() {
    matchedPairs = true;
  }
  
  void showCard(){
    show = true;
  }

  void displayCard() {
    if (matchedPairs || show) {
      image(img, xP, yP, size, size);
    }
  }
}


Replies(5)

Your question is rather difficult to understand. You only show one class... do you want to create two instances of that class? Is your array not working? (If so, that is because you aren't initializing it...)
Yes. I want two instances of the class. My array is working. I can loadImages and then they are displaying but how do I load images into the class. 

You have a PImage in the constructor of the Card, so you just pass the corresponding PImage as parameter.
for (int i=0; i<images.length; i++){
    cards[i] = new Card(i*50, height/2, 0, images[i],i);
  }

Okay. I think I'm not explaining the problem well enough. Basically this is what I want

Card1 is assigned to random image
Card2 is assigned to random image


Search shuffle in the forum(s) (search from Processing.org) to find out how to randomize the content of an array.