Wednesday, 11 November 2009

move a ball with your hands by using webcam movements

stop();
import flash.display.*;
import flash.geom.*;
// ----------------------------------------------------------
// INIT
// ----------------------------------------------------------
ball.inTrans = false;
setInterval(detection, 100);
gameSizeX = 250;
gameSizeY = 200;
// ----------------------------------------------------------
// Connect the webcam
// ----------------------------------------------------------
cam = Camera.get();
vid.attachVideo(cam);
// ----------------------------------------------------------
// Main Functions
// ----------------------------------------------------------
now = new BitmapData(cam.width, cam.height);
before = new BitmapData(cam.width, cam.height);
rect = new Rectangle(0, 0, cam.width, cam.height);
trace("cam.width="+cam.width+" cam.height="+cam.height);
sizeDif = gameSizeX / cam.width;
trace("This Screen Size VS Video = 1:"+ sizeDif);
trace("-----------------------------------------------");
pt = new Point(0, 0);
// Show The MonoColor image
//coverRect = new Rectangle(0, 0, 130, 120);
function detection() {

if (!cam.muted) {
now.draw(vid);
now.draw(before, new Matrix(), new ColorTransform(), 'difference');
now.threshold(now, rect, pt, '>', 0xff333333, 0xffff0000);
now.fillRect(coverRect, 0xff000000);
before.draw(vid);
//
re = now.getColorBoundsRect(0xffffffff, 0xffff0000, true);
// trace(r.toString());
ColorChange = now.threshold(now, re, new Point(re.x, re.y), '==', 0xffff0000, 0xffff0000);
// trace(ColorChange);
if (re.x<160>10) {
ballNewY = (re.height/2+re.y)*sizeDif;

}
if (re.y<200>10) {
ballNewX = re.width/2+re.x;
ballNewX = gameSizeX-(ballNewX*sizeDif);
}
tweenPlayer(ballNewY,ballNewX);
}
}

// display the motion detection bitmap
//_root.attachBitmap(now, 10);
//
vx = 4;
vy = 4;
//r = ball._width/2;
function tweenPlayer(param,param2) {
easeType = mx.transitions.easing.Regular.easeOut;
var mc = ball;
if (mc.inTrans == false) {
//mc.inTrans = true;
var begin = mc._y;
var beginX = mc._x;
var end = param;
//var endy = mc._y+(random(10)-5);
var time = 0.4;
ballTween = new mx.transitions.Tween(mc, "_y", easeType, begin, end, time, true);
ballTween = new mx.transitions.Tween(mc, "_x", easeType, beginX, param2, time, true);
ballTween.onMotionFinished = function() {
mc.inTrans = false;
};
}
}

No comments: