Jump to content

Graphics.getBounds() returns rectangle with x=0, y=0, width=0, height=0


spinnerbox
 Share

Recommended Posts

Damn I have buggy code :D

Trying to check if mouse points x/y position is in bounds of a given graphics object called leftShelveGraphics.

I do this:

gameObject.physics.enable(leftWallGraphics, phaser.Physics.ARCADE);

update: function () {
    console.log(leftWallGraphics.getBounds().x + ", " +
            leftWallGraphics.getBounds().y + ", " +
            leftWallGraphics.getBounds().width + ", " +
            leftWallGraphics.getBounds().height + ", " +
            horzBaffleObj.x + ", " +
            horzBaffleObj.y);
    if (phaser.Rectangle.contains(leftWallGraphics.getBounds(), horzBaffleObj.x, horzBaffleObj.y))  {
      console.log("I am over leftWallGraphics");
    }
}

But the above console.log() prints 0,0,0,0 for x/y/width/height. horzBaffle.x/y are actual values of the object I move on update.

Am i missing to set up something?

leftShelveGraphics contains other graphical shapes like polygons, rectangles, and more complex graphics like shelves.

EDIT: leftShelveGraphics is child of larger container graphics object called constrGrObject which is child of gameObject.world. Could this be causing some issues?

Link to comment
Share on other sites

Well I guess so. I had to access sub objects to test if bounds contain certain point:

update: function () {
       if (utils.isTruthy(horzBaffleObj)) {
             if (gameObject.input.mousePointer.isDown) {

                  horzBaffleObj.x = gameObject.input.mousePointer.x;
                  horzBaffleObj.y = gameObject.input.mousePointer.y;

                  if (leftShelveGraphics.getChildAt(0).getBounds().contains(horzBaffleObj.x, horzBaffleObj.y) ||
                      leftShelveGraphics.getChildAt(1).getBounds().contains(horzBaffleObj.x, horzBaffleObj.y))  {

                      console.log("I am over leftShelveGraphics");
                  }

                  if (rightShelveGraphics.getChildAt(0).getBounds().contains(horzBaffleObj.x, horzBaffleObj.y) ||
                      rightShelveGraphics.getChildAt(1).getBounds().contains(horzBaffleObj.x, horzBaffleObj.y))  {

                      console.log("I am over rightShelveGraphics");
                  }

             } else {
                  horzBaffleObj.destroy();
             }
       }
 },

 

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...