We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpPrograms › Need help with flipping cards in processing
Page Index Toggle Pages: 1
Need help with flipping cards in processing (Read 3319 times)
Need help with flipping cards in processing
Jun 8th, 2010, 6:55am
 
Hey,

I'm new to processing and trying to make a memory game.
Now, I did manage to do a lot, but can't manage to build a system that does the following:

1. you turn two cards
2a. if it's wrong --> the cards flips back again and you hear a sound that indicates your wrong
2b. if it's right --> the cards flips back again and you hear a sound that indicates your right.

The problem I'm having that you now can turn as much as you want without anything to happen. When you press a card, a sound will be heared. The idea of the game is to find the two matching sounds Help please!

Greetz,
John

Download my files with this link:
http://dl.dropbox.com/u/6538035/memory.zip
Re: Need help with flipping cards in processing
Reply #1 - Jun 8th, 2010, 10:44am
 
I hope I'm making myself clear with the problem. If not, tell me, I will try te explain it better.

John
Re: Need help with flipping cards (memory)
Reply #2 - Jun 9th, 2010, 11:45am
 
*bump* anyone?
Re: Need help with flipping cards in processing
Reply #3 - Jun 9th, 2010, 2:32pm
 
Well, when you flip and expose the first card you need to keep a record of that card: you can just create a single global variable of type 'Card' to hold this - let's call it 'firstCard'.  In mouseClicked assign the first card clicked to firstCard and when a second card is clicked just set firstCard = null; that way you can just test if it's null and always know when a first card is being drawn:

Code:
void mouseClicked() {
  for(int i=0; i<nrOfCards; i++)  {
    // note I've used some shorthand here
    if(card[i].inside(mouseX,mouseY) && !card[i].exposed){
      card[i].setExposed(true);
      card[i].setPlaying(true);
     
      // the first card is drawn
      if(firstCard == null){
        // keep a record of the first card
        firstCard = card[i];
      }
      // now a second card is drawn...
      else {
          // check that the sounds match and act accordingly
          // ...

          // reset firstCard:
        firstCard = null;
      }
}


Since firstCard is a reference to a Card object you can access all the card's properties from it...  However this is where you run into problems, as currently you don't seem to have any way of identifying matching pairs.  At first I assumed that they'd have matching filenames, but they don't...  (Which seems odd as this isn't an efficient way to reduce filesize!)  So that's something you're going to have to address - either as an added property of the class or possibly at a global level.

I'd personally ditch the code that turns the card back over when it's clicked a second time.  That confuses the issue and in terms of feedback for failure it's easier to just flip the card back automatically (though you may need to write a new method that lets the associated music play for a set amount of time before stopping).

Hopefully that's enough to get you back on track...
Re: Need help with flipping cards in processing
Reply #4 - Jun 10th, 2010, 2:29am
 
This is getting way to complicated for me. I was thinking to recreate it for now. I hope you can help me with this so it is way easier for me.

I want just a function that when two cards are drawn, all cards return to close again (black collor). It does not matter if it is right or wrong.

Maybe by this I will understand it, because It's way to complex for me right now.

- John
Re: Need help with flipping cards in processing
Reply #5 - Jun 10th, 2010, 10:19am
 
I'm surprised - you've managed to create a class and add some general functionality, but you don't follow the addition of a simple condition?

Let me explain it a bit further.  When you create the 'firstCard' variable you don't assign it a value; so it returns 'null'.  So when the mouse is pressed over a card you can test if firstCard == null and you know this is the first card to be clicked on.  So you then store the Card that was clicked on in the 'firstCard' variable...

Now the next time you click on the card, firstCard is no longer null so you go to the 'else' part of the condition.  You now have a reference to the first card clicked and the second card clicked so you can then test an appropriate property for each card to see if the match and reset firstCard to null so a new pait of cards can be clicked on...

As I suggested before the most suitable property to compare would be the file name.  I'd find another way to set the this when creating the card objects.  Probably the simplest option would be to create an array of file names and make sure each audio file is referenced twice within the array.  You could then run a shuffle routine on the array to ensure the position of the cards is different each time...

If you're still struglling I can post working code; but since this could well be homework I'm loath to hand you the answer on a plate.  Have a go at implementing my suggestions and post your code here (no need to include the audio files for general questions)...
Re: Need help with flipping cards in processing
Reply #6 - Jun 10th, 2010, 1:21pm
 
I got some help with the classes. I left this project for a month now, and it needs to be done tomorrow. So that is the situation I'm in right now. It's a bit to late to change a lot of my code, because basicly I even lost track of my own code because of the help a month ago.

I'm trying to figure out what to do, but I guess it just has been to long to even see what the heck I was doing, lol.  Tongue It's stressing me out a bit.
Re: Need help with flipping cards in processing
Reply #7 - Jun 17th, 2010, 8:39am
 
Grin i exactly know what you mean..I have no clue what i hacked in there a week ago..but it's funny
Page Index Toggle Pages: 1