2016-12-04 71 views
0

我正在尋找一些幫助,以在Tumult Hype中使用JavaScript爲Breakout遊戲添加一些代碼。我期待這樣做,所以一旦你達到一定的分數,球速會提高。JavaScript的突圍遊戲。在得分區間更改球速度

這是迄今沒有速度助推器的代碼。

var input1 = event.which || event.keyCode; 

if ((input1 == "37") && (window.setLoopLeft == false)) { // LEFT ARROW PRESSED 
    window.setLoopLeft = true; 
    window.intervalLeft = setInterval(moveLeft, 5); 
} else if ((input1 == "39") && (window.setLoopRight == false)) { // RIGHT ARROW PRESSED 
    window.setLoopRight = true; 
    window.intervalRight = setInterval(moveRight, 5); 
} else if ((input1 == "32") && (window.ballLaunched == false)) { // SPACE BAR PRESSED 
    window.ballLaunched = true; 
    // RUN THE MOVEBALL FUNCTION EVERY 10 MILLISECONDS 
    window.intervalMoveBall = setInterval(moveBall, window.ballSpeed); 
} 

function moveBall() { 
    var ballLeft = parseInt(hypeDocument.getElementById("ball").style.left); 
    var ballTop = parseInt(hypeDocument.getElementById("ball").style.top); 

這是我加入的代碼。現在我正在計劃的是創建一個全局變量來應用於window.intervalMoveBall。然後我會寫會在1000點檢測分值新功能和雙重球的速度使得它每5毫秒移動,而不是10

現在是什麼我不知道該怎麼辦實際上是寫if語句以便它檢測得分值。我想知道是否有人能夠告訴我如何正確,甚至能夠告訴我,如果使用全局函數和新函數並且if語句甚至可以爲此工作。

+0

你目前如何移動球體,你如何保持得分? –

+0

當頁面初始化樂譜時,會出現單獨的功能。該代碼是 hypeDocument.getElementById(「scoreValue2」)。innerHTML = window.score; 球根據擊中每個塊或槳板的位置以設定的角度移動。 – Zephyranthes

回答

0

您正在用setInterval的,所以改變你將不得不清除原來間隔的時間間隔,並開始新的一個新的時間間隔。更簡單的方法是使moveBall函數負責調用本身具有的setTimeout(和同爲moveLeft和MoveRight的),像這樣...

var input1 = event.which || event.keyCode; 

if ((input1 == "37") && (window.setLoopLeft == false)) { // LEFT ARROW PRESSED 
    window.setLoopLeft = true; 
    window.intervalLeft = setInterval(moveLeft, 5); 
} else if ((input1 == "39") && (window.setLoopRight == false)) { // RIGHT ARROW PRESSED 
    window.setLoopRight = true; 
    window.intervalRight = setInterval(moveRight, 5); 
} else if ((input1 == "32") && (window.ballLaunched == false)) { // SPACE BAR PRESSED 
    window.ballLaunched = true; 
    moveBall(); 
} 

function moveBall() { 
    setTimeout(moveBall, window.ballSpeed); 
    // the rest of your moveBall function 
} 

這意味着我們可以每次設置不同的時間跨度moveBall運行,但一些有條件的邏輯,例如,

function moveBall() { 
    setTimeout(moveBall, window.score > 1000 : 5 ? 10); 
    // the rest of your moveBall function 
} 

顯然,這是一個無限循環,所以你也想停止這一點,某種程度上增加,如支票,比賽尚未結束,例如,

function moveBall() { 
    if (window.gameFinished) { 
     return; 
    } 

    setTimeout(moveBall, window.score > 1000 : 5 ? 10); 
    // the rest of your moveBall function 
} 

正如順便說一句,它會變得非常難以維護使用大量存儲在window對象的全局變量,所以它可能是值得探討的JavaScript命名空間。