Compass is an extension that provides access to a device’s native compass sensor. Allows developers to retrieve compass reading information such as magnetic and true heading.
Some features:
- Provides access to native device compass sensor
- Provides compass reading information, i.e. true / magnetic heading
- Works across iOS and Android with the same code
- Sample project code and ASDocs reference
Sample
/** * __ __ __ * ____/ /_ ____/ /______ _ ___ / /_ * / __ / / ___/ __/ ___/ / __ `/ __/ * / /_/ / (__ ) / / / / / /_/ / / * \__,_/_/____/_/ /_/ /_/\__, /_/ * / / * \/ * http://distriqt.com * * @file TestCompass.as * @brief * @author Michael Archbold (ma@distriqt.com) * @created Jan 19, 2012 * @updated $Date:$ * @copyright http://distriqt.com/copyright/license.txt */ package { import com.distriqt.extension.compass.Compass; import com.distriqt.extension.compass.events.CompassEvent; import flash.display.Sprite; import flash.display.StageAlign; import flash.display.StageScaleMode; import flash.events.Event; import flash.events.MouseEvent; import flash.text.TextField; import flash.text.TextFormat; /** * Sample application for using the Compass Native Extension * * @author Michael Archbold (ma@distriqt.com) */ public class TestCompass extends Sprite { private var _registered : Boolean = false; private var _text : TextField; private var _heading : TextField; /** * Constructor */ public function TestCompass() { super(); stage.align = StageAlign.TOP_LEFT; stage.scaleMode = StageScaleMode.NO_SCALE; var tf:TextFormat = new TextFormat(); tf.size = 24; _text = new TextField(); _text.defaultTextFormat = tf; addChild( _text ); tf.color = 0x00FF00; tf.size = 36; _heading = new TextField(); _heading.defaultTextFormat = tf; addChild( _heading ); stage.addEventListener( Event.RESIZE, stage_resizeHandler, false, 0, true ); stage.addEventListener( MouseEvent.CLICK, mouseClickHandler, false, 0, true ); addEventListener( Event.ACTIVATE, activateHandler, false, 0, true ); addEventListener( Event.DEACTIVATE, deactivateHandler, false, 0, true ); message( "TestCompass" ); try { Compass.init( "your_dev_key" ); message( String(Compass.isSupported) ); Compass.service.addEventListener( CompassEvent.HEADING_UPDATED, compass_headingUpdatedHandler, false, 0, true ); } catch (e:Error) { message( "ERROR:"+e.message ); } } //////////////////////////////////////////////////////// // VARIABLES // //////////////////////////////////////////////////////// // FUNCTIONALITY // //////////////////////////////////////////////////////// // INTERNALS // private function message( str:String ):void { trace( str ); _text.appendText(str+"\n"); } //////////////////////////////////////////////////////// // EVENT HANDLERS // private function stage_resizeHandler( event:Event ):void { _text.width = stage.stageWidth; _text.height = stage.stageHeight - 100; _heading.y = stage.stageHeight - 85; _heading.width = stage.stageWidth; _heading.height = 50; } private function mouseClickHandler( event:MouseEvent ):void { try { if (Compass.isSupported) { if (!_registered) { message("registering" ); Compass.service.register(); } else { message("unregistering"); Compass.service.unregister(); } _registered = !_registered; } } catch (e:Error) { message( "ERROR:"+e.message ); } } private function compass_headingUpdatedHandler( event:CompassEvent ):void { // message( event.type +":"+ event.magneticHeading+":"+ event.trueHeading+":"+ event.headingAccuracy ); _heading.text = String(event.magneticHeading) +" ["+event.headingAccuracy+"]"; } private function activateHandler( event:Event ):void { } private function deactivateHandler( event:Event ):void { try { trace( "deactivateHandler() "); if (_registered) { Compass.service.unregister(); _registered = false; } } catch (e:Error) { message( "ERROR:"+e.message ); } } } }
RT @as3gamegears: New: Compass (extension to access a device’s native compass sensor) http://t.co/wHQPzrut #as3 #flash #gamedev