请你帮助我,我是as3的新手,我正在尝试使用Fisher-Yates算法创建一个洗牌的牌组。当我使用ctrl-enter运行代码时,它编译时没有错误,但是当我尝试使用trace()输出它时;它回来了:
场景1,图层'动作',第1帧,第6行1120:访问未定义属性shuffledArray。
就像我说的我是新来的,这将是我做一些非常愚蠢的事情,但同样我被卡住了。
这是代码
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");
}
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);
我知道这可能是愚蠢的事情,但我正在努力。
提前致谢!
保罗答案 0 :(得分:1)
var deck:CardDeck = new CardDeck("texasholdem");
trace(shuffledArray);
这不起作用,因为那里没有定义shuffledArray
。
尝试:
var deck:CardDeck = new CardDeck("texasholdem");
var array:Array = deck.shuffleFisherYates();
for(var i:int=0; i<array.length; i++)
{
trace(array[i].cardName);
trace(array[i].cardType);
trace(array[i].cardValue);
trace(array[i].isDrawn);
}
答案 1 :(得分:0)
“shuffledArray”是CardDeck对象内的属性。要访问其中的公共方法和属性,您需要使用点语法:
trace(deck.shuffleFisherYates());
但是,根据你正在做的事情,如果你的CardDeck对象是为了控制整个套牌,你可能不需要真正直接访问数组。