Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

222
Vistas
How to remove/delete a game object

How do you make an object that can kill/remove itself? e.g.

var bullet = [];
function bullet() {
  this.removeSelf = () => {
    this = false;
  }
}
function setup() {
  bullet[0] = new Bullet();
  bullet[0].removeSelf();
}
setup(); 
about 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

Not sure if I understood your question correctly but if you mean to destroy the object and free the memory then you first need to remove all references to it and then garbage collector will automatically do the job for you.

You're storing a reference to the object in the array so you can do this: bullet[0] = null

about 4 years ago · Santiago Trujillo Denunciar

0

If the goal is to remove it from the parent array, you need to edit that array. So we can pass the parent into the instance and have the removeSelf look for its instance in the parent and splice it out.

I also renamed the constructor to Bullet.

var bullets = [];

function Bullet(parent) {
  this.removeSelf = () => {
    const index = parent.indexOf(this);
    parent.splice(index, 1);
  }
}

function setup() {
  bullets[0] = new Bullet(bullets);
  bullets[1] = new Bullet(bullets); // Add a second isntance to the array
  bullets[0].removeSelf();
}

setup(); 
console.log(bullets.length) // Should be 1

about 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda