Skip to content

Commit f89563c

Browse files
committed
fix for gamespeed issues
1 parent bea21c8 commit f89563c

File tree

2 files changed

+41
-8
lines changed

2 files changed

+41
-8
lines changed

src/main/resources/static/js/game.js

Lines changed: 40 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,15 @@ var elem = document.body;
55
var two = new Two(params).appendTo(elem);
66
var FRAMERATE = 30 / 1000; // This is like 30fps
77

8+
var refreshRate = getRefreshRate();
9+
var frameInterval;
10+
811
var textFontFamily = "'Courier New', monospace";
912

1013
var audioElementMusic = document.createElement('audio');
1114
audioElementMusic.setAttribute('src','sound/music2.mp3');
1215
audioElementMusic.loop = true;
13-
audioElementMusic.playbackRate = 1.11111111; //get to 143.5 bpm
16+
audioElementMusic.playbackRate = .99; //get to 133.8
1417

1518

1619
var testText = "Failure is not the end; it's a detour on the road to success. Everyone stumbles and falls along the way, but what truly matters is how you respond. Instead of letting setbacks discourage you, view them as valuable learning experiences. Analyze what went wrong, extract the lessons, adjust your approach, and keep moving forward with renewed determination. Remember, every successful person has encountered failure at some point. It's through these challenges that we grow stronger, wiser, and more resilient.";
@@ -84,7 +87,9 @@ var hits = 0;
8487

8588
var startTime;
8689

87-
var gameIntervalId = 0;
90+
var stopRepeat;
91+
92+
var animationFrameCounter = 0;
8893

8994

9095
two.bind('update', update);
@@ -141,7 +146,6 @@ function update(frameCount) {
141146
groupText.position.set(cx-moddedGameSpeed,cy);
142147
}else{
143148
audioElementMusic.pause();
144-
clearInterval(gameIntervalId);
145149
document.getElementById("highScoreSubmit").style.display = 'block';
146150
}
147151
}
@@ -177,10 +181,7 @@ function startGame(){
177181
startScreenPopup.style.display = 'none';
178182
audioElementMusic.play();
179183
startTime = new Date().getTime();
180-
gameIntervalId = setInterval(function() {
181-
two.update();
182-
}, FRAMERATE);
183-
184+
gameTimerLoop();
184185
}
185186

186187
function phoneHome(){
@@ -198,6 +199,38 @@ function newGame(){
198199
});
199200
}
200201

202+
function gameTimerLoop() {
203+
if(animationFrameCounter == frameInterval){
204+
two.update();
205+
animationFrameCounter = 0;
206+
}
207+
animationFrameCounter++;
208+
209+
requestAnimationFrame(gameTimerLoop);
210+
}
211+
212+
function getRefreshRate() {
213+
let frameCount = 0;
214+
let startTime = performance.now();
215+
let refreshRate = 0;
216+
217+
function measureFrame() {
218+
frameCount++;
219+
let currentTime = performance.now();
220+
if (currentTime - startTime > 1000) {
221+
refreshRate = frameCount;
222+
console.log("Refresh Rate: " + refreshRate + " Hz");
223+
frameInterval = Math.round(refreshRate/60);//trying to hit 60 frames
224+
document.getElementById("playBtn").removeAttribute("disabled");
225+
return refreshRate;
226+
}
227+
requestAnimationFrame(measureFrame);
228+
}
229+
230+
measureFrame();
231+
}
232+
233+
201234
document.addEventListener('keydown', function(event) {
202235
var key = event.key;
203236
if(key == " "){

src/main/resources/templates/home.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
<!-- Start Screen Popup -->
1414
<div id="startScreenPopup" class="start-menu">
1515
<h1>Welcome to Falling Type!</h1>
16-
<button class="play-btn" onclick="startGame()">Play</button>
16+
<button type="button" id="playBtn" class="play-btn" onclick="startGame()" disabled>Play</button>
1717
<a href="highscores"> Leaderboard </a>
1818
</div>
1919

0 commit comments

Comments
 (0)