Bitmap inside a Sprite - But still not clickable? AS3 - actionscript-3

Basically I've got a bitmap which is an image that the user captures from a webcam which was previously stored as bitmapdata which I converted into a bitmap and added it onto the stage..
From here I want the bitmap to be selectable so I can do other things to it, so I realised I would need to add this into sprite in order to add event listeners onto it.
Having done so, for some reason my application ain't wanting to recognise the events added?
Here is the code I've got..
Bitmap..
//Create a new bitmap from the video capture
image = new Bitmap(bitmap,"auto",true);
//Set the width and height properties
image.width=150;
image.height=125;
//Set the stage position
image.x = 430;
image.y = 280;
stage.addChild(image);
Sprite..
var imageSprite:Sprite;
imageSprite = new Sprite();
imageSprite.x = 200;
imageSprite.y = 50;
imageSprite.addChild(image);
stage.addChild(imageSprite);
Event listener:
imageSprite.addEventListener(MouseEvent.MOUSE_DOWN, SelectWebImage1);
Function:
function SelectWebImage1(e:MouseEvent): void {
trace("Clicked");
}
I receive no errors from this but I noticed that when I have that code written in my application, all of the code underneath it doesn't seem to function?
I really don't know what i'm doing wrong, any help appreciated thanks!

When you set Sprite's dimensions, you implicitly set its scale, and also if you place some graphics by using width and height, the width and height includes any sprite's children, accounting for their position. You should place that bitmap into imageSprite and not set x,y proerties for the bitmap, this way you won't have to account for its position inside the sprite to position the bitmap.

Related

Add blur to everything in an area AS3

