Do you like my tutorials?

Then consider supporting me on Ko-fi

Talking about HTML5, Javascript and Phaser.

Do you know gyro.js? gyro.js is an adaptor which combines all the current interfaces and standards on reading Gyro and Accelerometer information and combines them into one simple object.

I am testing it with Phaser and in a few lines I made a “tilt to move” prototype.

You can point your mobile browser to this link to test it, unfortunately with your desktop you won’t see anything but if you open the page with a mobile browser you should make it work, although it breaks when the device changes orientation.

But this is a problem you can solve when you port it to a native app.

This is the source code:

window.onload = function() {

	// game definition, 320x480
	var game = new Phaser.Game(320,480,Phaser.CANVAS,"",{preload:onPreload, create:onCreate});                

     // the player
     var player

     // function executed on preload
	function onPreload() {
	    	game.load.image("player","player.png");	
	}

	// function to scale up the game to full screen
	function goFullScreen(){
		game.scale.pageAlignHorizontally = true;
		game.scale.pageAlignVertically = true;
		game.scale.scaleMode = Phaser.ScaleManager.SHOW_ALL;
		game.scale.setScreenSize(true);
	}

	// function to be called when the game has been created
	function onCreate() {
          // initializing physics system
          game.physics.startSystem(Phaser.Physics.ARCADE);
		// going full screen
          goFullScreen();
          // adding the player on stage
          player = game.add.sprite(160,240,"player");
          // setting player anchor point
          player.anchor.setTo(0.5);
          // enabling physics car.body.collideWorldBounds = true;
          game.physics.enable(player, Phaser.Physics.ARCADE);
          // the player will collide with bounds
          player.body.collideWorldBounds = true;
          // setting player bounce
          player.body.bounce.set(0.8);
	     // setting gyroscope update frequency
          gyro.frequency = 10;
		// start gyroscope detection
          gyro.startTracking(function(o) {
               // updating player velocity
               player.body.velocity.x += o.gamma/20;
               player.body.velocity.y += o.beta/20;
          });		
	}
}

And here you can download the full source code, with all required libraries. Just a test, but maybe you can make something useful out of it

Never miss an update! Subscribe, and I will bother you by email only when a new game or full source code comes out.