manage the frameRate for an increasing variable

Hi. I've been creating a game, and i added an animation based in images from an array. I created a variable for a "indexCounter" so the animation can happen. But the variable grows too fast for the animation. How can I manage it ? I tried with frameRate, but it decrease the fps of the entire content of the canvas. I have something like this.

let counter, movi, start, izq;
let sprite = [];
function preload(){
        for(var i = 0; i < 10; i++){
            sprite[i]= loadImage(`img${i}.png`);
        }
}
function setup(){
    createCanvas(600, 500);
    counter = 0;
    movi= 500;
    start = false;
    izq = 0;
}


function draw(){
    background(50);
    if(start){
        counter = (counter + 1) % sprite.length;
    }

    image(sprite[counter], movi, 100, 60, 60);
    movi+=izq * -5;

}

function keyPressed(){
    start = true;
    if(keyCode === 37){
        izq = 1
    }else if (keyCode === 39){
        izq = -1;
    }
}

function keyReleased(){
    izq = 0;
    start = false;
}
Tagged:

Answers

Sign In or Register to comment.