Starling, hitTest: rotation and scale - actionscript-3

I'm working on a game in which the shapes have to collide after changing rotation and size. As long as image1 isn't rotated using its .pivotX and .pivotY the hitTest works but as soon as I rotate around pivot the collision is wrong. I tried to use matrix but without good results.
Anyone can help me? Here the code:
package
{
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.geom.Matrix;
import flash.geom.Point;
import flash.geom.Rectangle;
import starling.core.Starling;
import starling.display.Image;
import starling.display.Sprite;
import starling.events.Event;
import starling.textures.Texture;
import starling.textures.TextureAtlas;
public class HitTest extends Sprite
{
[Embed(source="src/image.png")]
public static const ImagePng:Class;
public function HitTest()
{
addEventListener(starling.events.Event.ADDED_TO_STAGE, init);
}
private function init():void{
//First Image
var bitmap1:Bitmap = new ImagePng();
var texture1:Texture = Texture.fromBitmap(bitmap1);
var image1:Image = new Image(texture1);
addChild(image1);
/*
Here's the problem:
If you don't use .pivotX and .pivotY image1 doesn't rotate around its center but hits image2
If you use .pivotX and .pivotY image1 rotates around its center but the hittest doesn't work.
*/
// image1.pivotX = image1.width/2;
// image1.pivotY = image1.height/2;
image1.x=215;
image1.y=50;
image1.rotation=1;
var rect1:Rectangle = image1.getBounds(this);
var offset1:Matrix = new Matrix;
offset1.rotate(1);
offset1.tx = image1.x - rect1.x;
offset1.ty = image1.y - rect1.y;
var bitmapData1:BitmapData = new BitmapData(rect1.width, rect1.height, true, 0);
bitmapData1.draw(bitmap1, offset1);
//Second Image
var bitmap2:Bitmap = new ImagePng();
var texture2:Texture = Texture.fromBitmap(bitmap2);
var image2:Image = new Image(texture2);
addChild(image2);
image2.x=270;
image2.y=-10;
var rect2:Rectangle = image2.getBounds(this);
var offset2:Matrix = new Matrix;
offset2.tx = image2.x - rect2.x;
offset2.ty = image2.y - rect2.y;
var bitmapData2:BitmapData = new BitmapData(rect2.width, rect2.height, true, 0);
bitmapData2.draw(bitmap2, offset2);
var point1:Point = new Point(rect1.x, rect1.y);
var point2:Point = new Point(rect2.x, rect2.y);
//Hit Test
if(bitmapData1.hitTest(point1,255,bitmapData2,point2,255))
{
image2.color=0x00ff00;
}
}
}
}

