Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Touch Support to Gumball game #190

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions static/scenes/gumball/js/controls.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,15 @@ app.Controls = function(game) {
this.onKeyDown_ = this.onKeyDown_.bind(this);
this.onKeyUp_ = this.onKeyUp_.bind(this);
this.onDeviceOrientation_ = this.onDeviceOrientation_.bind(this);
this.onTouchStart_ = this.onTouchStart_.bind(this);
this.onTouchEnd_ = this.onTouchEnd_.bind(this);

// Events are cleared by app.Game in its dispose method.
$(window).on('keydown.gumball', this.onKeyDown_);
$(window).on('keyup.gumball', this.onKeyUp_);
$(window).on('deviceorientation.gumball', this.onDeviceOrientation_);
$(window).on('touchstart.gumball', this.onTouchStart_);
$(window).on('touchend.gumball', this.onTouchEnd_);
};

/**
Expand Down Expand Up @@ -121,3 +125,35 @@ app.Controls.prototype.onKeyUp_ = function(e) {
this.isRightDown = false;
}
};

/**
* Handles the on Touch Start. Called dynamically.
* @param {!Event} e The event object.
* @private
*/
app.Controls.prototype.onTouchStart_ = function(e) {
// Get the horizontal position where the touch started
var touchX = e.touches[0].clientX;

if (touchX < window.innerWidth / 2) { // Left
this.isLeftDown = true;
} else { // Right
this.isRightDown = true;
}

// Let tutorial know about touch so it can hide the tutorial.
if (!this.touchStarted) {
this.tutorial.off('device-tilt');
this.touchStarted = true;
}
};

/**
* Handles the on Touch End. Called dynamically.
* @param {!Event} e The event object.
* @private
*/
app.Controls.prototype.onTouchEnd_ = function(e) {
this.isLeftDown = false;
this.isRightDown = false;
};