Archive for the 'Adobe AIR for Android' Category

Accelerometer, AIR on Android and Tosia

Now it is fun. I did not realize that my handset has the accelerometer sensor until I saw the video. I didn’t even know that AIR for Android supports the thing. So I tried, and it worked! I modified the code from previous posts, removing multitouch events and adding simple AccelerometerEvent. This seems quite straightforward, and the only thing I still do not get is why it is called accelerometer while it aparently measures axis tilt, not acceleration.

Runtime check if the hardware supports us:

			if (Accelerometer.isSupported) 
			{ 
				accl = new Accelerometer(); 
				accl.setRequestedUpdateInterval(200); 
				accl.addEventListener(AccelerometerEvent.UPDATE, handleAccelerometer); 
			} else{
				tf.appendText('\naccelerometer NOT supported');
			}

And the actual handler:

		private function handleAccelerometer(e:AccelerometerEvent):void
		{
			var n:int = sprites.length;
			var s:Handle;
 
			// loop through all alive sprites to update their speed
			for(var i:int = 0; i < n ;  i++){
				s = sprites[i];
				s.speedX -= e.accelerationX * 10;
				s.speedY += e.accelerationY * 10;
			}
		}
 
        // this exits the app on any key (event volume up/down!)
		private function handleKey(e:KeyboardEvent):void
		{
			NativeApplication.nativeApplication.exit();
 
		}

playing AIR on Android

Keeping on with the simplest stuff and having innocent fun with touching a device :)

Multitouch in AIR 2.5 for Android

Quick and dirty test, which surprisingly says my Wacom/Win7 do not support multitouch, but the HTC Desire does. And that is where I meant it.
This app shows red circles, each under its finger. In my case, maximum two at the same time.

package
{
	import flash.display.Graphics;
	import flash.display.Sprite;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.events.TouchEvent;
	import flash.text.TextField;
	import flash.ui.Multitouch;
	import flash.ui.MultitouchInputMode;
 
	import mx.events.ResizeEvent;
 
	public class Android extends Sprite
	{
		private var lx:Number;
		private var ly:Number;
		private var bg:Sprite;
		private var sprites:Array = [];
		private var last:Sprite;
		private var tf:TextField;
 
		public function Android()
		{
			bg = new Sprite;
			addChild(bg);
 
			this.stage.scaleMode = StageScaleMode.NO_SCALE;
			this.stage.align = StageAlign.TOP_LEFT;			
			addEventListener(Event.RESIZE, handleStageResize);
 
 
 
			Multitouch.inputMode = MultitouchInputMode.TOUCH_POINT;
			trace('multitouch support: ' + Multitouch.supportsTouchEvents);
 
			bg.addEventListener(TouchEvent.TOUCH_BEGIN, handleTouchBegin);
			bg.addEventListener(TouchEvent.TOUCH_END, handleTouchEnd);
			bg.addEventListener(TouchEvent.TOUCH_MOVE, handleMove);
 
 
			lx = this.stage.stageWidth * 0.5;
			ly = this.stage.stageHeight * 0.5;
 
			tf = new TextField;
			tf.mouseEnabled = false;
 
			tf.width = 400;
			tf.height = 200;
			tf.y = ly;
 
			addChild(tf);
			tf.text = 'multitouch support: ' + Multitouch.supportsTouchEvents;
 
			layout();
		}
 
		private function handleStageResize(e:Event):void
		{
			layout();			
		}
 
		private function layout():void
		{
			var g:Graphics = bg.graphics;
			g.clear();
 
			g.beginFill(0xAAAAA0,1);
			g.drawRect(0,0,stage.stageWidth, stage.stageHeight);
			g.endFill();			
 
		}
 
		private function getNewSprite():Sprite
		{
			var s:Sprite = new Sprite;
			s.graphics.beginFill(0xff3333,1);
			s.graphics.drawCircle(0,0,50);
			s.graphics.endFill();
			s.mouseEnabled = false;
 
 
			last = s;
			return s;
		}
 
		private function handleClick(e:MouseEvent):void
		{
			trace('asdasd');
			lx = e.localX;
			ly = e.localY;
			//layout();
		}
 
		private function handleTouchBegin(e:TouchEvent):void
		{
			tf.text = 'id ' + e.touchPointID + ' t: ' + e.target;
			var s:Sprite;
			if(sprites[e.touchPointID]){
				s = sprites[e.touchPointID];
			}else{
				s = getNewSprite();
				sprites[e.touchPointID] = s;
			} 
			s.x = e.localX;
			s.y = e.localY;
			bg.addChild(s);
		}
 
		private function handleMove(e:TouchEvent):void
		{
			var s:Sprite = sprites[e.touchPointID];
			tf.text = 'id ' + e.touchPointID + ' t: ' + e.target;
			s.x = e.localX;
			s.y = e.localY;
		}				
 
		private function handleTouchEnd(e:TouchEvent):void
		{
			var s:Sprite = sprites[e.touchPointID];
			tf.text = 'id ' + e.touchPointID + ' t: ' + e.target;
		}		
	}
}