Home Tutorials Forums Articles Blogs Movies Library Employment Press
Old 10-15-2010, 09:58 AM   #1
Mr.Chillin
Registered User
 
Join Date: Mar 2008
Posts: 19
Default AS3 Webcam Game

Hello everyone,
recently I got stuck with a problem to which, unfortunately, I can't find a solution. I'm new to as3 and I decided to ask for an advice here. Maybe someone can help me out.

So what is going on:
I'm working on a very simple motion detection based webcam game.
I have a class for motion tracking by Brian Hadaway which creates a BitmapData on active zones. Here is a code:

ActionScript Code:
package {     import flash.events.TimerEvent;     import flash.utils.Timer;     import flash.geom.Matrix;     import flash.display.Sprite;     import flash.display.BitmapData;     import flash.display.Bitmap;     import flash.text.TextField;     import flash.media.Camera;     import flash.media.Video;     import flash.geom.Point;     import flash.geom.Rectangle;     /**      *      * BasicTracker      *      * @version 1.0 | 4/27/2010      * @author Brian Hadaway      *      * The BasicTracker is a class used for motion tracking with      * a webcam in ActionScript 3          public class BasicTracker extends Sprite     {                 // ----------------------------------         // PRIVATE MEMBERS         // ----------------------------------                         private var _camera : Camera;         private var _video : Video;         private var _output : BitmapData;         private var _difference : BitmapData;         private var _previous : BitmapData;         private var _label : TextField;         private var _renderTimer : Timer;         private var _cameraFPS : Number = 30;         private var _cameraW : Number = 550;         private var _cameraH : Number = 400;         private var _bm : Bitmap;         private var _matrix : Matrix;         private var _showVideo : Boolean = true;         private var _renderFrequency : Number = 100;         private var _thresholdMask : uint = 0xFF333333;         private var _showMotion : Boolean = true;         private var _motionArea : Rectangle = new Rectangle();         private var _motionColor : uint = 0xFF00FF00;         // ----------------------------------         // CONSTRUCTOR         // ----------------------------------                 public function BasicTracker()         {                                  init();         }                 // ----------------------------------         // PUBLIC METHODS         // ----------------------------------                 /**          * Initialize          */                 public function init() : void         {             _camera = Camera.getCamera();             _camera.setMode(_cameraW, _cameraH, _cameraFPS);             if (_camera == null) {                 _label = new TextField();                 _label.text = "NO WEBCAM";                 addChild(_label);             }             else             {                 _matrix = new Matrix(-1,0,0,1,_camera.width,0);                 _video = new Video(_camera.width, _camera.height);                 _video.transform.matrix = _matrix;                 _video.attachCamera(_camera);                 addChild(_video);                 _video.visible = false;                 _output = new BitmapData(_video.width, _video.height, true, 0x00000000);                 _difference = new BitmapData(_video.width, _video.height);                 _previous = new BitmapData(_video.width, _video.height);                                 _bm = new Bitmap(_output);                 addChild(_bm);                 _bm.visible = _showMotion;                                                 _renderTimer = new Timer(_renderFrequency);                 _renderTimer.addEventListener(TimerEvent.TIMER, render, false, 0, true);                 _renderTimer.start();                             }         }                 /**          * Render tracked motion          *          * @param e  TimerEvent dispatched by _renderTimer          */         public function render(e:TimerEvent = null) : void         {             if (!_camera.currentFPS) return;                         _video.visible = _showVideo;             _difference.draw(_video,_matrix);             _difference.draw(_previous,_matrix,null,"difference");             _output.fillRect(new Rectangle(0,0,output.width,output.height),0x00000000);             _output.threshold(_difference, new Rectangle(0,0,_difference.width,_difference.height), new Point(0,0), ">", _thresholdMask, _motionColor);             _previous.draw(_video);             _motionArea = output.getColorBoundsRect(0xFFFFFFFF, _motionColor);         }                 // ----------------------------------         // PUBLIC ACCESSORS         // ----------------------------------                 /**          * BitmapData used to track motion          */         public function get output() : BitmapData         {             return _output;         }                 /**          * Whether video is displayed          */         public function get showVideo() : Boolean         {             return _showVideo;         }                 public function set showMotion(value : Boolean) : void         {             _showMotion = value;         }                 /**          * Whether motion is displayed          */         public function get showMotion() : Boolean         {             return _showMotion;         }                 public function set showVideo(value : Boolean) : void         {             _showVideo = value;         }                 /**          * Camera width          */         public function get cameraW() : uint         {             return _cameraW;         }                 public function set cameraW(value : uint) : void         {             _cameraW = value;         }                 /**          * Camera height          */         public function get cameraH() : uint         {             return _cameraH;         }                 public function set cameraH(value : uint) : void         {             _cameraH = value;            }                 /**          * Camera FPS          */         public function get cameraFPS() : uint         {             return _cameraFPS;         }                 public function set cameraFPS(value : uint) : void         {             _cameraFPS = value;         }                 /**          * Frequency that motion is tracked (milliseconds)          */           public function get renderFrequency() : uint         {             return _renderFrequency;         }                 public function set renderFrequency(value : uint) : void         {             _renderFrequency = value;         }                 /**          * Mask color used to isolate motion          */         public function get thresholdMask() : uint         {             return _thresholdMask;         }                 public function set thresholdMask(value : uint) : void         {             _thresholdMask = value;         }                 /**          * Rectangle that bounds motionArea          */         public function get motionArea() : Rectangle         {             return _motionArea;         }                 /**          * Color used in rendering motion visualization (ARGB)          */         public function get motionColor() : uint         {             return _motionColor;         }                 public function set motionColor(value : uint) : void         {             _motionColor = value;         }     } }


Then I've created an "Enemy" class which I've extended and drew a Bitmap shape to hit test camera activity with an object. "Enemy" has different images in it that swap randomly and I can't make bitmap out of it right away because u will need to animate it. So I've created hit zone which is bitmap. 2 reason why I used bitmap to hit test it is that Basic Tracker class does not accept other hit tests as hitTestObject(); ot hitTestPoint(); it wants only hitTest();

Here is my main class code:

ActionScript Code:
package {     import flash.display.Bitmap;     import flash.display.MovieClip;     import flash.events.TimerEvent;     import flash.geom.Point;     import flash.utils.Timer;             public class Game extends BasicTracker     {         public var gameTimer:Timer;         public var enemyTimer:Timer;         public var army:Array;         public var enemy:Enemy;                 public function Game()         {                      army = new Array();             gameTimer = new Timer(25);             enemyTimer = new Timer(100);             enemyTimer.addEventListener(TimerEvent.TIMER, createEnemy);             gameTimer.addEventListener(TimerEvent.TIMER, onTick);             gameTimer.start();             enemyTimer.start();                     }                 public override function init():void         {             showVideo = true;             showMotion = true;             super.init();         }                 //creating enemies         public function createEnemy(timerEvent:TimerEvent):void         {             if ( Math.random() < 0.1 )             {                 var randomX:Number = Math.random() * 400;                 var randomImage:Number = Math.floor(Math.random()*7)+1;                 var newEnemy:Enemy = new Enemy(randomX, -100 );                 army.push( newEnemy );                                 newEnemy.gotoAndStop(randomImage);                                 addChild( newEnemy );                             }         }                 public function onTick(timerEvent:TimerEvent):void         {                var i:int = army.length - 1;             var enemy:Enemy;             while ( i > -1 )             {                 enemy = army[i];                 enemy.moveABit(0,2);                 //hit test detection with camera                    //!The problem here is that when an object gets hit entire array gets deleted.                 if(output.hitTest(new Point(0, 0),255, enemy.enemyBMP, new Point(enemy.enemyBMP.x, enemy.enemyBMP.y)))                 {                     removeChild( enemy );                     army.splice( i, 1 );                     //enemy.alpha = 0;                     trace("hit and removed!");                 }                                 if(enemy.y > 342)                 {                     removeChild( enemy );                     army.splice( i, 1 );                 }                 i = i - 1;             }                                 }             } }


As seen from the code I create an Array called Army which i populate with enemies and make them move down and it works just fine. It works great when I delete an array child every time it gets to the so called "blind zone" in the bottom of the screen where i delete it to clean computer memory from unused objects. But when i hit test it... the function:

ActionScript Code:
if(output.hitTest(new Point(0, 0),255, enemy.enemyBMP, new Point(enemy.enemyBMP.x, enemy.enemyBMP.y)))                 {                     removeChild( enemy );                     army.splice( i, 1 );                     //enemy.alpha = 0;                     trace("hit and removed!");                 }

removes every child from the Army Array... but this function:

ActionScript Code:
if(enemy.y > 342) {   removeChild( enemy );   army.splice( i, 1 ); }

deletes them one by one when they exeed 342 pixels on y axis.
How can I hit test every child in array and delete only that child that was hit?
I hope i made myself clear if you need additional information I'll provide it. Hope someone can help me out.
Mr.Chillin is offline   Reply With Quote
Old 10-15-2010, 09:59 AM   #2
Mr.Chillin
Registered User
 
Join Date: Mar 2008
Posts: 19
Default Enemy Class

Here is Enemy class:
ActionScript Code:
package {     import flash.display.Bitmap;     import flash.display.BitmapData;     import flash.display.MovieClip;     import flash.display.Sprite;         public class Enemy extends MovieClip     {         public var enemyBD:BitmapData;         public var enemyBMP:Bitmap;         public var enemyArea:Sprite;                 public function Enemy(startX:Number,startY:Number)         {             drawCircle();             createBMP();             x = startX;             y = startY             stop();         }                 public function moveABit(xSpeed:Number, ySpeed:Number):void         {             x = x + xSpeed + ((Math.random() * 2) -1);             y = y + ySpeed;         }                 public function drawCircle():void         {             //draw cirlce             enemyArea = new Sprite();             enemyArea.graphics.beginFill(0x000000,0.1);             enemyArea.graphics.drawCircle(44,44,44);             enemyArea.graphics.endFill();         }                 public function createBMP():void         {             //create bitmap out of circle             enemyBD = new BitmapData(enemyArea.width,enemyArea.height,true,0x000000);             enemyBD.draw(enemyArea);             enemyBMP = new Bitmap(enemyBD);             addChild(enemyBMP);             enemyArea.graphics.clear();         }                     } }
Mr.Chillin is offline   Reply With Quote
Reply


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT. The time now is 10:39 AM.

///
Follow actionscriptorg on Twitter

 


Powered by vBulletin® Version 3.8.5
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Ad Management plugin by RedTyger
Copyright 2000-2013 ActionScript.org. All Rights Reserved.
Your use of this site is subject to our Privacy Policy and Terms of Use.