Skip to content

Commit 71b4cc5

Browse files
committed
Added Sprite.getBounds function
1 parent d54a923 commit 71b4cc5

2 files changed

Lines changed: 71 additions & 0 deletions

File tree

examples/get_bounds.php

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<!DOCTYPE HTML>
2+
<html>
3+
<head>
4+
<title>phaser.js - a new beginning</title>
5+
<?php
6+
require('js.php');
7+
?>
8+
</head>
9+
<body>
10+
11+
<script type="text/javascript">
12+
13+
(function () {
14+
15+
var game = new Phaser.Game(800, 600, Phaser.CANVAS, '', { preload: preload, create: create, update: update, render: render });
16+
17+
var s;
18+
var r;
19+
20+
function preload() {
21+
game.load.image('test', 'assets/sprites/mana_card.png');
22+
}
23+
24+
function create() {
25+
26+
s = game.add.sprite(game.world.centerX, game.world.centerY, 'test');
27+
s.anchor.setTo(0.5, 0.5);
28+
29+
r = s.getBounds(r);
30+
31+
}
32+
33+
function update() {
34+
35+
s.angle += 1;
36+
s.getBounds(r);
37+
38+
}
39+
40+
function render() {
41+
42+
game.debug.renderRectangle(r);
43+
44+
}
45+
46+
})();
47+
48+
</script>
49+
50+
</body>
51+
</html>

src/gameobjects/Sprite.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,26 @@ Phaser.Sprite.prototype.getLocalPosition = function(p, x, y) {
219219

220220
}
221221

222+
Phaser.Sprite.prototype.getBounds = function(rect) {
223+
224+
rect = rect || new Phaser.Rectangle;
225+
226+
var left = Math.min(this.topLeft.x, this.topRight.x, this.bottomLeft.x, this.bottomRight.x);
227+
var right = Math.max(this.topLeft.x, this.topRight.x, this.bottomLeft.x, this.bottomRight.x);
228+
var top = Math.min(this.topLeft.y, this.topRight.y, this.bottomLeft.y, this.bottomRight.y);
229+
var bottom = Math.max(this.topLeft.y, this.topRight.y, this.bottomLeft.y, this.bottomRight.y);
230+
231+
rect.x = left;
232+
rect.y = top;
233+
rect.width = right - left;
234+
rect.height = bottom - top;
235+
236+
// This could work to include the children by running through the linked list and storing only the highest min.max values
237+
238+
return rect;
239+
240+
}
241+
222242
Object.defineProperty(Phaser.Sprite.prototype, 'angle', {
223243

224244
get: function() {

0 commit comments

Comments
 (0)