0

I am trying to create a canvas game using the proccessing.js library.

I use an array to hold all my objects.

class blah{
    ...
    void delete(){
       // this.remove ???
    }
}

blah myArray = [];
myArray.push(new blah());

Is there a way I can create a delete function inside that class so that when the element is deleted it is removed from the array? I have thought of a workaround like passing as an argument the element's position in the array. Isn't there a direct way to delete it without passing any argument?

The workaround I told above:

void delete(int i){
  myArray.splice(i,1);
}
17
  • 1
    I'm confused: JavaScript doesn't currently support classes or static typing... Am I missing something? Commented Sep 28, 2012 at 21:48
  • 1
    Your code snippet is confusing. In JavaScript you don't declare type of variable and you don't type the return value. Are you sure this isn't ActionScript or Java ? Commented Sep 28, 2012 at 21:48
  • 1
    I am using processing.js as I mentioned in the tags and at the begging of my question. This library allows C++-like declarations. processingjs.org/learning Commented Sep 28, 2012 at 21:49
  • 1
    @Xophmeister just another library which ruins the basic idea of JavaScript. Flexibility, create your own 'stuff'... Commented Sep 28, 2012 at 21:51
  • 2
    @benqus Not really. This is a great library, and I am not a big fan of libraries. But as C++ was the first language I have learnt this really helps in reducing the code size. You can create a fully-functional canvas game in 100 lines, which would take like 300 in pure javascript. And the good part is that I don't have to learn any new library because with this one you can still write in pure javascript, but you can still use the advantages of more advanced OOP languages like C++. Commented Sep 28, 2012 at 21:54

1 Answer 1

1

You should be able to find the instance with indexOf:

void delete() {
    int index = myArray.indexOf(this);

    if (index > -1) {
        myArray.splice(index, 1);
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.