This is to let everyone know that – thanks to Aaron’s help last week – I managed to fix the scoring glitch that I had in my game!
My game logic was missing a lot of elements here and there, which resulted in a massive bug that crashed the whole game.
Fixing a few functions inside the main class and adding id tracking variables and array in mousePressed() helped to resolve this issue.
void mousePressed() {
// play sound
click.play();
for (int i=0; i<cards.length; i++) {
// only flip cards over if there are less than 2 flipped
if (amountFlipped<2) {
// only flip if the card hasn't been flipped
// and also hasn't been matched (just to be safe)
if (!cards[i].flipped && !cards[i].matched) {
// check which card got clicked
if (cards[i].checkClicked()) {
// we've flipped it now so increase the amount that have been flipped
amountFlipped++;
}
}
}
}
}
// check the IDs of the flipped cards
void checkIDs() {
// make sure we have two flipped
// (already checked in mousePressed to make sure we're not counting matched cards)
if (amountFlipped == 2) {
// we need to keep track to make sure we check two cards
// this index does that (we already made sure it's only two just above)
int thisIndex = 0;
for (int i=0; i<cards.length; i++) {
// if the card is flipped, AND is NOT matched
if (cards[i].flipped && !cards[i].matched) {
// get that card's ID and its index
ids[thisIndex] = cards[i].id;
whichCard[thisIndex] = cards[i].index;
// increment our local index
thisIndex++;
}
}
// if we have something for both cards (otherwise one would be -1)
if (whichCard[0]>=0 && whichCard[1]>=0) {
// if they match set to matched and increase score
if (ids[0] == ids[1]) {
cards[whichCard[0]].matched = true;
cards[whichCard[1]].matched = true;
score++;
if (score == numCards/2){
scene = 2;
}
} else { // otherwise trigger the timer and reset things back to -1
cards[whichCard[0]].triggerTimer();
cards[whichCard[1]].triggerTimer();
ids = new int[]{-1, -1};
whichCard = new int[]{-1, -1};
}
// always reset the amountflipped back to 0 so we can start again
amountFlipped = 0;
}
}
}
Way to go!