Over a million developers have joined DZone.

Blast Aliens in Your Browser with Your iOS Trigger Device

DZone 's Guide to

Blast Aliens in Your Browser with Your iOS Trigger Device

· Mobile Zone ·
Free Resource
The guys over at WebDigi, a London-based web application development company, has released a demo of Space Invaders Spaceship Pilot, a pretty amazing application that uses iOS devices as the controller for a web browser-based game on your desktop or laptop.  No application is required on the iOS device, you simply scan the QR code with the device and the controller code reads the accelerometer values and sends the movements to the node.js server.

WARNING: This demo may cause a significant decrease in productivity around the workspace, as it almost did here at DZone.

Here are the key parts of the iOS device controller code:
//Detect if the browser supports DeviceMotionEvent
if (window.DeviceMotionEvent != undefined) {
//ondevicemotion is fired when iOS device detects motion
  window.ondevicemotion = function(e) {
//ax is the movement on the x axis.
//This motion is used to move the ship in the game
  ax = event.accelerationIncludingGravity.x * 5;
  ay = event.accelerationIncludingGravity.y * 5;
//Status 0 is start, 1 is left, 2 is right, 3 is stay
if(status == 0){ //initial condition
  status = 3; //stay
  socket.emit('spaceChange', {'ax': 3});
  statusmsg = 'Waiting for movement';
if(ax > 14 && status != 2){ //move right on device
  status = 2;
  socket.emit('spaceChange', {'ax': 2});
  statusmsg = 'Moving ship right';
if(ax < -14 && status != 1){ //move left on device
  status = 1;
  socket.emit('spaceChange', {'ax': 1});
  statusmsg = 'Moving ship left';
if(ax > -14 && ax < 14 && status != 3){ //device held steady
  status = 3;
  socket.emit('spaceChange', {'ax': 3});
  statusmsg = 'Ship held steady';

Only changes in direction on the iOS device are pushed to the node.js server, reducing the amount of data pushed between phone and server.  The node.js server is coupled with Socket.IO, making it easier to deliver data in realtime using most web browsers.  Since Socket.IO code is run on both the server and client sides, pairing it with node.js allows accelerometer values to be pushed instantly from mobile safari to your browser.

After testing (read: playing) the demo for a few minutes, I couldn't help but be impressed with the speed at which the accelerometer data was sent to the browser.  As pointed out on the WebDigi Blog:

Current space invaders game uses only X axis movement on the iOS device.  However, all three dimension values are available on iOS Safari ondevicemotion method.  It is technically possible to do much more with the iOS device.

-- webdigi

So if you have an iOS device, and you miss playing Space Invaders, be sure to give this demo a try.

Also, special thanks to Roshan Abraham (DZone username: rabraham) for sending us a link to this information.  If you know of any cool demos, useful tips, etc. be sure to submit them to DZone using this button:

which is located near the top of each page.


Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}