0
Good evening my dear, I am making a 'Function' in javascript but it is not running in my browser Chrome.
function move(){
var obj = document.getElementById("dv1");
var tecla = KeyboardEvent.keyCode;
/*37 -Esquerda - 38-cima - 39-direita 40-baixo*/
if(tecla==37){
px-=10; //salto
obj.style.left= px +"px";
}else if(tecla==38){
py-=10;
obj.style.top= py+"px";
}else if(tecla==39){
px+=10;
obj.style.left=px+"px";
}else if(tecla==40){
py+=10;
obj.style.top=py+"px";
}else if(tecla==13){
alert("este evento foi para o beleleu");
obj.removeEventListener("keydown",move);
}
}
document.addEventListener("keydown",move);
The idea is trivial to "move a square" with "keydown", but when inspecting the Chrome console there is an error in "Event.keycode" that I cannot determine. Thanks for your cooperation.
But you didn’t even use
event.keyCode
in your code, how could I miss that? And what would be theKeyboardEvent
invar tecla = KeyboardEvent.keyCode
?– Woss
Good evening Anderson, the transcript was wrong. But the case was solved.
– Jorge Borges