Home > Articles > Web Development > Dreamweaver & Flash

  • Print
  • + Share This
This chapter is from the book

Controlling the Accelerometer

With the release of the Flash Player 10.1 and Adobe Integrated Runtime, AIR 2.0, the Flash team added several new core API features (New!). Access to a devices Accelerometer is one of those. The role of the Accelerometer is to detect when you move your phone. The Accelerometer is a listener that is triggered when it is used. The following example adds an Accelerometer listener to your iPhone App.

Add an Accelerometer Listener

  • orange-circle-1.jpg Create a new iPhone App and add the necessary development properties in the iPhone settings.
  • orange-circle-2.jpg Add a dynamic text field to the Stage with the name myTextField in the Properties panel.
  • orange-circle-3.jpg Create a new layer on the Timeline with the named Actions, and then select the Actions layer.
  • orange-circle-4.jpg Open the Actions panel.
  • orange-circle-5.jpg Add code to import the libraries for the Accelerometer to work correctly:
    import flash.events.AccelerometerEvent
    import flash.sensors.Accelerometer;
    
  • orange-circle-6.jpg Add code to create a new Accelerometer object:
    var acc1:Accelerometer = new Accelerometer()
    
  • orange-circle-7.jpg Add a boolean object to test if the Accelerometer works or not:
    var isSupported:Boolean = Accelerometer.isSupported;
        checksupport();
    
  • orange-circle-8.jpg Add a function that contains the event listener, which waits for the Accelerometer to be triggered:
    function checksupport():void {
     if (isSupported) {
      myTextField.text = "Accelerometer feature supported";
    acc1.addEventListener (AccelerometerEvent.UPDATE, updateHandler);
    } else {
     myTextField.text = "howdy ";  }
    }
    
  • orange-circle-9.jpg Add a function that posts a message to the text field to tell what direction the device has moved to:
     function
     updateHandler(evt:AccelerometerEvent):void {
    myTextField.text = String("at: " + evt.timestamp
     + "\n" + "acceleration X: " + evt.accelerationX
     + "\n" + "acceleration Y: " + evt.accelerationY
     + "\n" + "acceleration Z: " +
     evt.accelerationZ);
    
  • orange-circle-10.jpg Publish and package your file into an iPhone App and test it on your iPhone.

The Accelerometer gives you new ways for your customers to interface with your applications beyond touchscreen controls. The Accelerometer works great on the iPhone but the same code can be used for Adobe AIR apps running on Google’s Android OS, Palm’s WebOS and RIM’s BlackBerry phones. Yes, that’s right. Develop one App and have it deployed to multiple mobile devices.

  • + Share This
  • 🔖 Save To Your Account