quinton-ashley / p5play

JavaScript game engine that uses q5.js/p5.js for graphics and Box2D for physics.
https://p5play.org
GNU Affero General Public License v3.0
667 stars 184 forks source link

Group cull method not working as expected #241

Closed calebfoss closed 1 year ago

calebfoss commented 1 year ago

In this example, in frame 1 cull() removes the above, below, and left sprites but not the sprite to the right of the canvas. group.includes(spriteRight) returns true for the first two frames and then false.

View on web editor

let group;
let spriteAbove, spriteBelow, spriteLeft, spriteRight;

function setup() {
  new Canvas(400, 400);
  group = new Group();
  spriteAbove = new Sprite(0, -1, 100);
  spriteBelow = new Sprite(0, height + 1, 100);
  spriteLeft = new Sprite(-1, 0, 100);
  spriteRight = new Sprite(width + 1, 0, 100);

  group.push(spriteAbove);
  group.push(spriteBelow);
  group.push(spriteLeft);
  group.push(spriteRight);
}

function draw() {
  background(220);
  group.cull(0);
  console.log(`FRAME ${frameCount}
Above: ${group.includes(spriteAbove)}
Below: ${group.includes(spriteAbove)}
Left: ${group.includes(spriteLeft)}
Right: ${group.includes(spriteRight)}`);
  noLoop();
}
function mousePressed() {
  redraw();
}

What's going on?

quinton-ashley commented 1 year ago

I just learned that for of loops do not automatically compensate for if a sprite is removed! That's why it removed only removed two sprites the first frame. Just fixed this in v3.6.9!