From 71b4cc532f28b36278320df7a51a796481b5cfa5 Mon Sep 17 00:00:00 2001 From: Richard Davey Date: Sun, 1 Sep 2013 11:15:13 +0100 Subject: [PATCH] Added Sprite.getBounds function --- examples/get_bounds.php | 51 +++++++++++++++++++++++++++++++++++++++ src/gameobjects/Sprite.js | 20 +++++++++++++++ 2 files changed, 71 insertions(+) create mode 100644 examples/get_bounds.php diff --git a/examples/get_bounds.php b/examples/get_bounds.php new file mode 100644 index 00000000..14ea1011 --- /dev/null +++ b/examples/get_bounds.php @@ -0,0 +1,51 @@ + + + + phaser.js - a new beginning + + + + + + + + \ No newline at end of file diff --git a/src/gameobjects/Sprite.js b/src/gameobjects/Sprite.js index a6d057e5..ffa4458e 100644 --- a/src/gameobjects/Sprite.js +++ b/src/gameobjects/Sprite.js @@ -219,6 +219,26 @@ Phaser.Sprite.prototype.getLocalPosition = function(p, x, y) { } +Phaser.Sprite.prototype.getBounds = function(rect) { + + rect = rect || new Phaser.Rectangle; + + var left = Math.min(this.topLeft.x, this.topRight.x, this.bottomLeft.x, this.bottomRight.x); + var right = Math.max(this.topLeft.x, this.topRight.x, this.bottomLeft.x, this.bottomRight.x); + var top = Math.min(this.topLeft.y, this.topRight.y, this.bottomLeft.y, this.bottomRight.y); + var bottom = Math.max(this.topLeft.y, this.topRight.y, this.bottomLeft.y, this.bottomRight.y); + + rect.x = left; + rect.y = top; + rect.width = right - left; + rect.height = bottom - top; + + // This could work to include the children by running through the linked list and storing only the highest min.max values + + return rect; + +} + Object.defineProperty(Phaser.Sprite.prototype, 'angle', { get: function() {