PDA

View Full Version : Basic Classes Question



dstanford
August 9th, 2007, 07:02 PM
I've been doing fairly complex stuff with AS 2.0 for years, but I always avoided creating external classes because I never had a project where it seemed that beneficial. To ease my way into AS 3.0 and to get over my fear of classes, I thought I'd do a few tutorials and try to create a memory card game using only external class scripts. So, I've been following this tutorial on Lynda.com. The tutorial guy started by creating a Card class that is associated with a card movie clip in the library, and it's only purpose is to detect when a card is clicked and make it flip over. Then, he created a MemoryGame class and assigned it as the document class for the MemoryCards FLA file. The MemoryGame class handles attaching the cards to the stage and detecting matches.

So here's what is weird to me. When each card is attached to the stage in MemoryCards.as, the tutorial tells me to create an event listener for every card to listen to see when the card is clicked and call the matchCheck function if it is clicked. This seems dumb to me because in Card.as, there's already a mouse event listener that allows each card to flip when it is clicked. I figured I'd just try calling matchCheck from Card.as. That was a no go because matchCheck wasn't defined in Card.as. (Is it clear yet how little I understand about how classes can or can't communicate with each other?) So, I thought I'd try moving the matchCheck conditions over to Card.as. So I wound up with this:

package
{
import flash.display.MovieClip;
import flash.events.MouseEvent;

public class Card extends MovieClip
{
public var cardPairNumber:Number;
public var flippedCardPairNumber1:Number = -1;
public var flippedCardPairNumber2:Number = -1;

public function Card()
{
this.buttonMode = true;
this.addEventListener(MouseEvent.CLICK, onClick);
}

private function onClick(event:MouseEvent):void
{
//flip card by playing the flip animation on its timeline
this.play();

//detect match
if (flippedCardPairNumber1 == -1 && flippedCardPairNumber2 == -1)
{
flippedCardPairNumber1 = cardPairNumber;

} else if (flippedCardPairNumber1 !== -1 && flippedCardPairNumber2 == -1)

{

flippedCardPairNumber2 = this.cardPairNumber;
if (flippedCardPairNumber1 == flippedCardPairNumber2)
{
trace("match!");
this.gotoAndPlay("flipToBack");
flippedCardPairNumber1 = -1;
flippedCardPairNumber2 = -1;
} else
{
trace("no match");
this.gotoAndPlay("flipToFront");
flippedCardPairNumber1 = -1;
flippedCardPairNumber2 = -1;
}
}
}
}
}

That seemed better until I realized that flippedCardPairNumber1 and flippedCardPairNumber2 variables were being created for each card rather than being at the "root" level (or stage level or whatever the hell it is now). So, I could flip every card on the stage and never get the match detection to happen because each card was holding its own info about which card number was flipped last.

I guess this means the tutorial guy was right and the best thing to do is just assign a new mouse event listener to every card as it's attached and put all the match checking conditions and flipped card tracking variables in MemoryCards.as. I just want to make sure that I'm not missing some really simple way of getting the classes to talk to each other before I proceed with the tutorial way of doing things. I know you can import one class into another or make a class an extension of another class, but that didn't seem like the right answer for this situation because MemoryCards.as is the document class and it imports the Card.as class (so I assume I shouldn't then try to also import MemoryCards.as into Card.as, since that seems to defeat the purpose of having a document-level class and smaller classes with more limited purposes).

Also, can someone tell me if I'm an idiot for setting flippedCardPairNumber1 = -1 and flippedCardPairNumber2 = -1 as their initial values? In AS 2.0, I would have just left them undefined and then checked to see if they were undefined in my if statements, but AS 3.0 says a Number variable can't be set as undefined. So I just figured I'd set -1 as their value to indicate no cards have been flipped, since I knew none of my cards would ever have a negative pair number. Thanks in advance!

devonair
August 9th, 2007, 07:50 PM
setting flippedCardPairNumber1 and flippedCardPairNumber2 to -1 just seems a quick and easy way to look at the script and see that those are not flipped. May be better to use a constant like private const UNFLIPPED:Number = -1 and set them to UNFLIPPED.. Seems like a lot of exposed (public) properties in that class, but it's really hard to tell why or answer any other questions without seeing all of the script and maybe the reasoning behind it.. Is this a free tutorial at Lynda, or pay?