Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Putting a function in a loop

function collision(){
        if(player.y + player.height + player.velocityY >= coin.y &&
        player.x + player.width >= coin.x &&
        player.x <= coin.x + coin.width){
            score = +1
        }
}

Right now my collision works but only once, ei if my player collides with two points it only adds one which makes sense because if statement only get read once. How can I put this function in a loop? I tried with while loop but it gave me call stack exceeded.

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

You need a game loop. In javascript, the simple way is using setInterval

The setInterval() method calls a function at specified intervals (in milliseconds).

const FPS = 30; // frames per second
setInterval(collision, 1000 / FPS);

Example game

setInterval()

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading