2017-03-31 58 views
0

我正在研究基於HTML的文本冒險遊戲,其中一個選擇導致下一個,基於用戶點擊的鏈接。我使用localStorage來存儲玩家的HP值,當HP值達到0時,遊戲應該結束,並且網站應該改變爲「遊戲結束」頁面。我有兩個功能,這兩個導航到不同的頁面,這取決於如果HP高於或低於一定數量JavaScript window.location.assign()不工作

function alterHP(value) { 
    if (typeof (Storage) !== "undefined") { 
     var currentHP = parseInt(localStorage.HP); 
     localStorage.HP = currentHP + value; 
     hp=parseInt(localStorage.HP); 
     if (hp<=0) { 
      alert(hp); //For debugging, is showing a negative number, like it should be 
      /*If the error is syntactical, it is the next line*/  
      window.location.replace('youLose.html'); 
      /*I used breakpoints and this line IS getting hit*/ 
      /*It's just not going to the 'youLose' page*/ 
     } 
    } 
} 

function getResult() { 
    var currentHP = parseInt(localStorage.HP); 
    if (currentHP > 4) { 
     window.location.assign('endAbove4.html');//These both work fine 
    } else { 
     window.location.assign('endBelow4.html');//These both work fine 
    } 
} 

<li><a 
    href="../S9/S9.html" onclick="alterHP(-6);" 
    class="ghost-button">Go on to the next scene</a></li> 

我已經嘗試了youLose.html路徑名的每一個變化,我可以想到,包括../youLose.html,../../youLose.html,.. \ youLose.html,甚至把完整的http://gw.mvctc.com/Class2018/smcintosh/Projects/StMarys(我物理打開該頁面並直接從URL欄複製到我的代碼,所以我知道這是正確的地址。)我也嘗試使用window.location.href =「../ youLose.html」「youLose.html」'../youLose.html'和'youLose.html' window.location.replace()

最令人沮喪的部分是,如果你到了getResult();被調用,你有-26hp,它行爲正確,並重定向到endBelow4.html頁面。我只是需要它重新定向到惠普網頁儘快惠普0

我不能爲我的生活弄清楚爲什麼它不會重定向到youLose頁面。

回答

0

window.close('game'); 
window.open('../youLose.html'); 

就地的window.location.assign的();那麼在您的index.html頁地方

<script>window.name("game");</script> 

,這將關閉的選項卡的遊戲,然後在新標籤替換原來的標籤頁中打開youLose.html。

2

使用window.open( 'youLose.html', '_自')

+0

我無法得到這個工作。 – Lordbug

+0

會檢查並讓你知道 – Ahefaz

+0

我實際上做了一些與window.open()搞亂,並得到它的工作。謝謝。 – Lordbug