***************************************************************** [[ACTIONS LAYER - BASKETBALL]] ***************************************************************** stop(); //Code borrowed from Guy Watson - Adobe dev Center //get the default camera //change your Default camera using the Flash Player Settings. cam=Camera.get() //this event is called whenever permission to access the local camera, is accepted or denied by the user cam.onStatus=function(e) { //if we are given permission if(e.code == "Camera.Unmuted") { //start the application initialize() } else { System.showSettings(3) } } //if there are no Cameras if(cam == null) { System.showSettings(3) } else { output.attachVideo(cam) } ******************************************************************** [[FUNCTIONS LAYER - BASKETBALL]] ******************************************************************** import flash.display.BitmapData import flash.filters.BlurFilter import flash.geom.* //'Initialize' borrowed from Guy Watson - Adobe Dev Center function initialize() { stop(); _global.object = false; //if paddle is located or not _global.gameover = false; //if game is over _global.step = 4; //step or distance to increase falling balls _global.xcord = 0; _global.ycord = 0; _global.points = 0; //keeps track of baskets made (points = baskets*2) //*************************************************************** //following code borrowed from Guy Watson - Adobe Dev Center m=new Matrix() //create an identity matrix //scale the transformation matrix to match the scale of the Video object on stage m.scale((output._xscale/100),(output._yscale/100)) //create a bitmap object in memory that is the same size as the Video object on stage now=new BitmapData(output._width,output._height) //create a bitmap object in memory to hold a layered bitmap from the fading image cue out=new BitmapData(output._width,output._height) //create a movieclip that can display the final bitmap this.createEmptyMovieClip("output_mc",this.getNextHighestDepth()) //attach the bitmap inside the holder movie clip output_mc.attachBitmap(out,1) //position it to the right of the live Webcam feed output_mc._x=output._width output_mc._y=output._y //****************************************************************** //call the snap function every 1/10th of a second to get webcam image setInterval(this,"snap",100) //call the BallFall function to drop the balls setInterval(this,"BallFall", 80) //call the endgame function after set time (45 seconds) setInterval(this,"endGame", 45000) } function endGame() // ends the game { _global.gameover = true; gotoAndStop("scoreboard"); finalScore.text = _global.points; } function BallFall() //makes the balls drop/move and cycles them when the pass through the game area { Ball1._y +=_global.step; Ball2._y +=_global.step; Ball3._y +=_global.step; if (Ball1._y > 270) //270 = bottom of game area - get new and random x-value { RandomNum(Ball1); } if (Ball2._y > 270) { RandomNum(Ball2); } if (Ball3._y > 270) { RandomNum(Ball3); } } function RandomNum(ball:MovieClip) { minNum = 112; //left side of output screen + 2 pixel margin maxNum = (270 - 32); //right side of output screen - width of ball rndNum = Math.ceil(Math.random() * (maxNum - minNum + 1)) + (minNum - 1); ball._x = rndNum; ball._y = court._y-32; //resets it above the stage ball._visible = true; } function CheckBasket(ball:MovieClip) //checks if basketball is within hoop { if ((ball._x >= Net._x) && (ball._x <= (Net._x+8))) { if (((ball._y+32) >= (Net._y)) && ((ball._y+32) <= (Net._y+15))) { _global.points = _global.points + 2; ball._visible = false; //when basket is made hide basketball ball._x = 100; //when basket is made move basketball off game area to prevent hidden baskets Points.text = _global.points; // at 14 and 28 points, speed up game to make it harder if ((_global.points == 14) || (_global.points == 28)) { _global.step += 2; } } } } //capture a freezeframe from the webcam and copy it, called regularly do detect movement function snap() { if (_global.gameover == false ) { //*************************************************************** //following code borrowed from Guy Watson - Adobe Dev Center //draw the movieclip into the bitmapdata object object with the scaled transformation matrix now.draw(output,m) //copy the current freezeframe done=now.clone() //now draw the previous freezeframe ("BEFORE"!) on top of the current frameframe and apply the 'difference' blendmode done.draw(before,m,null,"difference") //filter out all pixels in the differenced bitmap that are greater than just over black (0xFF111111) and make them green (0xFF00FF00) //over-write the previous contents of the bitmap done.threshold(done,done.rectangle,done.rectangle.topLeft,">",0xFF111111,0xFF00FF00,0x00FFFFFF,false) //clear the out bitmap object so we can start a fresh out.fillRect(out.rectangle,0xFF000000) //copy the current bitmap and save it for the next time this function is called before=now.clone() //********************************************************************** //displays overlapped bitmap into 'out', changing pixels within threshold to green. //This image will be used to locate and track the 'object' (paddle) out.threshold(done,out.rectangle,out.rectangle.topLeft,"==",0xFF00FF00,0xFF00FF00,0x00FFFFFF,false) var controla:Number = out.getPixel(0,0); var controlb:Number = out.getPixel(out.width,out.height); outputa.text = controla; outputb.text = controlb; var object:Boolean = false; xloc.text = _global.xcord; yloc.text = _global.ycord; xloc._visible = false; yloc._visible = false; xcord._visible = false; ycord._visible = false; color._visible = false; square._visible = false; output_mc._visible = false; //will enter following 'if' statement every time except first frame if ((controla != 65280) && (controlb != 65280)) { //if object has not been found (0,0) if ((_global.xcord == 0) && (_global.ycord == 0)) { //search the top corner for (var b:Number = 0; b<(out.height/4); b++) { if (object == true) { break; } for (var a:Number = 0; a<(out.width/4); a++) { var n:Number = out.getPixel(a,b); if (n == 65280) { var h:Number = out.getPixel(a+20, b); var i:Number = out.getPixel(a, b+20); var j:Number = out.getPixel(a+20, b+20); var zero:Number = out.getPixel(0,0); if ((h == 65280) && (i == 65280) && (j == 65280) && (zero != 65280)) { Lost._visible = false; object = true; _global.xcord = a; _global.ycord = b; Net._x = ((160-c)-40)+court._x; Net._y = d+court._y; break; } } } } } // if the object has been found then use previous location (a,b) to work off else { a = _global.xcord; b = _global.ycord; if ((b >= 20) && ( a >= 20)) { var d:Number = (b-20); var c:Number = (a-20); for ((d = (b-20)); d<=(b+20); d++) { if (object == true) { break; } for ((c = (a-20)); c<=(a+20); c++) { var n:Number = out.getPixel(c,d); if (n == 65280) { var h:Number = out.getPixel(c+20, d); var i:Number = out.getPixel(c, d+20); var j:Number = out.getPixel(c+20, d+20); if ((h == 65280) && (i == 65280) && (j == 65280)) { object = true; _global.xcord = c; _global.ycord = d; if (c >= 120) //prevent graphic from going off screen { c = 120; } Net._x = ((160-c)-40)+court._x; Net._y = d+court._y; break; } } //if n } //width (x) for loop } // height (y) for loop if ((c >= (a+20)) && (d >= (b+20))) { Lost._visible = true; _global.xcord = 0; _global.ycord = 0; Net._x = court._x + court._width - 40; Net._y = court._y; } } else if ((b < 20) && (a > 20)) //if object is within 20 of top { var d:Number = (0); var c:Number = (a-20); for (d = 0; d<=40; d++) { if (object == true) { break; } for ((c = (a-20)); c<=(a+20); c++) { var n:Number = out.getPixel(c,d); if (n == 65280) { var h:Number = out.getPixel(c+20, d); var i:Number = out.getPixel(c, d+20); var j:Number = out.getPixel(c+20, d+20); if ((h == 65280) && (i == 65280) && (j == 65280)) { object = true; _global.xcord = c; _global.ycord = d; Net._x = ((160-c)-40)+court._x; Net._y = d+court._y; break; } } //if n } //width (x) for loop } // height (y) for loop if ((c >= (a+20)) && (d >= 40)) { Lost._visible = true; _global.xcord = 0; _global.ycord = 0; Net._x = court._x + court._width - 40; Net._y = court._y; } } else if((b > 20) && (a < 20)) //if object is within 20 of left { var d:Number = (b-20); var c:Number = (0); for ((d = (b-20)); d<=(b+20); d++) { if (object == true) { break; } for (c = 0; c<=40; c++) { var n:Number = out.getPixel(c,d); if (n == 65280) { var h:Number = out.getPixel(c+20, d); var i:Number = out.getPixel(c, d+20); var j:Number = out.getPixel(c+20, d+20); if ((h == 65280) && (i == 65280) && (j == 65280)) { object = true; _global.xcord = c; _global.ycord = d; Net._x = ((160-c)-40)+court._x; Net._y = d+court._y; break; } } //if n } //width (x) for loop } // height (y) for loop if ((c >= 40) && (d >= (b+20))) { Lost._visible = true; _global.xcord = 0; _global.ycord = 0; Net._x = court._x + court._width - 40; Net._y = court._y; } } else //if object is within 20 of top and left sides { var d:Number = (0); var c:Number = (0); for (d = 0; d<=40; d++) { if (object == true) { break; } for (c = 0; c<=40; c++) { var n:Number = out.getPixel(c,d); if (n == 65280) { var h:Number = out.getPixel(c+20, d); var i:Number = out.getPixel(c, d+20); var j:Number = out.getPixel(c+20, d+20); if ((h == 65280) && (i == 65280) && (j == 65280)) { object = true; _global.xcord = c; _global.ycord = d; Net._x = ((160-c)-40)+court._x; Net._y = d+court._y; break; } } //if n } //width (x) for loop } // height (y) for loop if ((c >= 40) && (d >= 40)) { Lost._visible = true; _global.xcord = 0; _global.ycord = 0; Net._x = court._x + court._width - 40; Net._y = court._y; } } } //else if object is found CheckBasket(Ball1); CheckBasket(Ball2); CheckBasket(Ball3); }// if not first 2 frames all filled green } } ***************************************************************** [[ACTIONS LAYER - AIRPLANE]] ***************************************************************** stop(); //Code borrowed from Guy Watson - Adobe dev Center //get the default camera //change your Default camera using the Flash Player Settings. cam=Camera.get() //this event is called whenever permission to access the local camera, is accepted or denied by the user cam.onStatus=function(e) { //if we are given permission if(e.code == "Camera.Unmuted") { //start the application initialize() } else { System.showSettings(3) } } //if there are no Cameras if(cam == null) { System.showSettings(3) } else { output.attachVideo(cam) } ******************************************************************** [[FUNCTIONS LAYER - AIRPLANE]] ******************************************************************** import flash.display.BitmapData import flash.filters.BlurFilter import flash.geom.* function initialize() { stop(); _global.object = false; //object = player's paddle _global.gameover = false; _global.speed = 4; //speed of oncoming objects (rocks) _global.skyspeed = 3; //speed of sky background image _global.step = 1; //speed increase for rock objects _global.xcord = 0; _global.ycord = 0; _global.points = 0; //holds players score _global.lives = 3; //number of remaining lives (planes) _global.score = false; //if object is found, then player CAN score (true) //*************************************************************** //following code borrowed from Guy Watson - Adobe Dev Center m=new Matrix()//create an identity matrix //scale the transformation matrix to match the scale of the Video object on stage m.scale((output._xscale/100),(output._yscale/100)) //create a bitmap object in memory that is the same size as the Video object on stage now=new BitmapData(output._width,output._height) //create a bitmap object in memory to hold a layered bitmap from the fading image cue out=new BitmapData(output._width,output._height) //create a movieclip that can display the final bitmap this.createEmptyMovieClip("output_mc",this.getNextHighestDepth()) //attach the bitmap inside the holder movie clip output_mc.attachBitmap(out,1) //position it to the right of the live Webcam feed output_mc._x=output._width output_mc._y=output._y //******************************************************************** //call the snap function every 1/10th of a second to get webcam image setInterval(this,"snap",100) //call the background function to make the background move setInterval(this,"Background", 80) //call the speedup function every 10 seconds to make the game faster/harder setInterval(this,"SpeedUp", 10000) //call the rocksmove function to make the rocks move setInterval(this,"RocksMove", 60) } function SpeedUp() //function increases speed of oncoming rocks as game goes on { _global.speed += _global.step; } function endGame() //when no lives are left this function will end the game. { _global.gameover = true; gotoAndStop("scoreboard2"); finalScore.text = _global.points; } function Background() //function moves the background image for a feeling of actually moving { Sky1._x -= _global.skyspeed; Sky2._x -=_global.skyspeed; if (Sky1._x <= 50-400) { Sky1._x = 440; } if (Sky2._x <= 50-400) { Sky2._x = 440; } } function RocksMove() //function moves and cycles rocks onto the stage //stage is set with 4 static 'rocks'. these rocks move left untill they are off // the screen then they get a random value and are replaced to the right of the stage. { Rock1._x -=_global.speed; Rock2._x -=_global.speed; Rock3._x -=_global.speed; Rock4._x -=_global.speed; if (Rock1._x <= 0) { RandomNum(Rock1); } if (Rock2._x <= 0) { RandomNum(Rock2); } if (Rock3._x <= 0) { RandomNum(Rock3); } if (Rock4._x <= 0) { RandomNum(Rock4); } } function RandomNum(rock:MovieClip) //random # generator for y value of rocks { minNum = 140; //left side of output screen + 2 pixel margin maxNum = 270-40; //right side of output screen - width of rock/planet rndNum = Math.ceil(Math.random() * (maxNum - minNum + 1)) + (minNum - 1); rock._y = rndNum; rock._x = 525; //resets it to the right of the stage rock._visible = true; //makes sure rock is visible } function LoseLife() //function removes a life when there is a collision { if (_global.lives == 3) { livec._visible = false; //removes livec plane image } else if (_global.lives == 2) { liveb._visible = false; //removes liveb plane image } else if (_global.lives == 1) { livea._visible = false; //removes livea plane image } else //if no lives are left the game is over { Points.text = _global.points; endGame(); } _global.lives = _global.lives - 1; _global.xcord = 0; //zero out the object location (not found) _global.ycord = 0; Plane._x = 56; //replace airplane to starting position in upper left corner Plane._y = 140; } function CheckCollision(rock:MovieClip) { if ((_global.xcord != 0 )&& (_global.ycord != 0)) //object collision doesn't count if object is lost. { _global.score = true; //object is found so scoring can continue if (((rock._x+5 <= Plane._x+65) && (rock._x+5 >= Plane._x+5)) || ((rock._x+28 <= Plane._x+65) && (rock._x+28 >= Plane._x+5))) { if (((rock._y +5 <= Plane._y+25) && (rock._y+5 >= Plane._y+5)) || ((rock._y+28 <= Plane._y+25) && (rock._y+28 >= Plane._y+5))) { LoseLife() //plane and rock collided } } } else if ((_global.xcord == 0) && (_global.ycord == 0)) // object is not found, plane is not being moved, so don't allow scoring or collisions { _global.score = false; //don't score when player isn't actually playing if (rock._x <= 270) { rock._visible = false;//don't display the rock if it is in visible game area rock._y = 100; //set the rock off the game area to avoid hidden collisions } } } //capture a freezeframe from the webcam and copy it, called regularly do detect movement function snap() { if (_global.gameover == false ) { //*************************************************************** //following code borrowed from Guy Watson - Adobe Dev Center //draw the movieclip into the bitmapdata object object with the scaled transformation matrix now.draw(output,m) //copy the current freezeframe done=now.clone() //now draw the previous freezeframe ("BEFORE"!) on top of the current frameframe and apply the 'difference' blendmode done.draw(before,m,null,"difference") //filter out all pixels in the differenced bitmap that are greater than just over black (0xFF111111) and make them green (0xFF00FF00) //over-write the previous contents of the bitmap done.threshold(done,done.rectangle,done.rectangle.topLeft,">",0xFF111111,0xFF00FF00,0x00FFFFFF,false) //clear the out bitmap object so we can start a fresh out.fillRect(out.rectangle,0xFF000000) //copy the current bitmap and save it for the next time this function is called before=now.clone() //******************************************************************** //displays overlapped bitmap into out, changing pixels within threshold to green. //This image will be used to locate and track the 'object' (paddle) out.threshold(done,out.rectangle,out.rectangle.topLeft,"==",0xFF00FF00,0xFF00FF00,0x00FFFFFF,false) //when game starts, the whole screen flashes green (we don't want to search for the object yet) var controla:Number = out.getPixel(0,0); //checks upper left corner for motion var controlb:Number = out.getPixel(out.width,out.height); //checks lower right corner for motion var object:Boolean = false; //if the paddle is found or not xcord._visible = false; // ycord._visible = false; output_mc._visible = false; if (_global.score == true) { _global.points += 1; score.text = _global.points; } //will enter following 'if' statement every time except first frame if ((controla != 65280) && (controlb != 65280)) { if ((_global.xcord == 0) && (_global.ycord == 0)) { //if the object has not yet been found, search the top right corner for (var b:Number = 0; b<(out.height/4); b++) { if (object == true) { break; } for (var a:Number = ((out.width/4)*3); a< (out.width); a++) { var n:Number = out.getPixel(a,b); if (n == 65280) { var h:Number = out.getPixel(a+20, b); var i:Number = out.getPixel(a, b+20); var j:Number = out.getPixel(a+20, b+20); var zero:Number = out.getPixel(0,0); if ((h == 65280) && (i == 65280) && (j == 65280) && (zero != 65280)) { Lost._visible = false; object = true; _global.xcord = a; _global.ycord = b; Plane._x = (160-c)+110-70; Plane._y = d + 140; break; } } } } } // if the object has been found then use previous location (a,b) to work off else { a = _global.xcord; b = _global.ycord; if ((b >= 20) && ( a >= 20)) //if object is not within 20 pixels of edge of screen { var d:Number = (b-20); var c:Number = (a-20); for ((d = (b-20)); d<=(b+20); d++) { if (object == true) { break; } for ((c = (a-20)); c<=(a+20); c++) { var n:Number = out.getPixel(c,d); if (n == 65280) { var h:Number = out.getPixel(c+20, d); var i:Number = out.getPixel(c, d+20); var j:Number = out.getPixel(c+20, d+20); if ((h == 65280) && (i == 65280) && (j == 65280)) { object = true; _global.xcord = c; _global.ycord = d; Plane._x = (160-c)+110-70; Plane._y = d + 140; break; } } //if n } //width (x) for loop } // height (y) for loop if ((c >= (a+20)) && (d >= (b+20))) // if 40x40 search area failed to find object { Lost._visible = true; _global.xcord = 0; _global.ycord = 0; Plane._x = 56; Plane._y = 140; } } else if ((b < 20) && (a > 20)) //if object is within 20 of top of screen but not left modify search area accordingly to smarter search quadrant { var d:Number = (0); var c:Number = (a-20); for (d = 0; d<=40; d++) { if (object == true) { break; } for ((c = (a-20)); c<=(a+20); c++) { var n:Number = out.getPixel(c,d); if (n == 65280) { var h:Number = out.getPixel(c+20, d); var i:Number = out.getPixel(c, d+20); var j:Number = out.getPixel(c+20, d+20); if ((h == 65280) && (i == 65280) && (j == 65280)) { object = true; _global.xcord = c; _global.ycord = d; Plane._x = (160-c)+110-70; Plane._y = d + 140; break; } } //if n } //width (x) for loop } // height (y) for loop if ((c >= (a+20)) && (d >= 40)) // if 40x40 search area failed to find object { Lost._visible = true; _global.xcord = 0; _global.ycord = 0; Plane._x = 56; Plane._y = 140; } } else if((b > 20) && (a < 20)) //if object is with 20 of top but not left modify search area accordingly to smarter search quadrant { var d:Number = (b-20); var c:Number = (0); for ((d = (b-20)); d<=(b+20); d++) { if (object == true) { break; } for (c = 0; c<=40; c++) { var n:Number = out.getPixel(c,d); if (n == 65280) { var h:Number = out.getPixel(c+20, d); var i:Number = out.getPixel(c, d+20); var j:Number = out.getPixel(c+20, d+20); if ((h == 65280) && (i == 65280) && (j == 65280)) { object = true; _global.xcord = c; _global.ycord = d; Plane._x = (160-c)+110-70; Plane._y = d + 140; break; } } //if n } //width (x) for loop } // height (y) for loop if ((c >= 40) && (d >= (b+20))) // if 40x40 search area failed to find object { Lost._visible = true; _global.xcord = 0; _global.ycord = 0; Plane._x = 56; Plane._y = 140; } } else //if object is within 20 of top and left sides { var d:Number = (0); var c:Number = (0); for (d = 0; d<=40; d++) { if (object == true) { break; } for (c = 0; c<=40; c++) { var n:Number = out.getPixel(c,d); if (n == 65280) { var h:Number = out.getPixel(c+20, d); var i:Number = out.getPixel(c, d+20); var j:Number = out.getPixel(c+20, d+20); if ((h == 65280) && (i == 65280) && (j == 65280)) { object = true; _global.xcord = c; _global.ycord = d; Plane._x = (160-c)+110-70; Plane._y = d + 140; break; } } //if n } //width (x) for loop } // height (y) for loop if ((c >= 40) && (d >= 40)) // if 40x40 search area failed to find object { Lost._visible = true; _global.xcord = 0; _global.ycord = 0; Plane._x = 56; Plane._y = 140; } } } //else if object is found CheckCollision(Rock1); //check if rock1 is in collision with plane CheckCollision(Rock2); //check if rock2 is in collision with plane CheckCollision(Rock3); //check if rock3 is in collision with plane CheckCollision(Rock4); //check if rock4 is in collision with plane }// if not first 2 frames all filled green } }