FLARManager
Hello.
I don’t normally like to post code updates without a screen shot or a link to the file, but sadly my hosting ability is down for a day or two whilst I migrate it. I’ve been working with the good old FLARToolkit and FLARManager, and my research showed that quite a few people are having issues getting FLARManager to work. Why would you want to use FLARManager? It allows you to easily use standard event listener code when working with marker detection, and it allows you to work easily with more than one marker at a time. The following code will move two squares around on the stage when two markers are seen. To use it you’ll need FLARManager:
http://words.transmote.com/wp/20090718/flarmanager-v05-for-flartoolkit/
and TweenMax:
http://blog.greensock.com/tweenmaxas3/
My document class code:
package {
import com.transmote.flar.FLARManager;
import com.transmote.flar.marker.FLARMarkerEvent;
import com.transmote.utils.time.FramerateDisplay;
import flash.display.MovieClip;
import flash.display.Sprite;
import flash.events.ErrorEvent;
import gs.TweenMax;
public class FLAR_BCard extends Sprite {
private var flarManager:FLARManager; //create a new instance of the flarmanager
private var bSquare:blueSquare = new blueSquare(); //these are the two little squares and will be replaced by much cooler shit
private var rSquare:redSquare = new redSquare();
public function FLAR_BCard () {
this.init(); //the defacto empty constructor running the init method
}
private function init () :void {
this.flarManager = new FLARManager(“resources/flarConfig.xml”); //tell the flar manager instance where it’s config file is
this.flarManager.addEventListener(ErrorEvent.ERROR, this.onFlarManagerError); //these are flar manager events, the first is a simple error handler
this.flarManager.addEventListener(FLARMarkerEvent.MARKER_ADDED, this.onMarkerAdded); //these three are like mouse down, mouse move and mouse up
this.flarManager.addEventListener(FLARMarkerEvent.MARKER_UPDATED, this.onMarkerUpdated);
this.flarManager.addEventListener(FLARMarkerEvent.MARKER_REMOVED, this.onMarkerRemoved);
}
private function onFlarManagerError (evt:ErrorEvent) :void { //simple error handler
this.flarManager.removeEventListener(ErrorEvent.ERROR, this.onFlarManagerError);
}
private function onMarkerAdded (evt:FLARMarkerEvent) :void { //this function simply adds the squares and moves them to the location of the seen marker
addChild(bSquare);
addChild(rSquare);
TweenMax.to(this.getMarkerByPatternId(evt.marker.patternId), 1, { x:evt.marker.centerpoint.x, y:evt.marker.centerpoint.y, rotationZ:evt.marker.rotationZ *-1 } );
}
private function onMarkerUpdated (evt:FLARMarkerEvent) :void {
TweenMax.to(this.getMarkerByPatternId(evt.marker.patternId), 1, { x:evt.marker.centerpoint.x, y:evt.marker.centerpoint.y, rotationZ:evt.marker.rotationZ *-1 });
}
private function onMarkerRemoved (evt:FLARMarkerEvent) :void {
TweenMax.to(this.getMarkerByPatternId(evt.marker.patternId), 1, { x:25, y:25, rotationZ:0 });
}
private function getMarkerByPatternId (patternId:int) {
switch (patternId) {
case 0:
return bSquare;
case 1:
return rSquare;
case null:
return null;
default:
return null
}
}
}
}
You’ll need to make an .fla file with two movieclips called bSquare and rSquare. I use the markers, xml and camera data parameters file that comes with FLARManager. Nice tweened 2D control of movieclips with tracked markers.