dimanche 5 juillet 2020

How to randomize the position of an element when clicked

I'm making a basic game. I need this circle, when clicked, to move another random position on the screen.

The rest of the game : https://codepen.io/jtog95/pen/ExPbzvo

var circle = document.getElementById('circle');
var spaceWidth;
var spaceHeight;

initCircle();

function initCircle() {
  spaceWidth = screen.height - circle.height;
  spaceHeight = screen.width - circle.width;

  circle.addEventListener('click', moveCircle)
}

function moveCircle(){
  circle.style.top = Math.round(Math.random() * spaceWidth) + 'px';

  circle.style.left = Math.round(Math.random() * spaceHeight) + 'px';
}




Aucun commentaire:

Enregistrer un commentaire