PDA

View Full Version : [AS2] Dynamic array problem


fluke12
09-13-2009, 11:23 PM
I'm trying to write a card game. the code in question is below. I've set arrays for suit, number and number of card in the deck (aka, there is only one Ace of Spades in deck). The var "card" is used later to attach a movieclip and load the image on the stage.

theoretically, the second trace, should give you a number. My suspicion is that it becomes a string and doesn't trace the actual array value.

Any ideas on how to make this actually pull the array value?


Stop;
//setting names, point value and suits for all of the cards for images
card_name = new Array("2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A");
card_suit = new Array("Hearts", "Diamonds", "Spades", "Clubs");
Hearts_count = new Array(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1);
Spades_count = new Array(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1);
Diamonds_count = new Array(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1);
Clubs_count = new Array(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1);



//object to declare a card
cardObject = {number:card_name, suit:card_suit};


//random variables to get a random card
randomCardName = Math.floor(Math.random()*(13));
randomCardSuit = Math.floor(Math.random()*(4));

//gets specific card
card = String(cardObject.number[randomCardName]+"_"+cardObject.suit[randomCardSuit]);
card2=(cardObject.suit[randomCardSuit]+"_count["+[randomCardName]+"]");




trace(card)
trace(card2)

sam.uk.net
09-17-2009, 04:56 PM
Hi there,

To find out if it is or isn't, use actionscript 2's typeof() function. It returns the type of the variable. Usage:

trace(typeof(variable));

Example:


var myData="2";
trace(typeof(myData)); //traces string, because myData is in quotes



var myData=5;
if(typeof(myData)==string){
trace("Its a string!");
}
else if(typeof(myData)==number){
trace("Its a number!");
}

//this code will trace "Its a number!" to the output window


Hope that helps and good luck with your game! :)

sam.uk.net

rrh
09-17-2009, 05:23 PM
//you could store your suit counts in an object:
var bleh:Object = new Object();

bleh['Hearts']=Hearts_count;

//though there is an implicit object you can reference like so:
this[cardObject.suit[randomCardSuit]+"_count"][randomCardName]