| <?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.display.Sprite; |
| import flash.events.AccelerometerEvent; |
| import flash.events.Event; |
| import flash.sensors.Accelerometer; |
| |
| import spark.events.ViewNavigatorEvent; |
| import spark.skins.mobile.TextAreaSkin; |
| |
| 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')" |
| source480dpi="assets/icons/480/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')" |
| source480dpi="assets/icons/480/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. |
| 
 
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:navigationContent> |
| <s:Button click="navigator.popView()"> |
| <s:icon> |
| <s:MultiDPIBitmapSource source160dpi="@Embed('assets/icons/160/1_navigation_previous_item.png')" |
| source240dpi="@Embed('assets/icons/240/1_navigation_previous_item.png')" |
| source320dpi="@Embed('assets/icons/320/1_navigation_previous_item.png')"/> |
| </s:icon> |
| </s:Button> |
| </s:navigationContent> |
| |
| <s:actionContent> |
| <s:Button click="buttonHandler(event)" icon.DemoState="{aboutIcon}" icon.InfoState="{demoIcon}"/> |
| </s:actionContent> |
| |
| </s:View> |