I'd like to know how can I add blur in a certain area for example behind a movieclip, maybe a mask that will blur everything in the area of that moviclip.
I don't want to blur everything, just what's behind a movieclip :)
Sort of like Apple is doing with their menus
The only way to do this in AS3 is draw whatever is behind the MovieClip to a Bitmap, then blur the bitmap.
For example, say your MovieClip is a modal dialog of some sort with a semi-transparent background, you could use the following script (inside the MovieClip) to blur whatever is on the stage behind it onto a bitmap surface:
// create a bitmap surface to use as a blurred background
var blurData:BitmapData = new BitmapData(width, height, false);
var blur:Bitmap = new Bitmap(blurData);
addChildAt(blur, 0); // put the blur surface behind everything in the movieclip
// this function draws the portion of the stage that's behind the movieclip
// onto the bitmap surface, then blurs it
function drawBlurBehind():void {
// fill the bitmap with the stage color first
blurData.fillRect(blurData.rect, stage.color);
// account for the coordinate offset of the stage and movieclip
var offset:Point = globalToLocal(new Point());
var matrix:Matrix = new Matrix();
matrix.tx = offset.x;
matrix.ty = offset.y;
// hide the movieclip so it doesn't show up in the blurred background
visible = false;
// draw the stage behind the movieclip onto the bitmap
blurData.draw(stage, matrix);
// blur the background bitmap
blurData.applyFilter(blurData, blurData.rect, new Point(), new BlurFilter(25, 25));
// show the movieclip after the background has been blurred
visible = true;
}
Note that this drawing is not "live" and any time anything changes (you move the movieclip or what's behind it changes) you have to redraw. You could use an ENTER_FRAME event handler to continually redraw every frame, which would basically make it live, but that will be relatively expensive so avoid it if you can.
Also note that this script draws and blurs the whole stage (excluding the movieclip), not what is strictly "behind" the movieclip. So if you want things to appear above the movieclip and not appear blurred behind the movieclip, you'll have to set those to visible=false while you draw the blurred background.

AS3 - Setting BitmapData Dimensions

I'm brand new to AS3, trying to take a bitmap from the library and display it in the timeline with actionscript. Here's the code I'm using so far as per Adobe's site:
addChild(new Bitmap(new myBitmap(100, 100)));
and this works, but changing the dimensions in those brackets doesn't. How can I change the dimensions?
Also, how can I change the position of the bitmap? Right now it's at 0,0.
You first get a handler of that bitmap:
var theBitmap:Bitmap=new myBitmap();
Then, you can change its coordinates:
theBitmap.x=100;
theBitmap.y=100;
addChild(theBitmap);
In order to do scaling, you can also alter its scaleX and scaleY properties as with any other displayed object. In order to make a Bitmap object to be of certain size on screen, you have to scale it so that scaling parameter equals (new dimensions)/(old dimensions). But, if you want something else, you have to explain it further.

AS3 erase bitmap that has filters

So I use bitmap.bitmapData.draw(erase,null,null,"erase"); to erase part of a bitmap that has the sprite called erase over it. The problem is that I need the bitmap to be blurred, and after I apply a blur filter the bitmapData.draw function doesn't work anymore. Meaning absolutely nothing happens. I tried using bitmapdata.applyFilter, which almost works except when I erase near a blurred edge it "unblurs" itself.
var erase:Sprite=new Sprite();
erase.cacheAsBitmap = true;
var blur:BlurFilter = new BlurFilter(10,10,10);
var blurs:Array=new Array();
blurs.push(blur);
bitmap=new Bitmap(bitmapdata);
bitmap.blendMode = "layer";
bitmap.y=-10;
bitmap.x=-10;
Refs._stage.addChildAt(bitmap,0);
also:
erase.graphics.beginFill(0xFF0000);
erase.graphics.drawCircle(stage.mouseX+10,stage.mouseY+10,35);
erase.filters = blurs;
erase.graphics.endFill();
bitmap.bitmapData.draw(erase,null,null,"erase");
It's just how the erase draw works. It erases pixels based on the alpha value of the object being drawn in erase mode. Where the alpha is above 0.5, that pixel get its alpha dropped to zero. Other pixels are not touched. This is what you experience when you draw with erase, then blur, then draw with erase again. No, Flash is not intelligent enough to do erase-blur, you might have to do it yourself. You might try drawing a transparent BitmapData with BlendMode.ERASE to do a blur-erase, but I don't expect it to return a blurred bitmap with blurred erased regions.

AS3 - Thumbnail generator

I've a MovieClip with 10 frames and a photo (1024x768 px) in each frame. Now I'ld like to create a 174x174 px thumbnail from it and place it into a container (instance: thumb1 - thumb10)
How to make this happen?
var mc:MovieClip = new MovieClip();
var bitmapData:BitmapData = new BitmapData(mc.width, mc.height, ... );
bitmapData.draw(mc);
then just use the bitmapData to create a Bitmap and scale it the way you like.
Easiest way would be to create the thumbnails outside of Flash in a graphics editor (such as Photoshop or Paint.net). This would increase your file size slightly, but would probably work the best.
A simple way to do this in Flash, would be to give each image a Class linkage in the library, and just create a new instance of them when you need it. You can just set width and height (or scaleX and scaleY) on the Bitmaps to resize them, but make sure you set smoothing to true for each of them, or they wont look very nice.

Deleting a shape via code

Pretty basic question here, but its still got me a little confused..
I have an object(navigation menu bar) that I want to change the colors on with code, so in an updateColor function, I get the bounds of the object (which is a drawing shape contained in a movieclip) and redraw a new shape on top of it with the new color, but I've noticed that the last shape still exists behind this redraw.
I tried using obj.graphics.clear(); before the redraw but that didn't get rid of the original shape. Is there another command that I'm overlooking?
Unless you drew the object you wish to remove within the same graphics object, clearing won't work. You need to remove the DisplayObject.
Depending on the number of children you can do:
obj.removeChildAt(0);
This also removes movieclips / buttons you placed on the stage manually.
If you have a reference to the DisplayObject you wish to remove you can simply do
obj.removeChild(backgroundClip);
Note that you can also change the color of a DisplayObject directly:
import flash.geom.ColorTransform;
...
public var test:MovieClip; //instance on stage
...
var cf:ColorTransform = test.transform.colorTransform;
cf.color = 0xff0000;
test.transform.colorTransform = cf;
while(this.numChildren)
{
this.removeChildAt(0);
}
Will clear child object on this MovieClip,
if it's clearing too much, then put the shape drawing in a subclip, and clear the subclip.