2016-12-04 70 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命名空间。