Skip to content

Commit

Permalink
Merge pull request #21 from daniel-mf/master
Browse files Browse the repository at this point in the history
auto scroll set to false when reaching camera boundaries
  • Loading branch information
jdnichollsc authored Mar 20, 2018
2 parents b45bec7 + b918d45 commit 2ad99ed
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 21 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,12 +103,13 @@ The repository has some examples of the plugin, to run the examples created by t
- onUpdate callback to track delta

## Collaborators
[<img alt="VitaZheltyakov" src="https://avatars1.githubusercontent.com/u/4193707?s=117&v=4" width="117">](https://github.com/daniel-mf) |
[<img alt="VitaZheltyakov" src="https://avatars3.githubusercontent.com/u/5693437?v=3&s=117" width="117">](https://github.com/VitaZheltyakov) |
[<img alt="iamchristopher" src="https://avatars2.githubusercontent.com/u/5909516?v=3&s=117" width="117">](https://github.com/iamchristopher) |
[<img alt="daaaabeen" src="https://avatars0.githubusercontent.com/u/3760804?s=117&v=3" width="117">](https://github.com/daaaabeen) |
[<img alt="jdnichollsc" src="https://avatars3.githubusercontent.com/u/2154886?v=3&s=117" width="117">](https://github.com/jdnichollsc) |
:---: |:---: |:---: |:---: |
[Vitaliy](mailto:vita-zhelt@yandex.ru) | [Chris Wright](https://twitter.com/jorbascrumps) | [Daaaabeen](mailto:dianbin.lee@gmail.com) | [Nicholls](mailto:jdnichollsc@hotmail.com) |
:---: |:---: |:---: |:---: |:---: |
[Daniel](mailto:echo.dmf@gmail.com) | [Vitaliy](mailto:vita-zhelt@yandex.ru) | [Chris Wright](https://twitter.com/jorbascrumps) | [Daaaabeen](mailto:dianbin.lee@gmail.com) | [Nicholls](mailto:jdnichollsc@hotmail.com) |

## Other Projects
- **[IonPhaser](http://market.ionic.io/plugins/ionphaser)**
Expand Down
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "phaser-kinetic-scrolling-plugin",
"version": "1.0.3",
"version": "1.0.8",
"homepage": "https://github.com/jdnichollsc/Phaser-Kinetic-Scrolling-Plugin",
"authors": [
"Juan David Nicholls Cardona <jdnichollsc@hotmail.com>"
Expand Down
26 changes: 21 additions & 5 deletions dist/phaser-kinetic-scrolling-plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* @author Juan Nicholls <jdnichollsc@hotmail.com>
* @copyright 2018 Juan Nicholls - http://jdnichollsc.github.io/Phaser-Kinetic-Scrolling-Plugin/
* @license {@link http://opensource.org/licenses/MIT}
* @version 1.0.6
* @version 1.0.8
*/

(function (Phaser) {
Expand Down Expand Up @@ -183,12 +183,12 @@

if (typeof this.settings.onUpdate === 'function') {
var updateX = 0;
if (this.game.camera.x > 0 && this.game.camera.x + this.game.camera.width < this.game.camera.bounds.right) {
if (this.canCameraMoveX()) {
updateX = deltaX;
}

var updateY = 0;
if (this.game.camera.y > 0 && this.game.camera.y + this.game.camera.height < this.game.camera.bounds.height) {
if (this.canCameraMoveY()) {
updateY = deltaY;
}

Expand All @@ -197,6 +197,22 @@

};

/**
* Indicates when camera can move in the x axis
* @return {boolean}
*/
Phaser.Plugin.KineticScrolling.prototype.canCameraMoveX = function () {
return this.game.camera.x > 0 && this.game.camera.x + this.game.camera.width < this.game.camera.bounds.right;
};

/**
* Indicates when camera can move in the y axis
* @return {boolean}
*/
Phaser.Plugin.KineticScrolling.prototype.canCameraMoveY = function () {
return this.game.camera.y > 0 && this.game.camera.y + this.game.camera.height < this.game.camera.bounds.height;
};

/**
* Event triggered when a pointer is released, calculates the automatic scrolling.
*/
Expand Down Expand Up @@ -256,7 +272,7 @@
if (this.autoScrollX && this.amplitudeX != 0) {

delta = -this.amplitudeX * Math.exp(-this.elapsed / this.settings.timeConstantScroll);
if (delta > 0.5 || delta < -0.5) {
if (this.canCameraMoveX() && (delta > 0.5 || delta < -0.5)) {
this.game.camera.x = this.targetX - delta;
}
else {
Expand All @@ -268,7 +284,7 @@
if (this.autoScrollY && this.amplitudeY != 0) {

delta = -this.amplitudeY * Math.exp(-this.elapsed / this.settings.timeConstantScroll);
if (delta > 0.5 || delta < -0.5) {
if (this.canCameraMoveY() && (delta > 0.5 || delta < -0.5)) {
this.game.camera.y = this.targetY - delta;
}
else {
Expand Down
2 changes: 1 addition & 1 deletion dist/phaser-kinetic-scrolling-plugin.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 21 additions & 5 deletions examples/js/phaser-kinetic-scrolling-plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* @author Juan Nicholls <jdnichollsc@hotmail.com>
* @copyright 2018 Juan Nicholls - http://jdnichollsc.github.io/Phaser-Kinetic-Scrolling-Plugin/
* @license {@link http://opensource.org/licenses/MIT}
* @version 1.0.6
* @version 1.0.8
*/

(function (Phaser) {
Expand Down Expand Up @@ -183,12 +183,12 @@

if (typeof this.settings.onUpdate === 'function') {
var updateX = 0;
if (this.game.camera.x > 0 && this.game.camera.x + this.game.camera.width < this.game.camera.bounds.right) {
if (this.canCameraMoveX()) {
updateX = deltaX;
}

var updateY = 0;
if (this.game.camera.y > 0 && this.game.camera.y + this.game.camera.height < this.game.camera.bounds.height) {
if (this.canCameraMoveY()) {
updateY = deltaY;
}

Expand All @@ -197,6 +197,22 @@

};

/**
* Indicates when camera can move in the x axis
* @return {boolean}
*/
Phaser.Plugin.KineticScrolling.prototype.canCameraMoveX = function () {
return this.game.camera.x > 0 && this.game.camera.x + this.game.camera.width < this.game.camera.bounds.right;
};

/**
* Indicates when camera can move in the y axis
* @return {boolean}
*/
Phaser.Plugin.KineticScrolling.prototype.canCameraMoveY = function () {
return this.game.camera.y > 0 && this.game.camera.y + this.game.camera.height < this.game.camera.bounds.height;
};

/**
* Event triggered when a pointer is released, calculates the automatic scrolling.
*/
Expand Down Expand Up @@ -256,7 +272,7 @@
if (this.autoScrollX && this.amplitudeX != 0) {

delta = -this.amplitudeX * Math.exp(-this.elapsed / this.settings.timeConstantScroll);
if (delta > 0.5 || delta < -0.5) {
if (this.canCameraMoveX() && (delta > 0.5 || delta < -0.5)) {
this.game.camera.x = this.targetX - delta;
}
else {
Expand All @@ -268,7 +284,7 @@
if (this.autoScrollY && this.amplitudeY != 0) {

delta = -this.amplitudeY * Math.exp(-this.elapsed / this.settings.timeConstantScroll);
if (delta > 0.5 || delta < -0.5) {
if (this.canCameraMoveY() && (delta > 0.5 || delta < -0.5)) {
this.game.camera.y = this.targetY - delta;
}
else {
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "phaser-kinetic-scrolling-plugin",
"version": "1.0.6",
"release": "V 1.0.6",
"version": "1.0.8",
"release": "V 1.0.8",
"description": "Kinetic Scrolling Plugin for Phaser Framework",
"author": "Juan David Nicholls Cardona <jdnichollsc@hotmail.com>",
"logo": "https://raw.github.com/jdnichollsc/Phaser-Kinetic-Scrolling-Plugin/gh-pages/img/plugin.png",
Expand Down
Loading

0 comments on commit 2ad99ed

Please sign in to comment.