我最近一直在从事一个 ActionScript 3 纸牌游戏项目,我遇到了事件处理程序的问题,我发现这有点复杂。我将尝试解释它:
轮流打牌的玩家数量不定。在他们这样做之后,他们将轮到他们右边的玩家。
function Round(table:Table, lead:Player)/*Table is the list of players.
Lead is the person that starts the round*/
{
private var query:int = 0;
private var roundEnd:Boolean = false;
private var currentplater:Player = lead;
while(roundEnd == false)
{
query = currentPlayer.hasTurn();/*I'll explain this one later. It's
the problematic one*/
//Game stuff happens.
currentPlayer = currentPlayer.nextPlayer;/*Once the turn is over,
it gets assigned to the next player*/
if(currentPlayer.nextPlayer == lead)
{
roundEnd = true;//Everyone has played
}
}
}
正如那里评论的那样,复杂的是hasTurn
. 该函数必须从 Player 类激活并触发玩家处理事件并返回整数值的权限。
public function hasTurn():int
{
for each(var cards:Card in hand)
{
cards.addEventListener(MouseEvent.CLICK, playCard);
}
}
在那之后
public function playCard(theCard:Card)
{
for each(var cards:Card in hand)
{
cards.removeEventListener(MouseEvent.CLICK, playCard);
}
//More game stuff happens. Not currently relevant.
}
我在这里所做的是允许玩家手中的每张牌都被打出,然后在其中一张牌被打出时取消这些权限,但我永远不会返回值Round
,更重要的是,我无法让下一个玩家打出以后玩。我已经在脑海中多次解决这个问题,但似乎无法找到hasTurn
返回值或结束其执行的方法,以便Round
代码可以继续工作。我是否以错误的方式处理这个问题的逻辑?