Reputation: 89
i am trying to flip individual cards when i click on them. At the moment when i click all the cards flip at the same time.
here is my code. any help would be appreciated and thank you very much in advance.
package cards;
import processing.core.PApplet;
public class MemoryGame extends PApplet {
static final int COLS=2;
static final int RAWS=3;
Card[] cards;
public void setup() {
size(RAWS*Card.WIDTH, COLS*Card.HEIGHT); // card witdth 60*9+(10*9) for the gaps=630
cards = new Card[COLS*RAWS];
cards[0] = new Card(11, 0, 0);
cards[1] = new Card(3, Card.WIDTH, 0);
cards[2] = new Card(7, 2 * Card.WIDTH, 0);
cards[3] = new Card(3, 0, Card.HEIGHT);
cards[4] = new Card(7, Card.WIDTH, Card.HEIGHT);
cards[5] = new Card(11, 2 * Card.WIDTH, Card.HEIGHT);
}
public void draw() {
background(204);
for (int i = 0; i < 6; i++) {
cards[i].display(this);
}
}
public void mousePressed() {
for (int i = 0; i < 6; i++) {
cards[i].flip();
}
}
public static void main(String[] args) {
PApplet.main("cards.MemoryGame");
}
}
++++++++++++++++++++Card class++++++++++++++++++++++++++++++++++++++++++++++++++++++
package cards;
import processing.core.PApplet;
public class Card {
boolean shown=false;
static final int WIDTH = 120;
static final int HEIGHT = 180;
static final int gap = 20;
int value;
float x,y;
Card(int _v, float _x, float _y) {
value = _v;
x = _x;
y = _y;
}
public void display(PApplet applet) {
applet.stroke(0);
applet.strokeWeight(2);
if (shown) {
applet.fill(100);
applet.rect(x, y, WIDTH, HEIGHT,WIDTH/5);
applet.textAlign(applet.CENTER,applet.CENTER);
applet.textSize(WIDTH/2);
applet.fill(0);
applet.text(value,x+WIDTH/2,y+HEIGHT/2);
} else {
applet.fill(255);
applet.rect(x, y, WIDTH, HEIGHT,WIDTH/5);
}
}
public void flip() {
shown=!shown;
}
}
Upvotes: 0
Views: 3267
Reputation: 76
You need to get the mouse event in your mousePressed() method, that way you can determine which card has been clicked so you can flip it. Try implementing the java.awt.MouseListener interface in your MemoryGame class.
Upvotes: 1
Reputation: 298
You are flipping all cards when mouse button is pressed. You have to check if the mouse position is intersecting the card and the mouse is pressed, if so, flip that card. Make methods mouseX() and mouseY() which gives you mouse coordinates. And just add if(mouse position is intersecting the card):
public void mousePressed() {
for (int i = 0; i < 6; i++) {
if(mouseX() >= cards[i].x && mouseX() <= cards[i].x+cards[i].width && mouseY() >= cards[i].y && mouseY() <= cards[i].y+card[i].height) {
cards[i].flip();
}
}
}
Upvotes: 1
Reputation: 73578
You loop through all the cards in your mousePressed
method and flip them. What did you expect to happen?
I imagine you want to check where the user clicked and then flip the card that is in that location.
Upvotes: 1
Reputation: 356
That's because you loop trough every card when you press one card.
public void mousePressed() {
for (int i = 0; i < 6; i++) {
cards[i].flip();
}
}
Try defining which card you clicked and use the flip method only for that card.
Upvotes: 1