Hello!
Thanks for reading.
I have been having quite a difficult time making use of group objects… I have a group of walls, and my first wall is created at a different Y coordinate than the others! I am using the latest stable version, but this issue was not present on v3.18.1
Here’s a screnshot:
I have some code that looks like this following:
export default class Walls extends Phaser.Physics.Arcade.Group {
constructor(scene) {
super(scene.physics.world, scene);
this.key = 'walls'
this.maxSize = 3; // FIXME NOTE that the config option is notttt accepting this
this.scene = scene;
this.destroyed = 0;
}
add(child, addToScene) {
if (!this.isFull() && child instanceof Wall) {
let retVal = super.add(child, false);
child.spawn();
return retVal;
}
return super.add(child, false);
}
update() {
// Destroy old wall
let firstWall = this.getFirstAlive();
if (firstWall != null && firstWall.x - firstWall.width < 0) {
this.killAndHide(firstWall);
this.remove(firstWall);
firstWall.destroy(); // TODO I wonder if this is necessary
this.destroyed++;
this.scene.events.emit("score++");
}
// create new wall // TODO add some variation here?
let lastWall = this.getLast(true);
if (lastWall == null || lastWall.x + lastWall.width < this.scene.config.width/2) {
this.add(new Wall(this.scene, this.scene.config.width+45, 450, IMAGES.wall));
}
}
And the following child class:
export default class Wall extends Phaser.Physics.Arcade.Image {
constructor(scene, x, y, texture, frame) {
super(scene, x, y, texture, frame);
this.scene = scene;
}
spawn() {
this.scene.add.existing(this);
this.scene.physics.add.existing(this);
this.setDragX(0);
this.setImmovable(true);
this.setVelocityX(3 * 58.66 * -1); // FIXME static constant
return this;
}
}
Note that I am trying to make use of the maxSize parameter, which for some reason refuses to be made use of in the config… The maxSize parameter is NOT ignored if I extend GameObject.Sprite directly, however it seems that other physics related properties become an issue. Here’s an example of what my config looked like:
super(scene.physics.world, scene, [], {
key: 'walls',
maxSize: 3
});
And finally here’s how im adding it to my scene:
// Walls
this.walls = new Walls(this);
this.add.existing(this.walls); // fixme??
What’s going on here? Something dumb? And besides that, stylistically, am I approaching this problem poorly / correctly and extending the correct object types?
Thanks for looking!
Greg