Basically i have managed to randomized 2 variable's value with this codes:
function getRandomPosition()
{
var xx = document.body.clientWidth;
var yy = document.body.clientHeight;
var randomY = Math.floor(Math.random()*yy);
var randomX = Math.floor(Math.random()*xx);
}
This is working fine. Next i made a image random using this randomized coordinates.
function randomly()
{
var yx = getRandomPosition(img);
img.style.top = yx[0];
img.style.left = yx[1];
return[yx];
}
Next i want to constantly monitor the position of the mouse and the position of the image.
function plswork()
{
var yxx=getRandomPosition();
if((event.clientX < yxx[1]-210)||event.clientX>yxx[1]+340) //only comparing the x axis~~ etcetc
{ //play an audio track
}
}
document.onmousemove= plswork;//when mouse move, run that function.
HOWEVER, the problem i have now is that the image is spawning everywhere once i move the mouse as well. And if i call the getRandomPosition(); function it will run itself again and give me a different set of numbers to check with the mouse. Can anyone give me suggestions? or help thanks
My target is to check the mouse position and compare it to the randomized location that the image is spawned in. The 'yx' in function randomly() tells me the coordinates of the location the image is spawned in. So to bring this function over i have to return it, but when i do that in function plswork(), whenever i move my mouse, it will call and execute the entire function spawning images randomly... BUT, if i call the function getRandomPosition() to replace the var yx in function plswork(), im comparing a different set of coordinates from the 'yx' in the function randomly()..