Hi Everyone,
I’ve been following a tutorial on creating a webcam motion detect game for a project. The game(code below) basically works where, when a person waves their hand over the ball movieclip, the function ‘hitDetect’ activates the movie clip and plays the movieclip.
I wanted to incorporate multiple balls on the stage (multiple moveclips) so i did it by using Arrays and loops so that they would all respond to the function ‘hitDetect’
BUT it doesn’t seem to work, when i test the movie, only ‘ball1’ responds to the action, where as ‘ball2’ and ‘ball3’ stays inactive.
This is my code below. Please help as this is quite urgent for my project, thanks.
var video_vobj:Video;
var cam:Camera = Camera.getCamera();
video_vobj.attachCamera(cam)
this.addEventListener(Event.ENTER_FRAME, hitDetect);
import flash.display.BitmapData;
var screenS = new BitmapData(cam.width, cam.height);
var videoX:Number = video_vobj.x
var videoY:Number = video_vobj.y;
var videoW:Number = video_vobj.width;
var videoH:Number = video_vobj.height;
var sizeDif:Number = videoW/cam.width;
var now = new BitmapData(cam.width, cam.height);
var before = new BitmapData(cam.width, cam.height);
var ball:Array = new Array(ball1);
ball.push(ball2,ball3);
function hitDetect(e:Event):void {
for (var i:Number=0; i<ball.length; i++) {
var ballX:Number = (ball*.x-videoX)/sizeDif
var ballY:Number = (ball*.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) {
trace ("hit")
if (ball*.currentFrame == 1)
ball*.gotoAndPlay(2)
}
before.draw(video_vobj)
}
}