Skip to content
Browse files

Detecting if we are in a mobile browser or a normal browser to

draw the fake control areas
  • Loading branch information...
1 parent 758c81e commit 7c4d179ea96eff145b829060aa9ffe9cfc6184f6 Francisco Jordano committed Mar 30, 2012
Showing with 29 additions and 12 deletions.
  1. +11 −0 index.html
  2. +2 −2 js/car.js
  3. +16 −10 js/game.js
View
11 index.html
@@ -192,6 +192,17 @@
window.msCancelRequestAnimationFrame ? msCancelRequestAnimationFrame(handle.value) :
clearInterval(handle);
};
+
+ window.isMobileBrowser = function() {
+ var useragent = navigator.userAgent;
+ useragent = useragent.toLowerCase();
+
+ if (useragent.indexOf('iphone') != -1 || useragent.indexOf('symbianos') != -1 || useragent.indexOf('ipad') != -1 || useragent.indexOf('ipod') != -1 || useragent.indexOf('android') != -1 || useragent.indexOf('blackberry') != -1 || useragent.indexOf('samsung') != -1 || useragent.indexOf('nokia') != -1 || useragent.indexOf('windows ce') != -1 || useragent.indexOf('sonyericsson') != -1 || useragent.indexOf('webos') != -1 || useragent.indexOf('wap') != -1 || useragent.indexOf('motor') != -1 || useragent.indexOf('symbian') != -1 ) {
+ return true;
+ }
+
+ return false;
+ }
var mycanvas = document.getElementById("gamecanvas");
the_game = new Game({ canvas:mycanvas });
View
4 js/car.js
@@ -351,9 +351,9 @@ Car.prototype.update = function(msDuration, world){
var incr=(this.max_steer_angle/500) * msDuration;
if(this.movement & MOVE_RIGHT ){
- this.wheel_angle=Math.min(this.wheel_angle+(incr/3), this.max_steer_angle); //increment angle without going over max steer
+ this.wheel_angle=Math.min(this.wheel_angle+(incr/2), this.max_steer_angle); //increment angle without going over max steer
}else if(this.movement & MOVE_LEFT){
- this.wheel_angle=Math.max(this.wheel_angle-(incr/3), -this.max_steer_angle); //decrement angle without going over max steer
+ this.wheel_angle=Math.max(this.wheel_angle-(incr/2), -this.max_steer_angle); //decrement angle without going over max steer
}else{
// if left/right is not pressed, gradually stabilize wheels
if( this.wheel_angle<0 ) this.wheel_angle=Math.min(this.wheel_angle+incr*6, 0)
View
26 js/game.js
@@ -73,7 +73,10 @@ function Game(params) {
// physical world
this.myworld = undefined;
+ // Simple log string
this.log = "";
+
+ this.isMobile = false;
}
@@ -87,6 +90,7 @@ Game.prototype.getSprite = function(name) {
// --------------------------------------------------------------------------------------------
Game.prototype.init = function() {
+ this.isMobile = window.isMobileBrowser();
// load map data (exported as JSON from Tiled)
$.getJSON("maps/race_track.json", (function(data) {
@@ -274,9 +278,9 @@ Game.prototype.init_phase_two = function() {
'x': this.map_width/2,
'y': this.map_height/2,
'angle':Math.PI,
- 'power':7,
+ 'power':3,
'max_steer_angle':30,
- 'max_speed':30,
+ 'max_speed':20,
'game': this,
'wheels':[{'x':-0.3*car_width, 'y':-0.3*car_height, 'width':0.1, 'height':0.2, 'revolving':true, 'powered':true}, //top left
{'x':0.3*car_width, 'y':-0.3*car_height, 'width':0.1, 'height':0.2, 'revolving':true, 'powered':true}, //top right
@@ -879,14 +883,16 @@ Game.prototype.render = function() {
}
//Draw the controls LoL
- this.ctx.fillStyle = '#FF0000';
- this.ctx.fillRect(0, this.canvas.height * 0.8, this.canvas.width * 0.25, this.canvas.height);
- this.ctx.fillStyle = '#00FF00';
- this.ctx.fillRect(this.canvas.width * 0.25, this.canvas.height * 0.8, this.canvas.width * 0.25, this.canvas.height);
- this.ctx.fillStyle = '#FF0000';
- this.ctx.fillRect(this.canvas.width * 0.8, 0, this.canvas.width * 0.2, this.canvas.height * 0.5);
- this.ctx.fillStyle = '#00FF00';
- this.ctx.fillRect(this.canvas.width * 0.8, this.canvas.height * 0.5, this.canvas.width * 0.2, this.canvas.height * 0.5);
+ if(this.isMobile) {
+ this.ctx.fillStyle = '#FF0000';
+ this.ctx.fillRect(0, this.canvas.height * 0.8, this.canvas.width * 0.25, this.canvas.height);
+ this.ctx.fillStyle = '#00FF00';
+ this.ctx.fillRect(this.canvas.width * 0.25, this.canvas.height * 0.8, this.canvas.width * 0.25, this.canvas.height);
+ this.ctx.fillStyle = '#FF0000';
+ this.ctx.fillRect(this.canvas.width * 0.8, 0, this.canvas.width * 0.2, this.canvas.height * 0.5);
+ this.ctx.fillStyle = '#00FF00';
+ this.ctx.fillRect(this.canvas.width * 0.8, this.canvas.height * 0.5, this.canvas.width * 0.2, this.canvas.height * 0.5);
+ }
// request next render
requestAnimFrame(this.boundrender);

0 comments on commit 7c4d179

Please sign in to comment.
Something went wrong with that request. Please try again.