Tengo una función que verifica si la bala golpea a un jugador, y si es así, debería eliminar la bala y el jugador que fue golpeado.
Tengo check_if_bullet_hit_player(bulletRef) que obtiene una referencia de viñeta, y la identificación del jugador recibió un disparo.
También tengo una función shoot() que llama a check_if_bullet_hit_player(bulletRef) para cada movimiento de bala.
Actualmente, cuando una bala golpea a un jugador, la bala desaparece, pero no puedo hacer que el jugador que recibió el golpe también desaparezca.
Probé la siguiente función shoot :
function shoot(speed=0.5, distance=5, targetX, targetY){ var shoot = setInterval(function() { check_if_bullet_hit_player(bulletRef).then(player_got_shot_id => { if (player_got_shot_id != ""){ clearInterval(shoot); bulletRef.remove(); hitPlayerRef = firebase.database().ref(`players/${player_got_shot_id}`); hitPlayerRef.once("value").then(function(){ alert("hi"); hitPlayerRef.remove(); }) } }) }) } hi una alerta cuando un jugador recibe un golpe, y el jugador que recibe el golpe desaparece por un momento y luego se vuelve a crear (que no es lo que se busca). Creo que el reproductor se vuelve a crear debido a la sincronización, así que probé diferentes métodos.
Intenté como la siguiente respuesta de desbordamiento de pila , que espera a que regrese la referencia:
function shoot(speed=0.5, distance=5, targetX, targetY){ var shoot = setInterval(function() { check_if_bullet_hit_player(bulletRef).then(player_got_shot_id => { if (player_got_shot_id != ""){ clearInterval(shoot); bulletRef.remove(); await firebase.database().ref(`players/${player_got_shot_id}`).remove(); } }) }) }y obtengo:
Uncaught SyntaxError: await is only valid in async functions and the top level bodies of modules
Traté de esperar con then :
function shoot(speed=0.5, distance=5, targetX, targetY){ var shoot = setInterval(function() { check_if_bullet_hit_player(bulletRef).then(player_got_shot_id => { if (player_got_shot_id != ""){ clearInterval(shoot); bulletRef.remove(); hitPlayerRef = firebase.database().ref(`players/${player_got_shot_id}`); return hitPlayerRef.get(); // HERE we chain the promise } }).then(hola => { hola.remove(); }) }) }Pero nada pasa.
¿Cómo puedo hacer que el jugador golpeado desaparezca?