Added Sprite.getBounds function

This commit is contained in:
Richard Davey 2013-09-01 11:15:13 +01:00
parent d54a92310d
commit 71b4cc532f
2 changed files with 71 additions and 0 deletions

51
examples/get_bounds.php Normal file
View file

@ -0,0 +1,51 @@
<!DOCTYPE HTML>
<html>
<head>
<title>phaser.js - a new beginning</title>
<?php
require('js.php');
?>
</head>
<body>
<script type="text/javascript">
(function () {
var game = new Phaser.Game(800, 600, Phaser.CANVAS, '', { preload: preload, create: create, update: update, render: render });
var s;
var r;
function preload() {
game.load.image('test', 'assets/sprites/mana_card.png');
}
function create() {
s = game.add.sprite(game.world.centerX, game.world.centerY, 'test');
s.anchor.setTo(0.5, 0.5);
r = s.getBounds(r);
}
function update() {
s.angle += 1;
s.getBounds(r);
}
function render() {
game.debug.renderRectangle(r);
}
})();
</script>
</body>
</html>

View file

@ -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() {