blob: fbb7c5b3067642ba3b2bb2ea7f1e83566b94fe6a [file] [log] [blame]
<?xml version="1.0" encoding="utf-8"?>
<!--
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<s:View xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark" title="Accelerometer" viewActivate="init()" viewDeactivate="deactivate(event)">
<fx:Script>
<![CDATA[
import flash.sensors.Accelerometer;
import spark.events.ViewNavigatorEvent;
private var ball:Sprite;
private var accelerometer:Accelerometer;
private var xSpeed:Number = 0;
private var ySpeed:Number = 0;
private const RADIUS:Number = 20;
private function init():void
{
stage.autoOrients = false;
createBall();
if (Accelerometer.isSupported)
{
accelerometer = new Accelerometer();
accelerometer.addEventListener(AccelerometerEvent.UPDATE, accUpdateHandler);
stage.addEventListener(Event.ENTER_FRAME, enterFrameHandler);
}
}
private function createBall():void
{
ball = new Sprite();
ball.graphics.beginFill(0xFF0000);
ball.graphics.drawCircle(0, 0, RADIUS);
ball.cacheAsBitmap = true;
ball.x = stage.stageWidth / 2;
ball.y = stage.stageHeight / 2;
container.addChild(ball);
}
private function enterFrameHandler(event:Event):void
{
event.stopPropagation();
moveBall();
}
private function moveBall():void
{
var newX:Number = ball.x + xSpeed;
var newY:Number = ball.y + ySpeed;
if (newX < 20)
{
ball.x = RADIUS;
xSpeed = 0;
}
else if (newX > stage.stageWidth - RADIUS)
{
ball.x = stage.stageWidth - RADIUS;
xSpeed = 0;
}
else
{
ball.x += xSpeed;
}
if (newY < RADIUS)
{
ball.y = RADIUS;
ySpeed = 0;
}
else if (newY > this.height - RADIUS)
{
ball.y = this.height - RADIUS;
ySpeed = 0;
}
else
{
ball.y += ySpeed;
}
}
private function accUpdateHandler(event:AccelerometerEvent):void
{
xSpeed -= event.accelerationX * 2;
ySpeed += event.accelerationY * 2;
}
protected function deactivate(event:ViewNavigatorEvent):void
{
stage.autoOrients = true;
}
protected function buttonHandler(event:MouseEvent):void
{
if(currentState == "DemoState"){
currentState = "InfoState";
}else if(currentState == "InfoState"){
currentState = "DemoState";
}
}
]]>
</fx:Script>
<s:states>
<s:State name="DemoState"/>
<s:State name="InfoState"/>
</s:states>
<fx:Declarations>
<s:MultiDPIBitmapSource id="aboutIcon" source160dpi="@Embed('/assets/icons/160/about.png')" source240dpi="@Embed('/assets/icons/240/about.png')" source320dpi="@Embed('/assets/icons/320/about.png')"/>
<s:MultiDPIBitmapSource id="demoIcon" source160dpi="@Embed('/assets/icons/160/dock.png')" source240dpi="@Embed('/assets/icons/240/dock.png')" source320dpi="@Embed('/assets/icons/320/dock.png')"/>
</fx:Declarations>
<s:SpriteVisualElement id="container" includeIn="DemoState"/>
<s:TextArea includeIn="InfoState" skinClass="spark.skins.mobile.TextAreaSkin" left="10" right="10" top="10" bottom="10" editable="false" text="The Accelerometer class dispatches events based on activity detected by the device's motion sensor. This data represents the device's location or movement along a 3-dimensional axis. When the device moves, the sensor detects this movement and returns acceleration data. The Accelerometer class provides methods to query whether or not accelerometer is supported, and also to set the rate at which acceleration events are dispatched.
&#xd; &#xd;Note: Use the Accelerometer.isSupported property to test the runtime environment for the ability to use this feature. While the Accelerometer class and its members are accessible to the Runtime Versions listed for each API entry, the current environment for the runtime determines the availability of this feature. For example, you can compile code using the Accelerometer class properties for Flash Player 10.1, but you need to use the Accelerometer.isSupported property to test for the availability of the Accelerometer feature in the current deployment environment for the Flash Player runtime. If Accelerometer.isSupported is true at runtime, then Accelerometer support currently exists."/>
<s:actionContent>
<s:Button click="buttonHandler(event)" icon.DemoState="{aboutIcon}" icon.InfoState="{demoIcon}"/>
</s:actionContent>
</s:View>