2015-12-20 166 views
0

我编写了一个简单的游戏,其中我使用了引导按钮来启动新游戏或做另一个游戏。需要在按钮上点击两次

第一次点击(开始新游戏)效果很好。然后,当我想要做另一个游戏时(通过停止并重新初始化当前游戏),它也可以工作。

我的问题是位于重新初始化这后一场比赛(所以,第二次重新启动):我必须单击引导按钮两次。

这里是JavaScript代码段(引导按钮是buttonNewGame):

var buttonNewGame = document.getElementById('buttonNewGame'); 

function startGame() { 
    // If currentGame then call initGame 
    if (isCurrentGame) { 
    isCurrentGame = false; 
    initGame(); 
    console.log('HERE : stop and reinitialize current game'); 
    } 
    else { 
    isCurrentGame = true; 
    } 

    // Call main function 
    currentGame(); 
} 

对于那些我需要点击两次的情况,console.log('HERE : stop and reinitialize current game');显示什么的第一次点击,那么它在调用startGame()第二次点击。

这是与引导按钮的焦点问题还是问题有关?

感谢

+0

所以只检查比isCurrentGame已经价值的预期。我想你在需要时不会重新初始化它 –

回答

0

如果按照执行的顺序,你会发现,你第一次进入startGame()价值为isCurrentGamefalse这就是为什么它不打印console.log

第一次进入startGame()时,isCurrentGame的值更改为true,这就是为什么当您第二次进入时。它进入if条件。

为了解决这个问题,你就需要重置的isCurrentGame为true值调用按钮时

首播时间:

isCurrentGame值是false,所以它会在else和将被分配为真。

if (isCurrentGame) { 
    isCurrentGame = false; 
    initGame(); 
    console.log('HERE : stop and reinitialize current game'); 
} else 
    isCurrentGame = true; 

它会在第二次,isCurrentGametrue,所以它会走,如果条件中并调用initGame()并将打印console.log

相关问题