Question
Adding parameters to functions within addEventListener
I am trying to get a funtion to run from within an addEventListener to animate a circle moving across the screen. The speed of the circle would need to be passed as parameters inside the function. No matter how I change this code, I am unable to get this to work.
//Setting up inital speed of the molecules
var speedX = 8;
var speedY = 10;
//Setting up initial position of the molecules within a certain bound
this.mol.x = Math.random()*(500-250+1)+250;
this.mol.y = Math.random()*(350-50+1)+50;
//animate
this.addEventListener("tick",function.bind(this){moveMol(this.speedX,this.speedY);})
//definition of function that animates
function moveMol(spX,spY)
{
this.mol.x+=spX;
this.mol.y+=spY;
if(this.mol.x>500 || this.mol.x<250)
{
spX*=-1
}
if(this.mol.y>350 || this.mol.y<50)
{
spY*=-1
}
}
