I found a solution. The problem will be solved if you insert these two pieces of code.
Puts the scene on pause when the focus is lost.
// Get the TimeManager object through runtimeScene
var timeManager = runtimeScene.getTimeManager(); // It's important to ensure this is the correct time management object
// On window focus:
window.addEventListener('focus', function (event) {
// Restore normal time speed
timeManager.setTimeScale(1); // Restores normal speed
console.log('Time scale set to 1 (focus)'); // Output message for debugging
});
// On window blur:
window.addEventListener('blur', function (event) {
// Stop the game (set time scale to 0)
timeManager.setTimeScale(0); // Freezes the game
console.log('Time scale set to 0 (blur)'); // Output message for debugging
});
Resets all buttons when the game window gains focus.
// Function to clear all properties of an object
function clearObjectProperties(obj) {
for (let prop in obj) {
if (obj.hasOwnProperty(prop)) {
delete obj[prop];
}
}
}
// Function that clears keyboard states
function clearKeyboardStates() {
const inputManager = runtimeScene.getGame().getInputManager();
// Clear _pressedKeys (pressed keys)
if (inputManager._pressedKeys && typeof inputManager._pressedKeys.clear === "function") {
inputManager._pressedKeys.clear();
} else if (inputManager._pressedKeys) {
clearObjectProperties(inputManager._pressedKeys);
}
// Clear _releasedKeys (released keys)
if (inputManager._releasedKeys && typeof inputManager._releasedKeys.clear === "function") {
inputManager._releasedKeys.clear();
} else if (inputManager._releasedKeys) {
clearObjectProperties(inputManager._releasedKeys);
}
console.log("Keyboard states cleared (blur).");
}
// Add a permanent blur event handler
window.addEventListener('blur', function (event) {
clearKeyboardStates();
});