Question

This might be a naive question, but is there a destructor in Processing.js? I know that regular Processing is Java-based so there isn't a destructor, but I'm not sure if Processing.js runs the same way.

Just so it's here, here's the class I want to build a destructor for, if needed:

// Obstacle Class
class Obstacle {
    float r,g,b;
    float x, y, w, h;
    float speed;

    Obstacle(float x_pos, float y_pos, float width, float height, float sp) {
        // Initialize Color
        r = random(255);
        g = random(255);
        b = random(255);

        // Initial Size
        w = width;
        h = height;

        // Initial Position
        x = x_pos;
        y = y_pos;

        // Initialize Speed
        speed = sp;
    }

    void update() {
        y += speed;
    }

    void draw() {
        fill(r,g,b);
        rect(x,y,w,h);
    }
}
Was it helpful?

Solution

Processing.js doesn't control memory allocation or cleanup, it's all left to the JavaScript engine. Once you remove all references to an object, the JS engine will queue the object for garbage collection, and will actually free the memory whenever it needs to, afterwards.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top