My concept is rather simple. Animate an image one way, then make it come back.
var xPos = 10;
function main(){
window.requestAnimationFrame(main);
var canvas = document.getElementById("myCanvas");
var c = canvas.getContext("2d");
//Initialize images
var img = new Image();
img.src = "goomba.png";
c.clearRect(0,0,500,500);
c.drawImage(img,xPos,10);
//increment coordinates
if(xPos <= 250){
xPos+=2;
}
else if(xPos >= 250){
xPos-=2;
}
}
window.requestAnimationFrame(main);
I already know what the problem is with my code. When the xPos goes over 250, then the second if statement becomes true. However, once it goes below 250, the first if becomes true again. So I know what the problem is, I just dont know how to fix it. Any help is appreciated!