How to test multitouch inside Flash/SWF

This site provides a good tutorial for how to get things up and running, and testing things with your computer instead of having an external device, such as an iPhone.

So, the steps are actually simple once you know how to do it:

1. Get the Tuio ActionScript 3.0 library

2. Get the udp-flashlc-bridge (and simulator)

3. Build your Flash application.

Here’s some code from the website to get you up and running:

package
{
 
	import flash.display.MovieClip;
 
	import org.tuio.*;
	import org.tuio.debug.TuioDebug;
	import org.tuio.TuioManager;
	import org.tuio.connectors.*;
 
	public class Main extends MovieClip
	{
 
		// as3-tuio
		private var tuio:TuioClient;
		private var tuioManager:TuioManager;
		private var tdbg:TuioDebug;
 
		public function Main():void
		{
			//starts TUIO - LC based
			this.tuio = new TuioClient(new LCConnector());
 
			// This is the TuioClient listener, doesn't do TouchEvent events and uses a different type of listeners
			// http://bubblebird.at/tuioflash/guides/using-the-tuioclient/
			//this.tuio.addListener(this);
 
			//This activates listening to Blobs for TouchEvents.
			this.tuioManager = TuioManager.init(stage, this.tuio);
			this.tuioManager.triggerTouchOnBlob = true; 
 
			//Debugging data, just delete to remove
			tdbg = TuioDebug.init(stage);
			this.tuio.addListener(tdbg);
 
			//Nice Addition, no need for anything on the stage for TouchEvents to work, unlike Touchlib's setup
			stage.addEventListener(TouchEvent.TOUCH_DOWN, onTouchDown);
			stage.addEventListener(TouchEvent.TOUCH_MOVE, onTouchMove);
			stage.addEventListener(TouchEvent.TOUCH_UP, onTouchUp);
 
		}
 
		public function onTouchDown(evt:TouchEvent):void{
			trace("Touch ID = " + evt.tuioContainer.sessionID + ", Event = TOUCH_DOWN");
		}
		public function onTouchMove(evt:TouchEvent):void{
			trace("Touch ID = " + evt.tuioContainer.sessionID + ", Event = TOUCH_MOVE");
		}
		public function onTouchUp(evt:TouchEvent):void{
			trace("Touch ID = " + evt.tuioContainer.sessionID + ", Event = TOUCH_UP");
		}
 
	}
}

How to get a specific Flickr photo set with ActionScript 3

This is with http://as3flickrlib.googlecode.com.

var photoSetID:String = "123456789";
var service:FlickrService = new FlickrService(api_key);
service.addEventListener(FlickrResultEvent.PHOTOSETS_GET_PHOTOS, handleSetList);
service.photosets.getPhotos(photoSetID);
 
private function handleSetList(e:FlickrResultEvent):void {
		if(e.success) {
			var photoList:PhotoSet = new PhotoSet();
			photoList = e.data.photoSet;
			trace("Number of photos: " + photoList.photos.length);
			for(var i:int=0; i<photoList.photos.length; i++) {
				var imgurl:String = "http://static.flickr.com/" + photoList.photos[i].server + "/" + photoList.photos[i].id + "_" + photoList.photos[i].secret + ".jpg";
				var imageRequest:URLRequest = new URLRequest(imgurl);
				var loader:Loader = new Loader();
				loader.contentLoaderInfo.addEventListener(Event.COMPLETE, displayImages);
				loader.load(imageRequest);
			}
		}
	}

Moving a MovieClip inside a box/rectangle

Here’s some simple code on how to move a MovieClip (i.e. a bouncing ball) within a defined area, a rectangle in this case. I’ve set the whole stage to be a possible area for the ball to move, but this can of course be changed.

Add an eventlistener for each frame event.

this.addEventListener(Event.ENTER_FRAME, bounceBall);
private function bounceBall(e:Event):void {
			ball_mc.x += ballXSpeed;
			ball_mc.y += ballYSpeed;
 
			if((ball_mc.x >= stage.stageWidth - ball_mc.width) || ball_mc.x <= 0) {
				ballXSpeed *= -1;
			}
 
			if((ball_mc.y >= stage.stageHeight - ball_mc.height) || ball_mc.y <= 0) {
				ballYSpeed *= -1;
			}
		}

Difference between empty string and zero value

Type comparisons between text field string values and integers can be bothersome as an empty string would trace as 0. Undeclared integers always default to zero.

textfield.text = "";
trace(int(textfield.text)); // 0

If you need to check whether a textfield is empty or if it contains the number zero, use a boolean.

if(Boolean(textfield.text)) {
// textfield contains some input
if(textfield.text == "0") {
// textfield contains the zero character
}
}