4 Replies - 593 Views - Last Post: 23 March 2013 - 05:04 PM Rate Topic: -----

#1 hashmi82  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 23-March 13

AS3 shuffle problem 1120 undefined property

Posted 23 March 2013 - 04:03 PM

Hello All,

Please can you help me out I am new to as3 and I am trying to create a shuffled deck using the Fisher-Yates Algorithm. When I run the code with ctrl-enter it compiles with no errors but when I try to output it with trace(); it comes back with:

Scene 1, Layer 'actions', Frame 1, Line 6 1120: Access of undefined property shuffledArray.

Like I said I am new to this and it will be me doing something very stupid but all the same i'm stuck.

Here is the code
package src.CardDeck
{
	public class CardDeck
	{
		public var allCards:Array = [];
		public var cardNames:Array;
		public var cardValues:Array;
		public var gameType:String;
		public var drawnCards:uint = 0;

		public function CardDeck(game:String)
		{
			gameType = game;
			cardNames = ["Ace","Two","Three",
						 "Four","Five","Six",
						 "Seven","Eight","Nine",
						 "Ten","Jack","Queen","King"];
			if(gameType == "texasholdem")
			{
				cardValues = [1,2,3,4,5,6,7,8,9,10,10,10,10];
			}
			makeSuit("Spade");
			makeSuit("Heart");
			makeSuit("Diamond");
			makeSuit("Club");
		}
		private function makeSuit(suitString:String):void
		{
			var card:Object;
			
			for(var i:uint = 0; i < cardNames.length; i++)
			{
				card = {};
				card.cardType = suitString;
				card.cardName = cardNames[i];
				card.cardValue = cardValues[i];
				card.isDrawn = false;
				allCards.push(card);
			}
		}
		
		public function shuffleFisherYates():Array 
		{
			var shuffledArray:Array = [];
			var randomCardIndex: int;
    		do 
			{
        		randomCardIndex = Math.floor(Math.random()* allCards.length);
        		shuffledArray.push(allCards[randomCardIndex]); // add to mix
        		allCards.splice(randomCardIndex,1); // remove from deck
    		}while(allCards.length); // Meaning while allCards.length != 0
    		return shuffledArray;
			
			
		}
	}
}


and here is the .fla actions layer
import src.CardDeck.CardDeck;

var deck:CardDeck = new CardDeck("texasholdem");
trace(shuffledArray);



I know its probably something silly but i'm struggling.

Thanks in advance!

Paul

This post has been edited by ButchDean: 23 March 2013 - 04:37 PM
Reason for edit:: Fixed code tags.


Is This A Good Question/Topic? 0
  • +

Replies To: AS3 shuffle problem 1120 undefined property

#2 hashmi82  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 23-March 13

Re: AS3 shuffle problem 1120 undefined property

Posted 23 March 2013 - 04:33 PM

code put in brackets properly! bit of a duh moment sorry.
package src.CardDeck
{
	public class CardDeck
	{
		public var allCards:Array = [];
		public var cardNames:Array;
		public var cardValues:Array;
		public var gameType:String;
		public var drawnCards:uint = 0;

		public function CardDeck(game:String)
		{
			gameType = game;
			cardNames = ["Ace","Two","Three",
						 "Four","Five","Six",
						 "Seven","Eight","Nine",
						 "Ten","Jack","Queen","King"];
			if(gameType == "texasholdem")
			{
				cardValues = [1,2,3,4,5,6,7,8,9,10,10,10,10];
			}
			makeSuit("Spade");
			makeSuit("Heart");
			makeSuit("Diamond");
			makeSuit("Club");
		}
		private function makeSuit(suitString:String):void
		{
			var card:Object;
			
			for(var i:uint = 0; i < cardNames.length; i++)
			{
				card = {};
				card.cardType = suitString;
				card.cardName = cardNames[i];
				card.cardValue = cardValues[i];
				card.isDrawn = false;
				allCards.push(card);
			}
		}
		
		public function shuffleFisherYates():Array 
		{
			var shuffledArray:Array = [];
			var randomCardIndex: int;
    		do 
			{
        		randomCardIndex = Math.floor(Math.random()* allCards.length);
        		shuffledArray.push(allCards[randomCardIndex]); // add to mix
        		allCards.splice(randomCardIndex,1); // remove from deck
    		}while(allCards.length); // Meaning while allCards.length != 0
    		return shuffledArray;
			
			
		}
	}
}


.fla
import src.CardDeck.CardDeck;

var deck:CardDeck = new CardDeck("texasholdem");
trace(shuffledArray);


Was This Post Helpful? 0
  • +
  • -

#3 anonymous26  Icon User is offline

  • D.I.C Lover

Reputation: 1
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

Re: AS3 shuffle problem 1120 undefined property

Posted 23 March 2013 - 04:40 PM

It looks to me that you have declared shuffledArray in function scope, then attempted to use it in your .fla file where it cannot be seen, and is therefore undefined at that point.
Was This Post Helpful? 0
  • +
  • -

#4 hashmi82  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 23-March 13

Re: AS3 shuffle problem 1120 undefined property

Posted 23 March 2013 - 04:48 PM

Hi there,

Yeah got rid of the error now by changing the .fla to

import src.CardDeck.CardDeck;

var deck:CardDeck = new CardDeck("texasholdem");
var array:Array = deck.shuffleFisherYates();

trace(array);



I am now getting [object Object} 52 times which is good im just stuck in how to pull in the array?

Im pretty sure I to elaborate on trace(); but not 100% on how to do this.
Thanks for the reply by the way.

Cheers
Paul

need*
Was This Post Helpful? 0
  • +
  • -

#5 anonymous26  Icon User is offline

  • D.I.C Lover

Reputation: 1
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

Re: AS3 shuffle problem 1120 undefined property

Posted 23 March 2013 - 05:04 PM

Maybe have a function that presents a card in order when it is called? You will need a static index of some kind in the funcion to keep track of cards as they are created.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1