I am trying to make a game such as concentration for my class. I am having trouble with taking information that is supplied as inputs in a function and saving them to a variable. Here is the working version of what I am trying to do: Working Version
Here is the code from my card component:
<fx:Script>
<![CDATA[
private var _frontFile:String;
private var _backFile:String;
public function makeCard(suit:String, value:String):void
{
_frontFile = "asset/Cards_deck_" + suit + "_" + value + ".jpg";
_backFile = "asset/Cards_back_Jaguar.jpg";
var _suit:String = suit;
var _value:String = value;
}
public function turnUp():void
{
source = _frontFile;
}
public function turnDown():void
{
source = _backFile;
}
public function turnOver():void
{
if (source == _frontFile) {
source = _backFile;
}
else if (source == _backFile) {
source = _frontFile;
}
}
]]>
</fx:Script>
</s:Image>
My main MXML file:
<fx:Style>
@namespace s "library://ns.adobe.com/flex/spark";
@namespace mx "library://ns.adobe.com/flex/mx";
s|Application {
backgroundColor: #006600;
}
#titleLbl {
color: white;
fontSize: 60px;
paddingTop: 15;
}
#infoLbl {
color: yellow;
fontSize: 24px;
}
</fx:Style>
<fx:Script>
<![CDATA[
//Gabe Dougherty
//n222
//1-24-13
import cardGames.*;
private var deck:Deck = new Deck();
private function init():void
{
// Let's exclude the face cards.
deck.makeDeck(false);
deck.shuffle();
// Deal the cards.
var cardCount:int = deck.numCards();
for (var i:int = 0; i < cardCount; i++) {
// Get a card and add it to 'playArea'.
var c:Card = deck.dealCard();
playArea.addElement(c);
// Get the card ready for play.
c.turnDown();
c.addEventListener(MouseEvent.CLICK, chooseCard);
}
}
private function chooseCard(e:MouseEvent):void
{
var card:Card = Card(e.currentTarget);
card.turnUp();
// Report on what type of card.
infoLbl.text = "The card is the " + card.value + " of " + card.suit;
}
]]>
</fx:Script>
<s:Label id="titleLbl" text="Concentration (sort of)"/>
<s:Label id="infoLbl" text="The card is ..."/>
<s:TileGroup id="playArea" requestedRowCount="4"/>
</s:Application>
And then my deck component if needed:
<fx:Script>
<![CDATA[
//set the properties
private var _suitNames:Array = ['clubs', 'diamond', 'heart', 'spade'];
private var _valueNames:Array = ['2', '3', '4', '5', '6', '7', '8', '9', '10'];
private var _faceNames:Array = ['jack', 'queen', 'king', 'ace'];
private var _cards:Array = new Array;
//set the methods
public function dealCard():Card {
return _cards.pop();
}
public function numCards():Number {
return _cards.length;
}
public function shuffle():void {
for (var i:int = 0; i < _cards.length; i++) {
// Take the last card from the deck.
var c:Card = _cards.pop();
// Insert the card back towards the front of the deck, at a random location.
var index:int = Math.random() * (i + 1);
_cards.splice(index, 0, c);
}
}
public function makeDeck(includeFaces:Boolean=true):void {
for each (var suit:String in _suitNames) {
for each (var value:String in _valueNames) {
var card:Card = new Card();
card.makeCard(suit, value);
_cards.push (card);
}
}
if(includeFaces == true){
for each (var suit:String in _suitNames) {
for each (var value:String in _faceNames) {
var card:Card = new Card();
card.makeCard(suit, value);
_cards.push (card);
}
}
}
}
]]>
</fx:Script>
</fx:Object>
If you declare a variable inside a function then its scope is local to that function. You can’t use it outside the function–it doesn’t exist.
So you’ll need to move those declarations for
_suitand_valueoutside of themakeCardfunction if you want them to be accessible by the rest of the class:But…
Since those are private variables, you won’t be able to access them from outside the Card class. You could just make them public variables, but a more flexible way is to add accessor methods to the class. These are public functions that let other classes access the values:
Now your program should work.
If you need to be able to change those values from outside the Card class, you’ll need to write public ‘setter’ methods to modify the values of the private variables.