i'm coding a game in JS, witch run good, but with time, the FPS slow down. After search here, i think thiks that the problem can be from the load of images, but i don't know how to use the function to run the code after images are loaded. Furthermore my code are in a DOMContentLoaded addevenlistener. the problem can really provide from the load of images ? Thanks!
Here theire a slice of my code
function animate() {
ctx.resetTransform();
ctx.translate(-(player.x - canvas.width / 2), -(player.y - canvas.height / 2)); //for center the character on the map
ctx.drawImage(Images_array[1], 0, 0, canvasSize.width / 1.1, canvasSize.height / 1.1, 0, 0, canvasSize.width, canvasSize.height); //drawing the background
// enterInHouse();
drawSprite(Images_array[0], player.width * player.frameX, player.height * player.frameY, player.width, player.height, player.x, player.y, player.width * scale, player.height * scale) // drawing the sprite
movePlayer()
handlePlayerFrame()
// collisionRedbox()
// displaGameboy()
// if (inHouse === false) {
// createBluebox.enterInCollision(blackBoxs, player)
// }
window.requestAnimationFrame(animate);
}
animate()
window.addEventListener('resize', displayCanvas)
i loaded the images like this.
const playerSprite = new Image();
playerSprite.src = "img/main_chara.png ";
const background = new Image();
background.src = "img/background.png";
const office = new Image();
office.src = "img/interieur_git.png";
const retraining = new Image();
retraining.src = 'img/reconversion.png';
const contact = new Image();
contact.src = 'img/contact_me.png';
const grangeOne= new Image();
grangeOne.src = 'img/grange.png'
const house= new Image();
house.src = 'img/maison.png'
const bigHouse= new Image();
bigHouse.src='img/maison_principale.png'
i can show you more of code if you want. Thanks a lot for helping me
CodePudding user response:
You can use onload
function to understand when image was loaded and then start your game.
const playerSprite = new Image();
playerSprite.src = "img/main_chara.png ";
playerSprite.onload = function(){}
For more information: https://www.techrepublic.com/article/preloading-and-the-javascript-image-object/
CodePudding user response:
You're binding on every drawing of your animation an eventlistner.
window.addEventListener('resize', displayCanvas)
So after 1 second at 60 fps you have 60 eventlisteners, after 2 seconds 120, after a minute 3600 event listeners, after ten minutes 36000
Bind an eventhandler once, bind it outside of your animate() function.