DZone
Mobile Zone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Mobile Zone > Blast Aliens in Your Browser with Your iOS Trigger Device

Blast Aliens in Your Browser with Your iOS Trigger Device

Chris Smith user avatar by
Chris Smith
·
Mar. 21, 12 · Mobile Zone · Interview
Like (2)
Save
Tweet
15.99K Views

Join the DZone community and get the full member experience.

Join For Free
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.

Aliens (novel series) BLAST (protocol) mobile app Node.js Browser-based Web application QR code Data (computing) Space (architecture)

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • 10 Programming Habits a Web Developer Should Embrace
  • How to Submit a Post to DZone
  • 12 Modern CSS Techniques For Older CSS Problems
  • Spring, IoC Containers, and Static Code: Design Principles

Comments

Mobile Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo