# Webcam motion detection

**URL:** <https://forum.kirupa.com/t/webcam-motion-detection/291551>\
**Category:** Uncategorized\
**Created:** [July 1, 2009, 8:33am UTC](https://forum.kirupa.com/t/webcam-motion-detection/291551 "2009-07-01T08:33:14Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![gius](https://avatars.discourse-cdn.com/v4/letter/g/4af34b/32.png) [@gius](https://forum.kirupa.com/u/gius)\
**Post date:** [July 1, 2009, 8:33am UTC](https://forum.kirupa.com/t/webcam-motion-detection/291551/1 "2009-07-01T08:33:14Z")

</div>

Hi,  
I have found a simple tutorial,  
but I can not when there is  
movement in the webcam to make the ball disappear.

I have added  
delete ball\_mc

and inserted

var root: movieclip = this;  
/ /  
/ / 1. Get your webcam on the screen  
/ /  
var video\_vobj: Video;  
var cam: Camera = Camera.get ();  
video\_vobj.attachVideo (cam);  
/ /  
/ / 2. the activityLevel property  
/ /  
this.onEnterFrame = function () (  
var actLevel: Number = cam.activityLevel;  
root.act\_txt.text = “activity level =” + actLevel;  
);  
cam.onActivity = function (isActive: Boolean) (  
);  
/ /  
/ / 3. The BitmapData class  
/ /  
import flash.display.BitmapData;  
var screens = new BitmapData (cam.width, cam.height);  
snap1\_btn.onRelease = function () (  
screenS.draw (video\_vobj);  
root.attachBitmap (screens, 1);  
);  
var videox: Number = video\_vobj.\_x;  
var videoY: Number = video\_vobj.\_y;  
var videoW: Number = video\_vobj.\_width;  
var videoH: Number = video\_vobj.\_height;

snap2\_btn.onRelease = function () (  
screenS.draw (video\_vobj);  
var holder: movieclip root.createEmptyMovieClip = ( “holder”, 1);  
holder.attachBitmap (screens, 1);  
holder.\_x = videox;  
holder.\_y = videoY;  
holder.\_width = videoW;  
holder.\_height = videoH;  
);  
release\_btn.onRelease = function () (  
root.holder.removeMovieClip ()  
);  
/ /  
/ / 4. Hit detection: Setup  
/ /  
var speed: Number = 2;  
moveBall function () (  
ball\_mc.\_x + = speed;  
if (ball\_mc.\_x\> (+30 + videox videoW)) (  
ball\_mc.\_x = videox-30;  
ball\_mc.\_y = videoY + random (videoH-60) +30;  
)  
)  
ball\_mc.onEnterFrame = moveBall;  
/ /  
/ / 5. Hit detection  
/ /  
var sizeDif: Number = videoW / cam.width;  
var now = new BitmapData (cam.width, cam.height);  
var before = new BitmapData (cam.width, cam.height);  
hitDetect function () (  
var ballX: Number = (ball\_mc.\_x-videox) / sizeDif  
Bally var: Number = (ball\_mc.\_y-videoY) / sizeDif  
now.draw (video\_vobj)  
var valNow: Number = (now.getPixel (ballX, Bally)\>\> 16 & 0xFF);  
var valBefore: Number = (before.getPixel (ballX, Bally)\>\> 16 & 0xFF);  
if (valNow\> valBefore +30 | | valNow \<valBefore-30) (  
\*\*\*\*\*\*\*\*\*\*\*\* Delete ball\_mc  
/ / (trace “hit”)  
if (ball\_mc.\_currentframe == 1)  
ball\_mc.gotoAndPlay (2)  
)  
before.draw (video\_vobj)  
)  
var intervalID: Number = setInterval (hitDetect, 20);

but does not work,

thanks

Best Regards
