javascriptphaser-frameworkphaserjs

How do I find the x coordinate and y coordinate of a group in Phaser 3?


I have created a dynamic group in phaser 3 using let group = this.physics.add.group(). However, I have tried group.x,group.x to try and get the x and y coordinate of the group, but the result seems to be undefined.

I want to find a way to find the x coordinate and the y coordinate of the group, but I haven't found anything. The way I have created the group is let group = this.physics.add.group().


Solution

  • Well the answer is, it is not possible! The documenation states.

    "...Groups themselves aren't displayable, and can't be positioned, rotated, scaled, or hidden...."

    Groups are only a collection of objects, that are not bound together. If you want to position multiple objects you could use a container, it can be positioned (link to the documentation). But might be tricky with physics.

    Depending on you usecase it might be better to stick with a group and create your own helper method, where you iterate over the group children something like this:

     let group = this.physics.add.group().
     ...
     group.getChildren().forEach((child) => {
         // do something with the position
         // child.x += 10 ;
         // child.y += 100;
     });
    

    For getting the position of the group, you would have to decide, how you would want to calculate it. should it be the center of all children, top left corner of a bounding box, ...

    It all depends on the usecase