| |||||||||||||||||||||||||||
|
| |||||||||||||||||||||||||||
Print This Article REALbasic University: Column 032
RBU Pyramid VIn part IV we got RBU Pyramid to let the user select cards and even detect matches, but we didn't do anything with those matches. Let's fix that today.
Removing Card MatchesOpen your most recent version of the project and go to the cardMatch method within gameWindow. Delete the temporary code there and replace it with the following: // "Delete" the matched cards Pretty simple, eh? This hides the two selected cards and increases the score by two (one point for each card removed). When we're done we call updateCards to update which cards are clickable. Note that we have introduced a new global variable here -- gScore -- which represents the player's score. Let's add that to globalsModule before we forget. Open globalsModule (double-click on it) and add a new property (Edit menu, "New Property"): gScore as integer. Now let's tackle the more complicated routines. Back in gameWindow, go to the discardMatch method. Remember, this is the match routine that's called when one or more of the selected cards is on a discard pile. Why do we need a separate routine for that contingency? Because we don't want to remove (hide) a card on a discard pile. Here's the code for discardMatch:
This routine has to do several things: if first hides the non-discard match (if any), and then it uses the top card on the discard pile. If it's the temp discard pile, which can only hold one card, it removes it and that's it. However, if the match is from the main discard pile, it must remove the top card and set the discard pile to display the next card in the pile. It does this by looking at gTheDiscard to see if uBound is not zero. If it isn't, then it sets gDiscard.card (the actual cardCanvas object of the main discard pile) to the last card in gTheDiscard. What happens when a player clicks on a king card is very similar but different enough it warrants its own routine. Put this code into the cardKing method:
Note that we first determine if the king match is one of the discards or not. If it isn't, we just remove (hide) it. If it is a discard, we handle it in a similar manner to the way we did in discardMatch. Good. Try running the program now: it should work and let you remove matches (including kings). However, since we don't have the Deck working, we can't tell if that code works yet since there are no discards. Let's get that Deck working!
Enabling the DeckGetting the Deck working might not seem complicated, but it is. The Deck affects many other aspects of the program, such as both discard piles. To get it going we'll need to add some code to our existing routines as well as write some new ones. So pay close attention as we'll be jumping around a bit. First, we need to draw the card deck. Go to our drawCard routine and find the comment: // Deck gets drawn here. Add the following code after that, but before the final end if line.
This drawing routine is simple. We first check to see if gTheDeck has cards left. If it does, we simply draw the back of a card (a gray box). We draw the card back with black if reverse is true (meaning the card is selected -- more on that later). More complicated is what happens when the deck is empty: here we draw an "R" if the deck is ready to be redealt, or "D" if we've exhausted the deck for the second time (no redeal permitted). How do we tell if the deck is on its second deal? Ah! Sharp-eyed readers will note that we've introduced a new variable to track this. It's a boolean (either true or false) telling us if we're on the first run-through or not. Open globalsModule and add this property: gFirstTimeThrough as boolean. That's the property we check in the above to tell if we draw an "R" or a "D". Our next step in activating the deck is that we need to detect when a person clicks on the deck. Detecting this means we need to modify some code we wrote earlier. Open cardCanvas' mouseDown routine and scroll to the very bottom. Add this code:
This will detect if the user clicks on the deck and the deck has cards left. If so, it calls a method called deckAdvance. Speaking of that, let's add that method now and put this code in it:
What does all this do? Well, first it deselects any existing card selection. Next, we check to see if there are cards left in the discard pile. If there aren't, we reset the deck (moving the discard cards back into the deck). If there are cards left, we deal out the top card to the temp discard pile. If the temp discard pile already has a card on it, we first move it to the main discard pile. Finally, we draw the deck twice: once as selected (reverse mode), and once not. That will give the player visual feedback for when they click on the deck. As part of this code we are calling a routine named resetDeck. We won't add the code for that until next week, but just so we can get this to compile, add a new method to gameWindow called resetDeck (leave the contents blank). If you run RBU Pyramid right now, it should work. You should be able to remove matching cards, either from the discards and/or the pyramid, and the deck should advance when you click on it. However, you'll note that the deck won't reset: we'll fix that next week. If you would like the REALbasic project file for this week's tutorial, you may download it here.
Next WeekWe'll get the deck to redeal, and we'll update the scoring system so that our pyramid game is playable.
NewsMatt Neuburg, author of REALbasic: The Definitive Guide, has written an interesting article for O'Reilly on REALbasic for HyperCard users. In other news, volunteers are hard at work translating REALbasic University into other languages. Floris van Sandwijk has already translated the first few columns into Dutch, and Kazuo Ishizuka is working on a Japanese version. I'd like to publicly thank these two for their incredible work: translating technical material like programming tutorials is tough. Please let them know you appreciate their efforts! Also, if you notice errors, contact them directly rather than notifying me. (If you are interested in translating RBU columns into another language, please contact me for permission and instructions.)
Letters
Thanks for the letter, Mike. I haven't had much experience scripting Quark XPress -- AppleScript's syntax is, frankly, bizarre. The language changes with each app you attempt to script! Debugging is worse than frustrating: it's impossible. PageMaker uses a proprietary language but I still find it easier than using AppleScript to automate InDesign or Quark. As to your REALbasic problem, I'm not positive without seeing more of your code, but I suspect your problem comes from failing to instantiate your cSheet object. Remember, you not only need to allocate space in the array for the object, but you must use the new command to generate a new instance of the cSheet object. For example:
You only need to new an object once, but since an array is made up of multiple objects, you must do it for each object in the array. The rule of thumb: any class object must be instantiated with the new command before you do anything with it. If that's not your problem, there are some other possibilities:
It's an interesting problem: let me know if you figure it out. (By the way, since I work in the prepress industry, I'd be curious to see your finished app if you're game. I don't use Preps now, though I did at one time.) Keep those letters coming! I may not answer your question immediately, but I'll hopefully get around to it in a future column. (If you don't want your correspondence published, just be sure to indicate that when you write. Otherwise I'll assume it's fair game.) About the Column REALbasic University is a weekly instructional column on programming with REALbasic and is brought to you by REALbasic Developer, the magazine for REALbasic programmers. Each week we answer select reader questions, and we're always open to ideas for future columns. Send your questions to . (Keep your questions simple and specific. General queries like "How do I write my own web browser?" will be neglected.) Your question won't be answered immediately, but will be answered in a future column. (If you don't want your correspondence published, just be sure to indicate that when you write. Otherwise it's fair game.) About the Author See the REALbasic University Archives
REALbasic University contents ©2001-2004 by Marc Zeedar and REALbasic Developer. All Rights Reserved.
|
. |
| |||||||||||||||||||||||||