This is again with matrix, but I think it works pretty well (it's somehow old, but the idea remains the same): http://www.mikechambers.com/blog/2009/06/24/using-bitmapdata-hittest-for-collision-detection/

Related

Stage.RESIZE : Is it possible to run this example in a browser?

Is it possible to run this simple code in a browser?
In the .swf all seems to work fine, but I'm unable to resize the .swf in a browser...
The stage is not resizing in an HTML page.
Through the .swf flile it works like a charm.
Do somebody have an idea about this issue?
package com{
import flash.display.Graphics;
import flash.display.MovieClip;
import flash.display.Stage;
import flash.display.StageAlign;
import flash.display.StageQuality;
import flash.display.StageScaleMode;
import flash.events.Event;
import flash.text.TextField;
public class Main extends MovieClip{
private static const ORANGE:int = 0xff9900;
var bg:MovieClip;
var bg_mask:MovieClip;
var marginx:int = 10;
var marginy:int = 10;
var display:MovieClip;
var displayTextField:TextField;
var ellipseWidth:int = 90;
var ellipseHeight:int = 90;
public function Main(){
super();
bg = new MovieClip();
bg_mask = new MovieClip();
display = new MovieClip();
displayTextField = new TextField();
this.addChild(bg);
this.addChild(bg_mask);
this.addChild(display);
this.display.addChild(displayTextField);
displayTextField.text = "";
displayTextField.x = ellipseWidth/Math.PI;
displayTextField.y = ellipseHeight/Math.PI;
stage.align = StageAlign.TOP_LEFT; // or StageAlign.TOP
stage.scaleMode = StageScaleMode.NO_SCALE;
drawBackground();
display.mask = bg_mask;
addListeners();
}
private function drawBackground(thickness:int=1,lineColor:int=0x000000,lineAlpha:Number=0.5,fillColor:int=ORANGE,fillAlpha:Number=0.5):void{
var g:Graphics = bg.graphics;
g.clear();
g.lineStyle(thickness,lineColor,lineAlpha);
g.beginFill(fillColor,fillAlpha);
g.drawRoundRect(marginx,marginy,this.stage.stageWidth-marginx*2,this.stage.stageHeight-marginy*2,ellipseWidth,ellipseHeight);
g.endFill();
}
private function drawMask():void{
var g:Graphics = bg_mask.graphics;
g.clear();
g.lineStyle(1,0x000000,0.3);
g.beginFill(0xcccccc,0.1);
g.drawRoundRect(marginx,marginy,this.stage.stageWidth-marginx*2,this.stage.stageHeight-marginy*2,90,90);
g.endFill();
}
private function addListeners():void{
stage.addEventListener(Event.ADDED,updateLabel);
stage.addEventListener(Event.RESIZE,updateLabel);
}
private function updateLabel(e:Event):void{
updateDisplay();
drawBackground();
drawMask();
}
private function updateDisplay():void{
var tf:TextField = displayTextField;
tf.text = ("{" + this.stage.stageWidth + ";" + this.stage.stageHeight + "}");
}
}
}
In an online context, the flash document is contained within an HTML parent element (object/embed).
Your AS3 code controls how to display your flash content within that element. It does not know anything beyond it's container.
Most likely, your issue is that HTML container. If it has a fixed size, then no matter what you do in AS3 it will not scale beyond it's container.
Try giving your HTML container fluid dimensions (eg. 100% width, 100% height) as well as any grandparent html elements to make your sizing more dynamic.

Problems with STAGE

I am pretty new to as3 programming. This forum helped me already a lot, but now I have a problem where I don't know how to get on. So this is my first Post on stack overflow.com.
I need StageWebView for displaying a PDF-document. After several hours, I was successful. I created the code in a new blank document and tested it step by step.
This is my code:
import flash.display.MovieClip;
import flash.media.StageWebView;
import flash.geom.Rectangle;
import flash.filesystem.File;
import flash.display.Sprite;
import flash.display.Stage;
public function StageWebViewExample(pdfdoc:String, xpos:Number, ypos:Number, breite:Number, hoehe:Number)
{
var webView:StageWebView = new StageWebView();
webView.stage = this.stage; //PROBLEM LINE
webView.viewPort = new Rectangle (xpos, ypos, breite, hoehe);
var file:String = pdfdoc;
var pdf:File = File.applicationDirectory.resolvePath(file);
webView.loadURL(pdf.nativePath);
}
StageWebViewExample("test.pdf", 200, 200, 600, 1200);
After testing, I copied the code in my existing flash-document. (The code in a several as-File and the "calling" (StageWebViewExample("....) in the existing flash-document...)
But now the code does't work anymore and there are the following Errors:
- 1119 Access of possibly undefined property stage...
- 1059 Property is read-only.
--> Both Errors referring to the same line I marked in the Code.
Has anyone an idea why it don't work?
I would really appreciate a good hint!
Answer for question is your actions ;) You moved code from the Document Class, in another one, that don't have access to the stage as instance property. I mean this.stage.
Pass Stage also to the method StageWebViewExample, function signature will look like:
public function stageWebViewExample(stage: Stage, pdfdoc:String, xpos:Number, ypos:Number, breite:Number, hoehe:Number):void {
var webView:StageWebView = new StageWebView();
webView.stage = stage;
webView.viewPort = new Rectangle (xpos, ypos, breite, hoehe);
var file:String = pdfdoc;
var pdf:File = File.applicationDirectory.resolvePath(file);
webView.loadURL(pdf.nativePath);
}
Working AIR example:
package {
import flash.display.Sprite;
import flash.display.Stage;
import flash.display.StageAlign;
import flash.display.StageScaleMode;
import flash.events.Event;
import flash.geom.Rectangle;
import flash.media.StageWebView;
public class StackOverflow extends Sprite {
public function StackOverflow() {
addEventListener(Event.ADDED_TO_STAGE, onAdded);
}
private function onAdded(e:Event):void {
removeEventListener(Event.ADDED_TO_STAGE, onAdded);
stage.align = StageAlign.TOP_LEFT;
stage.scaleMode = StageScaleMode.NO_SCALE;
start();
}
private function start():void {
showWebViewAt(this.stage, "http://www.iab.net/media/file/VASTv3.0.pdf", new Rectangle(0, 0, stage.stageWidth * 0.5, stage.stageHeight));
}
private function showWebViewAt(stage:Stage, path:String, frame:Rectangle):void {
var webView:StageWebView = new StageWebView();
webView.stage = stage;
webView.viewPort = frame;
webView.loadURL(path);
}
}
}

Showing main menu over objects / movieclips?

I am creating a game in Flash and I am creating a main menu for the game (With buttons like 'Play', 'How to Play', 'Hiscores' etc.) and was wondering what is the best way to go about it?
All of my Actionscript code is in external .as files and I've used classes throughout but I was having trouble figuring out how to get it so that the menu will be shown as soon as the game is ran. The main problem is that there are timers in my game that have event handlers attached to them and I was trying to think of the best way to essentially stop these timers until the user actually clicks 'Play', otherwise the objects spawn over the top of the menu and the timer ticks down.
Would stopping the timers but then adding an event handler to the play button to start the timers be a good idea? I am trying to figure out the best way to do this for future reference.
Thank you for any assistance.
Edit: Tried Cherniv's advice, getting some errors.
Main.as:
package {
import flash.display.MovieClip;
import flash.display.Sprite;
import flash.display.DisplayObject;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.events.TimerEvent;
import flash.utils.Timer;
import flash.ui.Mouse;
import flash.media.Sound;
import flash.media.SoundChannel;
import flash.text.TextFormat;
import flash.text.TextField;
import flash.display.Loader;
import flash.net.URLRequest;
import flash.net.URLLoader;
import flash.system.LoaderContext;
import flash.display.Sprite;
import flash.net.Socket;
public class Main extends MovieClip {
public static var gameLayer:Sprite = new Sprite;
public static var endGameLayer:Sprite = new Sprite;
public static var menuLayer:Sprite = new Sprite;
public var gameTime:int;
public var levelDuration:int;
public var crosshair:crosshair_mc;
static var score:Number;
var enemyShipTimer:Timer;
var enemyShipTimerMed:Timer;
var enemyShipTimerSmall:Timer;
static var scoreHeader:TextField = new TextField();
static var scoreText:TextField = new TextField();
static var timeHeader:TextField = new TextField();
static var timeText:TextField = new TextField();
public function Main()
{
var mainMenu:myMenu = new myMenu;
addChild(gameLayer);
addChild(endGameLayer);
addChild(menuLayer);
playBtn.addEventListener(MouseEvent.CLICK, startButtonPressed);
}
function startButtonPressed(e:Event)
{
levelDuration = 30;
gameTime = levelDuration;
var gameTimer:Timer = new Timer(1000,levelDuration);
gameTimer.addEventListener(TimerEvent.TIMER, updateTime);
gameTimer.addEventListener(TimerEvent.TIMER_COMPLETE, timeExpired)
gameTimer.start();
scoreHeader = new TextField();
scoreHeader.x = 5;
scoreHeader.text = String("Score: ");
gameLayer.addChild(scoreHeader);
scoreText = new TextField();
scoreText.x = 75;
scoreText.y = 0;
scoreText.text = String(0);
gameLayer.addChild(scoreText);
timeHeader = new TextField();
timeHeader.x = 490;
timeHeader.y = 0;
timeHeader.text = String("Time: ");
gameLayer.addChild(timeHeader);
timeText = new TextField();
timeText.x = 550;
timeText.y = 0;
timeText.text = gameTime.toString();
gameLayer.addChild(timeText);
var scoreFormat = new TextFormat("Arial Rounded MT Bold", 20, 0xFFFFFF);
scoreHeader.setTextFormat(scoreFormat);
scoreText.setTextFormat(scoreFormat);
timeHeader.setTextFormat(scoreFormat);
timeText.setTextFormat(scoreFormat);
enemyShipTimer = new Timer(2000);
enemyShipTimer.addEventListener("timer", sendEnemy);
enemyShipTimer.start();
enemyShipTimerMed = new Timer(2500);
enemyShipTimerMed.addEventListener("timer", sendEnemyMed);
enemyShipTimerMed.start();
enemyShipTimerSmall = new Timer(2750);
enemyShipTimerSmall.addEventListener("timer", sendEnemySmall);
enemyShipTimerSmall.start();
crosshair = new crosshair_mc();
gameLayer.addChild(crosshair);
crosshair.mouseEnabled = crosshair.mouseChildren = false;
Mouse.hide();
gameLayer.addEventListener(Event.ENTER_FRAME, moveCursor);
resetScore();
}
function sendEnemy(e:Event)
{
var enemy = new EnemyShip();
gameLayer.addChild(enemy);
gameLayer.addChild(crosshair);
}
function sendEnemyMed(e:Event)
{
var enemymed = new EnemyShipMed();
gameLayer.addChild(enemymed);
gameLayer.addChild(crosshair);
}
function sendEnemySmall(e:Event)
{
var enemysmall = new EnemyShipSmall();
gameLayer.addChild(enemysmall);
gameLayer.addChild(crosshair);
}
static function updateScore(points)
{
score += points;
scoreText.text = String(score);
var scoreFormat = new TextFormat("Arial Rounded MT Bold", 20, 0xFFFFFF);
scoreHeader.setTextFormat(scoreFormat);
scoreText.setTextFormat(scoreFormat);
}
static function resetScore()
{
score = 0;
scoreText.text = String(score);
}
function updateTime(e:TimerEvent):void
{
trace(gameTime);
// your class variable tracking each second,
gameTime--;
//update your user interface as needed
var scoreFormat = new TextFormat("Arial Rounded MT Bold", 20, 0xFFFFFF);
timeText.defaultTextFormat = scoreFormat;
timeText.text = String(gameTime);
}
function timeExpired(e:TimerEvent):void
{
var gameTimer:Timer = e.target as Timer;
gameTimer.removeEventListener(TimerEvent.TIMER, updateTime)
gameTimer.removeEventListener(TimerEvent.TIMER, timeExpired)
// do whatever you need to do for game over
}
function moveCursor(event:Event)
{
crosshair.x=mouseX;
crosshair.y=mouseY;
}
}
}
Menu.as:
package {
import flash.display.MovieClip;
import flash.display.Sprite;
import flash.display.DisplayObject;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.events.TimerEvent;
import flash.utils.Timer;
import flash.ui.Mouse;
import flash.media.Sound;
import flash.media.SoundChannel;
import flash.text.TextFormat;
import flash.text.TextField;
import flash.display.Loader;
import flash.net.URLRequest;
import flash.net.URLLoader;
import flash.system.LoaderContext;
import flash.display.Sprite;
import flash.net.Socket;
public class Menu extends MovieClip
{
var mainMenu:Menu = new Menu();
Main.menuLayer.addChild(myMenu);
playBtn.addEventListener(MouseEvent.CLICK, playBtnPressed);
function playBtnPressed()
{
Main.menuLayer.removeChild(myMenu);
dispatchEvent(new Event("playButtonPressed"))
}
}
My menu is a movieclip named myMenu and the class is set as Menu but I get errors such as:
Main.as, Line 50 1120: Access of undefined property playBtn
I gave the button an instance name of playBtn before I converted the menu to a movieclip so not sure what's going on there. I'm probably missing something really easy but it's a bit confusing for me after typing all day.
If you have all of your initialization stuff (including timers initializations) in Main constructor function , so you need to split it to two functions , Main constructor will show the menu , and "start" button will fire the second function , that will include all the initialization stuff

Flash Text Engine - TextLine calculates height incorrectly in some font

I am using Flash Text Engine's TextBlock & TextLine to display text. However, I found that some font make TextLine calculate height incorrectly. Here is my sample code:
package users
{
import flash.display.Sprite;
import flash.display.StageScaleMode;
import flash.events.Event;
import flash.geom.Rectangle;
import flash.text.TextField;
import flash.text.TextFormat;
import flash.text.engine.ElementFormat;
import flash.text.engine.FontDescription;
import flash.text.engine.FontLookup;
import flash.text.engine.FontPosture;
import flash.text.engine.FontWeight;
import flash.text.engine.RenderingMode;
import flash.text.engine.TextBaseline;
import flash.text.engine.TextBlock;
import flash.text.engine.TextElement;
import flash.text.engine.TextLine;
public class TestTextSize extends Sprite
{
private var textBlock:TextBlock = null;
private var textElement:TextElement = null;
private var _textField:TextField;
private var _textFormat:TextFormat;
private var elementFormat:ElementFormat;
private var _textLine:TextLine;
[Embed(source="assets/fonts/SWANSE__.TTF", fontFamily="Swansea", mimeType="application/x-font", embedAsCFF="true")]
private const Swansea:Class;
public function TestTextSize()
{
super();
addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
}
private function onAddedToStage(e:Event):void {
removeEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
stage.scaleMode = StageScaleMode.NO_SCALE;
var fontDescription:FontDescription = new FontDescription("Swansea"
, FontWeight.NORMAL, FontPosture.NORMAL, FontLookup.EMBEDDED_CFF, RenderingMode.CFF);
elementFormat = new ElementFormat();
elementFormat.fontDescription = fontDescription;
elementFormat.fontSize = 114.5;
elementFormat.color = 0x990000;
textElement = new TextElement("This is some text", elementFormat);
textBlock = new TextBlock(textElement);
textBlock.baselineZero = TextBaseline.ASCENT;
_textLine = textBlock.createTextLine(null, 1000,0,true);
_textLine.y = 0;
addChild(_textLine);
trace(_textLine.height);
trace(_textLine.textHeight);
trace(_textLine.totalHeight);
trace(_textLine.totalAscent);
trace(_textLine.totalDescent);
var rect:Rectangle = _textLine.getBounds(this);
trace(rect.y, rect.height);
var sprite:Sprite = new Sprite();
sprite.graphics.beginFill(0x004400);
sprite.graphics.drawRect(rect.x, rect.y, rect.width, rect.height);
sprite.graphics.endFill();
sprite.alpha = 0.3;
sprite.y = 0;
addChild(sprite);
}
}
}
The font I'm using is Swansea (http://www.fontspace.com/roger-white/swansea) standard normal font.
From the code, I extract TextLine's height by using getBounds, and use that height to draw rectangle. The rectangle appears to has height shorter than the actual text, means that getBounds can't get correct actual text height.
The result from running the code looks like this:
The console give the following result:
57.6
57.59326171875
57.59326171875
0
57.59326171875
0 57.6
which means TextLine.textHeight, height, totalHeight, and result from getBounds give the same value, roughly 57.6 pixel.
So, my question is, how can I get the actual text height?
Please don't tell me not to use Swansea. So far, I have tried 5-6 different fonts and only Swansea give me this problem. But, how do I know that this problem will not happen again? At least, if there is a way to distinguish font that give this problem and font that won't, that would be highly appreciated.
I have found that if I comment out the line
textBlock.baselineZero = TextBaseline.ASCENT;
then the totalHeight and totalAscent property will return 83.81396484375.
Maybe you could also try to re-package the font with a font editor.

adding noise to a video in actionscript

Before I begin, I would like to apologize if what I'm going to ask is to be considered as super newbie question.
so I was trying out Lee Brimelow's tutorial on creating a simple AR scene using actionscript3. (http://blog.theflashblog.com/?p=901)
It worked out pretty well, considering I have never created something out of Adobe Flex Builder before. (I installed Flex Builder right after I saw those tutorial)
here is the source code
package {
import flash.display.BitmapData;
import flash.display.Sprite;
import flash.events.Event;
import flash.media.Camera;
import flash.media.Video;
import flash.utils.ByteArray;
import org.libspark.flartoolkit.core.FLARCode;
import org.libspark.flartoolkit.core.param.FLARParam;
import org.libspark.flartoolkit.core.raster.rgb.FLARRgbRaster_BitmapData;
import org.libspark.flartoolkit.core.transmat.FLARTransMatResult;
import org.libspark.flartoolkit.detector.FLARSingleMarkerDetector;
import org.libspark.flartoolkit.support.pv3d.FLARBaseNode;
import org.libspark.flartoolkit.support.pv3d.FLARCamera3D;
import org.papervision3d.lights.PointLight3D;
import org.papervision3d.materials.shadematerials.FlatShadeMaterial;
import org.papervision3d.materials.utils.MaterialsList;
import org.papervision3d.objects.primitives.Cube;
import org.papervision3d.render.BasicRenderEngine;
import org.papervision3d.scenes.Scene3D;
import org.papervision3d.view.Viewport3D;
[SWF(width="640", height="480", frameRate="30", backgroundColor="#FFFFFF")]
public class FLARdemo extends Sprite
{
[Embed(source="pat1.pat", mimeType="application/octet-stream")]
private var pattern:Class;
[Embed(source="camera_para.dat", mimeType="application/octet-stream")]
private var params:Class;
private var fparams:FLARParam;
private var mpattern:FLARCode;
private var vid:Video;
private var cam:Camera;
private var bmd:BitmapData;
private var raster:FLARRgbRaster_BitmapData;
private var detector:FLARSingleMarkerDetector;
private var scene:Scene3D;
private var camera:FLARCamera3D;
private var container:FLARBaseNode;
private var vp:Viewport3D;
private var bre:BasicRenderEngine;
private var trans:FLARTransMatResult;
public function FLARdemo()
{
setupFLAR();
setupCamera();
setupBitmap();
setupPV3D();
addEventListener(Event.ENTER_FRAME, loop);
}
private function setupFLAR():void
{
fparams = new FLARParam();
fparams.loadARParam(new params() as ByteArray);
mpattern = new FLARCode(16, 16);
mpattern.loadARPatt(new pattern());
}
private function setupCamera():void
{
vid = new Video(640, 480);
cam = Camera.getCamera();
cam.setMode(640, 480, 30);
vid.attachCamera(cam);
addChild(vid);
}
private function setupBitmap():void
{
bmd = new BitmapData(640, 480);
bmd.draw(vid);
raster = new FLARRgbRaster_BitmapData(bmd);
detector = new FLARSingleMarkerDetector(fparams, mpattern, 80);
}
private function setupPV3D():void
{
scene = new Scene3D();
camera = new FLARCamera3D(fparams);
container = new FLARBaseNode();
scene.addChild(container);
var pl:PointLight3D = new PointLight3D();
pl.x = 1000;
pl.y = 1000;
pl.z = -1000;
var ml:MaterialsList = new MaterialsList({all: new FlatShadeMaterial(pl)});
var Cube1:Cube = new Cube(ml, 30, 30, 30);
var Cube2:Cube = new Cube(ml, 30, 30, 30);
Cube2.z = 50
var Cube3:Cube = new Cube(ml, 30, 30, 30);
Cube3.z = 100
container.addChild(Cube1);
container.addChild(Cube2);
container.addChild(Cube3);
bre = new BasicRenderEngine();
trans = new FLARTransMatResult();
vp = new Viewport3D;
addChild(vp);
}
private function loop(e:Event):void
{
bmd.draw(vid);
try
{
if(detector.detectMarkerLite(raster, 80) && detector.getConfidence() > 0.5)
{
detector.getTransformMatrix(trans);
container.setTransformMatrix(trans);
bre.renderScene(scene, camera, vp);
}
}
catch(e:Error){}
}
}
}
what I am asking is :
is it possible for us to add some noise in the final video output? I'm trying to find out the PSNR by adding noises respectively.
can I do some convolution between the noise and the video?
oh btw I'm doing this for my assignment in college. My prof wanted me to explain how exactly the FlarToolKit works. (details such as Matrix Projections, Obtaining Errors by doing Iterative Method, etc.)
Thank you.
Prama
Creating fine-grained noise dynamically is computationally expensive, so I generate low-res noise and scale it up. In your project, simply add the bitmap setup code after the addEventListener call and then add the single line to generate the noise to your activity loop.
package
{
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.BlendMode;
import flash.display.Sprite;
import flash.events.Event;
import flash.geom.Matrix;
import flash.media.Camera;
import flash.media.Video;
import flash.utils.getTimer;
public class NoiseMain extends Sprite
{
protected static const VID_WIDTH : uint = 640;
protected static const VID_HEIGHT : uint = 480;
protected static const NOISE_SCALE_X : Number = 4;
protected static const NOISE_SCALE_Y : Number = 2;
protected var _vid : Video;
protected var _noise : BitmapData;
protected var _composite : BitmapData;
protected var _matrix : Matrix;
public function NoiseMain()
{
// We're creating a webcam outlet, but not adding it to the stage since we want to post-process the image.
_vid = new Video(VID_WIDTH, VID_HEIGHT);
var cam : Camera = Camera.getCamera();
cam.setMode(VID_WIDTH, VID_HEIGHT, 30);
_vid.attachCamera(cam);
var w : uint = Math.ceil(VID_WIDTH / NOISE_SCALE_X);
var h : uint = Math.ceil(VID_HEIGHT / NOISE_SCALE_Y);
_noise = new BitmapData(w, h, false, 0xFFFFFF);
_composite = new BitmapData(VID_WIDTH, VID_HEIGHT, false, 0x000000);
var bmp : Bitmap = new Bitmap(_composite);
addChild(bmp);
_matrix = new Matrix(NOISE_SCALE_X, 0, 0, NOISE_SCALE_Y);
stage.addEventListener(Event.ENTER_FRAME, enterFrameHandler);
}
protected function enterFrameHandler(evt : Event) : void
{
_noise.noise(getTimer(), 204, 255, 7, true);
_composite.lock();
_composite.draw(_vid);
_composite.draw(_noise, _matrix, null, BlendMode.MULTIPLY);
_composite.unlock();
}
}
}
EDIT: I've revised my example to be more in line with what you're trying to do. You can tweak the noise scale constants to change the "texture" of the noise as well as messing with the strength of the noise (the 2nd and 3rd arguments to the noise() method) and the visual appearance of the noise by changing the BlendMode in the 2nd draw() call).