2016-04-24 6 views
0

私はそのようなゲームをデザインしていますが、カウントダウンタイマーはまだ秒です。だから私はそれを数分に変更したい。どのように分単位で変更できますか?説明をお願いします。カウントダウンタイマーを分単位で変更するにはどうすればいいですか?

//thanks to GameAlchemist 
function createCountDown(timeRemaining) { 
    var startTime = Date.now(); 
    return function() { 
    return timeRemaining - (Date.now() - startTime); 
    } 
} 
var currentCountDown = createCountDown(30000); 
// Draw everything 
var render = function() { 
var countDownValue = currentCountDown(); 
returnKillsNeeded(stageNum); 
    ctx.drawImage(startGameImg, 0,0); 
    ctx.font = "24px Helvetica"; 
    ctx.textAlign = 'center' 
    ctx.textBaseline = "top"; 
    ctx.fillStyle="#FF0000"; 
    ctx.fillText("press Enter to play", 250, 450); 
    ctx.fill(); 
    if(gameStart){ 
if (bgReady) { 
ctx.drawImage(bgImage, 0, 0); 
} 
ctx.fillStyle="#522900"; 
ctx.fillRect(0,480,500,120); 
ctx.drawImage(scoreImg, 22,522); 
ctx.drawImage(livesImg, 360,522); 
ctx.drawImage(progressImg, 200,492); 
createProgressBar(); 
createProgressPercent(); 
ctx.fillText("progress", 170,492); 
setEnemyHealthText(); 
drawPlayer(); 
if(countDownValue <=0){ 
    countDownValue = 0; 
}else{ 
    ctx.fillText(countDownValue, 200,190); 
} 
+0

あなたのために、車を改革しないでください。例:[jQuery Countdown](http://keith-wood.name/countdown.html)。 – trincot

+0

提供されたコードに不均衡な中カッコがあります。正しい構文を持つコードを提供してください。論理的な方法でコードをインデントするといいでしょう。 – trincot

+0

あなたは与えられた答えにいくらかフィードバックを与えて、あなたのニーズに合った答えを受け入れることができますか? – trincot

答えて

0

置き換えます

ctx.fillText(countDownValue, 200,190); 

で:

// Convert miliseconds to seconds. 
var seconds = Math.floor(countDownValue/1000); 
// A minute is 60 seconds. See how many times 60 fits in the number of seconds: 
var minutes = Math.floor(seconds/60); 
// Calculate how many seconds are left when removing those minutes: 
seconds -= minutes * 60; 
// Format the seconds to always have 2 digits 
seconds = seconds.toFixed(2); 
// Output the result: 
ctx.fillText(minutes + ':' + seconds, 200, 190); 

いくつかのコメント:

あなたは、その後、車輪の再発明をしていないため、プラグインを使用してOKであれば。例えば、jQuery Countdown

あなたが指定したコードには、中括弧がありません。もちろん、コードを実行する前に修正する必要があります。

0

あなたは分と秒

if(countDownValue <=0){ 
    countDownValue = 0; 
}else{ 
    ctx.fillText(
     (countDownValue/1000/60) << 0 + 
     ':' + 
     (countDownValue/1000) % 60, 200, 190 
    ); 
} 
0

1分にミリス変換右、60秒のですか?

countDownValueを60分に分けて、それを下方に丸めます。 Math.floor()を使用してきれいにしてください。モジュロcountDownValueは60秒で取得します。 MMのような時間を印刷する

その後
var minutes = Math.floor(countDownValue/60); 
var seconds = countDownValue%60; 

:SS

if(seconds > 9) 
    ctx.fillText(minutes + ":" + seconds, 200,190); 
else 
    ctx.fillText(minutes + ":0" + seconds, 200,190); 

この文は、常に7:06、ない7として時間を印刷する場合:プラグインを使用してOKであれば6

関連する問題