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;
		}		
	}
}

Air for my Desire


Now I’m proud. Setting up everything was quite a pain, and I still do not grasp the whole process. Nevertheless, the first interactive app, written in AS3, using Flash Builder, works fullscreen on my HTC Desire! The secret plan is to make an eye-and-ear-catching app that my 7-months baby girl could not turn off by random hand events… For now, the code was that simple:

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 mx.events.ResizeEvent;
 
	public class Android extends Sprite
	{
		private var lx:Number;
		private var ly:Number;
		private var bg:Sprite;
 
		public function Android()
		{
			bg = new Sprite;
			addChild(bg);
 
			this.stage.scaleMode = StageScaleMode.NO_SCALE;
			this.stage.align = StageAlign.TOP_LEFT;			
			addEventListener(Event.RESIZE, handleStageResize);
			bg.addEventListener(MouseEvent.CLICK, handleClick);
			bg.addEventListener(TouchEvent.TOUCH_TAP, handleTap);
 
			lx = this.stage.stageWidth * 0.5;
			ly = this.stage.stageHeight * 0.5;
 
			layout();
		}
 
		private function handleStageResize(e:Event):void
		{
			layout();			
		}
 
		private function layout():void
		{
			var g:Graphics = bg.graphics;
			g.clear();
 
			g.beginFill(0x003333,1);
			g.drawRect(0,0,stage.stageWidth, stage.stageHeight);
			g.endFill();			
 
			g.beginFill(0xff0000,1);
			g.drawRect(lx - 50, ly - 50, 100,100);
			g.endFill();
 
			g.beginFill(0xffff00,1);
			g.drawRect(Math.random() * stage.stageWidth, Math.random() * stage.stageHeight, 5,5);
			g.endFill();			
		}
 
		private function handleClick(e:MouseEvent):void
		{
			trace('asdasd');
			lx = e.localX;
			ly = e.localY;
			layout();
		}
 
		private function handleTap(e:TouchEvent):void
		{
			trace('asdasd');
			lx = e.localX;
			ly = e.localY;
			layout();
		}		
	}
}

Next Page »