Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

221
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!