Skip to content

Commit 171191e

Browse files
committed
Added Camera centerOnX and centerOnY methods.
1 parent f8cd237 commit 171191e

2 files changed

Lines changed: 53 additions & 8 deletions

File tree

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,8 @@
7373
* `Geom.Line.GetNearestPoint` is a new static method that will return the nearest point on a line to the given point.
7474
* `Geom.Line.GetShortestDistance` is a new static method that will return the shortest distance from a line to the given point.
7575
* `Camera.getBounds` is a new method that will return a rectangle containing the bounds of the camera.
76+
* `Camera.centerOnX` will move the camera horizontally to be centered on the given coordinate, without changing its vertical placement.
77+
* `Camera.centerOnY` will move the camera vertically to be centered on the given coordinate, without changing its horizontally placement.
7678

7779
### Updates
7880

src/cameras/2d/BaseCamera.js

Lines changed: 51 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -590,35 +590,78 @@ var BaseCamera = new Class({
590590
},
591591

592592
/**
593-
* Moves the Camera so that it is centered on the given coordinates, bounds allowing.
593+
* Moves the Camera horizontally so that it is centered on the given x coordinate, bounds allowing.
594+
* Calling this does not change the scrollY value.
594595
*
595-
* @method Phaser.Cameras.Scene2D.BaseCamera#centerOn
596-
* @since 3.11.0
596+
* @method Phaser.Cameras.Scene2D.BaseCamera#centerOnX
597+
* @since 3.16.0
597598
*
598599
* @param {number} x - The horizontal coordinate to center on.
599-
* @param {number} y - The vertical coordinate to center on.
600600
*
601601
* @return {Phaser.Cameras.Scene2D.BaseCamera} This Camera instance.
602602
*/
603-
centerOn: function (x, y)
603+
centerOnX: function (x)
604604
{
605605
var originX = this.width * 0.5;
606-
var originY = this.height * 0.5;
607606

608-
this.midPoint.set(x, y);
607+
this.midPoint.x = x;
609608

610609
this.scrollX = x - originX;
611-
this.scrollY = y - originY;
612610

613611
if (this.useBounds)
614612
{
615613
this.scrollX = this.clampX(this.scrollX);
614+
}
615+
616+
return this;
617+
},
618+
619+
/**
620+
* Moves the Camera vertically so that it is centered on the given y coordinate, bounds allowing.
621+
* Calling this does not change the scrollX value.
622+
*
623+
* @method Phaser.Cameras.Scene2D.BaseCamera#centerOnY
624+
* @since 3.16.0
625+
*
626+
* @param {number} y - The vertical coordinate to center on.
627+
*
628+
* @return {Phaser.Cameras.Scene2D.BaseCamera} This Camera instance.
629+
*/
630+
centerOnY: function (y)
631+
{
632+
var originY = this.height * 0.5;
633+
634+
this.midPoint.y = y;
635+
636+
this.scrollY = y - originY;
637+
638+
if (this.useBounds)
639+
{
616640
this.scrollY = this.clampY(this.scrollY);
617641
}
618642

619643
return this;
620644
},
621645

646+
/**
647+
* Moves the Camera so that it is centered on the given coordinates, bounds allowing.
648+
*
649+
* @method Phaser.Cameras.Scene2D.BaseCamera#centerOn
650+
* @since 3.11.0
651+
*
652+
* @param {number} x - The horizontal coordinate to center on.
653+
* @param {number} y - The vertical coordinate to center on.
654+
*
655+
* @return {Phaser.Cameras.Scene2D.BaseCamera} This Camera instance.
656+
*/
657+
centerOn: function (x, y)
658+
{
659+
this.centerOnX(x);
660+
this.centerOnY(y);
661+
662+
return this;
663+
},
664+
622665
/**
623666
* Moves the Camera so that it is looking at the center of the Camera Bounds, if enabled.
624667
*

0 commit comments

Comments
 (0)