Le principe est simple : il faut utiliser la méthode draw() pour comparer le flux de la caméra avec sa copie.
Le filtre threshold nous permettra de griller l'image au noir et blanc et ainsi capter plus précisément les mouvements.

Exemple :



Le code :

import flash.display.*;
import flash.geom.*;
///////
cam = Camera.get();
vid.attachVideo(cam);
///////
now = new BitmapData(cam.width, cam.height);
before = new BitmapData(cam.width, cam.height);
rect = new Rectangle(0, 0, cam.width, cam.height);
pt = new Point(0, 0);
/////////
_onEnterFrame = function () {
if (!cam.muted) {
now.draw(vid);
//compare l'image actuelle avec l'image de la derniere frame
//utilise le mode 'difference' pour l'effet inversé
now.draw(before, new Matrix(), new ColorTransform(), 'difference');
// remplace le gris par du blanc en grillant l'image
now.threshold(now, rect, pt, '>', 0xff111111, 0xffffffff);
//dessine l'image actuelle de la camera pour la comprarer avec la prochaine
before.draw(vid);
var val = now.getPixel(impact._x, impact._y);
//trace(val);
if (val>10000000) {
tt.text = "Le point rouge a été touché !";
impact._alpha = 20;
my_sound.start();v } else {
impact._alpha = 100;
tt.text = "...";
}
}
};
inter = setInterval(this, "_onEnterFrame", 50);
var mc:MovieClip = this.createEmptyMovieClip("mc", this.getNextHighestDepth());
mc.attachBitmap(now, 10);
mc._visible = false;
mc._x = 160;
this.attachMovie("impact", "impact", this.getNextHighestDepth(), {_x:10, _y:10});
var my_sound:Sound = new Sound(this);
my_sound.attachSound("bip");
my_sound.setVolume(10);
this.onMouseDown = function() {
mc._visible = !mc._visible;
};