Skip to content

Commit 9325834

Browse files
authored
Merge pull request phaserjs#4589 from samme/x/arcade-static-body-setSize
Change arguments to Arcade.StaticBody#setSize
2 parents 19c6c94 + 7bba56b commit 9325834

1 file changed

Lines changed: 17 additions & 7 deletions

File tree

src/physics/arcade/StaticBody.js

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -520,15 +520,13 @@ var StaticBody = new Class({
520520
*
521521
* @param {integer} [width] - The width of the Body in pixels. Cannot be zero. If not given, and the parent Game Object has a frame, it will use the frame width.
522522
* @param {integer} [height] - The height of the Body in pixels. Cannot be zero. If not given, and the parent Game Object has a frame, it will use the frame height.
523-
* @param {number} [offsetX] - The horizontal offset of the Body from the Game Object's center.
524-
* @param {number} [offsetY] - The vertical offset of the Body from the Game Object's center.
523+
* @param {boolean} [center=true] - Modify the Body's `offset`, placing the Body's center on its Game Object's center. Only works if the Game Object has the `getCenter` method.
525524
*
526525
* @return {Phaser.Physics.Arcade.StaticBody} This Static Body object.
527526
*/
528-
setSize: function (width, height, offsetX, offsetY)
527+
setSize: function (width, height, center)
529528
{
530-
if (offsetX === undefined) { offsetX = this.offset.x; }
531-
if (offsetY === undefined) { offsetY = this.offset.y; }
529+
if (center === undefined) { center = true; }
532530

533531
var gameObject = this.gameObject;
534532

@@ -550,7 +548,19 @@ var StaticBody = new Class({
550548
this.halfWidth = Math.floor(width / 2);
551549
this.halfHeight = Math.floor(height / 2);
552550

553-
this.offset.set(offsetX, offsetY);
551+
if (center && gameObject.getCenter)
552+
{
553+
var ox = gameObject.displayWidth / 2;
554+
var oy = gameObject.displayHeight / 2;
555+
556+
this.position.x -= this.offset.x;
557+
this.position.y -= this.offset.y;
558+
559+
this.offset.set(ox - this.halfWidth, oy - this.halfHeight);
560+
561+
this.position.x += this.offset.x;
562+
this.position.y += this.offset.y;
563+
}
554564

555565
this.updateCenter();
556566

@@ -638,7 +648,7 @@ var StaticBody = new Class({
638648
this.world.staticTree.remove(this);
639649

640650
gameObject.setPosition(x, y);
641-
651+
642652
gameObject.getTopLeft(this.position);
643653

644654
this.updateCenter();

0 commit comments

Comments
 (0)