0

I have a class in my project called Floor. Theoretically, the class is defined, and I can successfully create an instance of it. However, 48 lines later, both class and instance are undefined. There are no delete statements or the like in those lines. I can reproduce this problem in Firefox, Chromium (Brave), and WebKit (Epiphany).

Code (abridged):

class Floor extends Entity {
    constructor(position = 700){
        super('floor');
        const transform = new Transform2D(0, position);
        this.attach(transform);
        this.attach(new class extends Component {
            canUse(system) {
                return system instanceof RenderSystem2D;
            }
            update(system) {
                system.ctx.beginPath();
                system.ctx.moveTo(transform.x, transform.actY);
                system.ctx.lineTo(640, transform.actY);
                system.ctx.strokeStyle = "red";
                system.ctx.stroke();
                console.log(system.game);
                for (let entity of system.game?.getWhere(e => e.hasComponent(Transform2D)            
                                                           && !e.hasComponent(Camera2D)
                                                           && e.getComponent(Transform2D).y > transform.y
                ) ?? []) {
                    system.game?.remove(entity);
                }
            }
            init(_system) {}
        });
    }
}
const fl = new Floor; // `fl` has been successfully constructed, and is a `Floor`.
// ...
// Both `fl` and `Floor` get deleted somewhere in the following two lines:
const tilemap = await platinum.image.load('tilemap.png');
const tileBitmap = await createImageBitmap(tilemap, 0, 0, 32, 32);
// ...
scene.add(fl); // both `fl` and `Floor` are undefined here; why?

One thing that may be of interest is that all of the lines in the definition of Floor are greyed out in the debugger; does that mean that they've been optimized out by the VM? If so, why?

Thank you.

platinum is https://github.com/aleksrutins/platinum.

nonagone
  • 1
  • 2
  • 1
    `new Floor()`, you didn't call your constructor. – Peterrabbit Apr 06 '22 at 15:54
  • 1
    @Peterrabbit: that is actually valid JS syntax; see https://stackoverflow.com/questions/3034941/can-we-omit-parentheses-when-creating-an-object-using-the-new-operator#3034952. I should clarify that it is actually successfully constructed, sorry. – nonagone Apr 06 '22 at 16:10
  • We learn every day ^^ ! Thx. – Peterrabbit Apr 06 '22 at 16:25
  • How the 2 instructions you named and the `f1` are related ? I think you could add a little more context about how the objet is used. When you say Floor is undefined, you mean your constructor doesn't exist anymore ? – Peterrabbit Apr 06 '22 at 16:31
  • @Peterrabbit: that's the thing, they have nothing to do with each other. The `Floor` class is an object to render a red line at the bottom of the play area (this is a game) that destroys anything below it. It doesn't even use any images. And yet somehow neither the `Floor` constructor nor the `fl` instance exist after those two lines of code. That's why I'm wondering if they were optimized out, and if so, why, and how to prevent it. – nonagone Apr 06 '22 at 20:24

0 Answers0