davidfig / pixi-cull

a library to visibly cull objects designed to work with pixi.js
MIT License
109 stars 15 forks source link

Cannot find name 'viewport' with ES6 import #6

Closed Mathieu-C closed 4 years ago

Mathieu-C commented 4 years ago

I'm trying to use this library with the (excellent) pixi-viewport, with the following code:

import * as PIXI from "pixi.js";
import { Viewport as PixiViewport } from "pixi-viewport";
import Cull from "pixi-cull";

I get this error:

TypeScript error in /work/pixi-project/src/game/Game.ts(75,18):
Cannot find name 'viewport'.  TS2304

    73 | 
    74 |     const cull = new Cull.Simple();
  > 75 |     cull.addList(viewport.children);
       |                  ^
    76 |     cull.cull(viewport.getVisibleBounds());
    77 | 
    78 |     window.maps = [];

I believe it's because of my use of import instead of require, the documentation doesn't provide a workaround for import, is it possible to use it currently?

davidfig commented 4 years ago

Where do you declare viewport? You need something like:

const viewport = new PixiViewport()
Mathieu-C commented 4 years ago

Thank you for your answer, I was indeed not declaring things properly. However, I have an issue with sprites added to the viewport from another module (I have a global viewport defined in window.viewport, and sprites gets added to it through a dedicated class). I get a TypeError: box is undefined error on scroll:

  108 | for (let object of list)
  109 | {
  110 |     const box = object[this.AABB]
> 111 |     object[this.visible] =
      | ^  112 |         box.x + box.width > bounds.x && box.x < bounds.x + bounds.width &&
  113 |         box.y + box.height > bounds.y && box.y < bounds.y + bounds.height
  114 | }

Things do work flawlessly when I add sprites from the same context as the culling, I noticed that sprites added from another module don't have the AABB attribute.

Let me know if I'm not being clear enough, or if you require more details regarding that issue.

davidfig commented 4 years ago

How are you adding your objects to the cull list? when it first adds it, it should create object[this.AABB] (which I think defaults to object.aabb). by default this is done on initialization of the objects within the cull system to avoid having to calculate bounds on each frame.

Mathieu-C commented 4 years ago

Indeed, you helped me understand that my Viewport initialization was happening before the sprites were added to it, so cull's addList method was being called with an incomplete list.

The issue is now fully resolved, thanks a lot for your work and support 👍