Initially, the background of the circle should be white. When clicking on the circle, execute the change function in a loop, and when you click again, leave the background of the white circle stopping the change function. How to do this?
// The circle moves with the keys a, w, s, d, but I can not execute the above events.
follows the complete code:
//----VARIÁVEIS----//
var dx;
var dy;
var px;
var py;
var vel;
var anima;
var ang;
var jog;
var skin;
var rotacao;
var tecla;
var tmp;
var tmpCor;
//-----------------//
function inicia(){
dx = 0;
dy = 0;
px = 0;
py = 0;
vel = 5;
ang = 0;
rotacao = false;
estadoCor = false;
jog = document.getElementById("jog");
skin = document.getElementById("skin");
skin.addEventListener("click",giraPara);
skin.addEventListener("click",mudaCor);
document.addEventListener("keydown",teclaDw);
document.addEventListener("keyup",teclaUp);
tmp = setInterval(enterFrame,5);
tmpCor = setInterval(mudaCor,1000);
}
//---FUNÇÕES DE ROTAÇÃO DO JOG---//
function gira(){
if(ang>360){
ang = 0;
}
ang+=8;
jog.style.transform="rotate("+ang+"deg)";
anima = requestAnimationFrame(gira);
}
function giraPara(){
if(!rotacao){
rotacao = true;
anima = requestAnimationFrame(gira);
}else if(rotacao){
rotacao = false;
cancelAnimationFrame(anima);
}
}
//-------------------------------//
//---FUNÇÕES DE MOVIMENTO DO JOG---//
//Teclas --> a=65, w=87, d=68, s=83
function teclaDw(){
tecla = event.keyCode;
if(tecla==65){
dx = -1;
}else if(tecla==87){
dy = -1;
}else if(tecla==68){
dx = 1;
}else if(tecla==83){
dy = 1;
}
}
function teclaUp(){
tecla = event.keyCode;
if(tecla==65){
dx = 0;
}else if(tecla==87){
dy = 0;
}else if(tecla==68){
dx = 0;
}else if(tecla==83){
dy = 0;
}
}
function enterFrame(){
px+= (dx*vel);
py+= (dy*vel);
jog.style.left = px + "px";
jog.style.top = py + "px";
}
//----------------------------------//
//---FUNÇÃO DE CORES---//
function mudaCor(){
var r = Math.floor(Math.random()*255);
var g = Math.floor(Math.random()*255);
var b = Math.floor(Math.random()*255);
skin.style.backgroundColor= "rgb("+r+","+g+","+b+")";
}
window.addEventListener("load",inicia);
#jog{
width: 100px;
height: 100px;
border: 1px solid #000;
position: absolute;
top: 0px;
left: 0px;
background-color: #f00;
display: flex;
}
#skin{
width: 100px;
height: 100px;
border: 2px solid #000;
background-color: #fff;
border-radius: 50%;
margin-left: -1px;
margin-top: -2px;
align-self: auto;
cursor: pointer;
}
<div id="jog">
<div id="skin"></div>
</div>