AS3 - Setting BitmapData Dimensions - actionscript-3

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.

Related

How to use a mask in actionscript 3.0?

I want to mask the png image pattern.png with another image - mask.png, but it doesn't work at all and I can't find the reason. Instead of masking the image, the mask just disappears and the pattern stays the same as it was.
I tried making a MovieClip, drawing e.g. a circle and using that as the mask instead of mask.png and it works just fine. Is it because you can't use loader objects as masks? How do I make it work?
edit: After changing the size of mask.png to be smaller than the pattern, I've realized that it actually does kind of work, but what happens is instead of cutting the pattern into the shape I've drawn in the png file it just cuts it into the shape of the entire file, as in, it counts the rectangular transparent background as well. How can I make it cut out just the shape?
var mask:Loader = new Loader();
mask.load(new URLRequest("mask.png"));
var pattern:Loader = new Loader();
pattern.load(new URLRequest("pattern.png"));
pattern.contentLoaderInfo.addEventListener(Event.COMPLETE, loadComplete);
function loadComplete(e:Event):void {
addChild(pattern);
addChild(mask);
pattern.mask = mask;
}
Your code is looks correctly. The mask property of DisplayObject wants DisplayObject too. But try to make few things, to find the trouble:
You have only one listener, to pattern.png. But you must be sure, that mask.png has loaded already too.
Despite that Loader is DisplayObject too - try to get content from loader for mask, and just after that try to use it.
*Ah, and one more thing. You must at first add listener, and only later try to load.

Bitmap inside a Sprite - But still not clickable? AS3

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.

flash actionscript 3.0 hide part of an image

I am working on a flash sound mixer application with multiple sound channels, and I am having trouble with the lights beside the volume knob.
Is there a way to hide just a part of an image?
On the image below, image-2 is on top of image-1 to create some kind of volume level indicator effect, and how much of image-2 is shown depends on the value of the volume.
image-url: http://s30.postimg.org/r3ow1g5bl/volume_lights_level.png
I've tried by just reducing the height of image-2, but it looks awful and distorted.
Is there something in flash that works closely the same as CSS's behavior.
example: I'll just make image-2 a background of a shape, and when I reduce the shape's height, the image-background does not get distorted or changes it's height as well.
By searching for solutions, I have come across the mask property, but I don't quite understand how it works, and most of the examples shown are images placed inside circles.
Is the mask property applicable in this situation?
I'm quite new to flash so I don't know a lot of things yet.
You can indeed use a mask.
How to programmatically create your mask
Put an occurrence of your image named myImage on the stage, and put over this occurrence a mask named myMask with the same dimensions. You can apply myMask mask to myImage using it's mask property like below:
Main Timeline
myImage.mask = myMask;
function mouseMoveHandler(e:MouseEvent):void {
myMask.height = myImage.y - e.stageY;
}
stage.addEventListener(MouseEvent.MOUSE_MOVE, mouseMoveHandler);
You have just to adapt this code to your animation, in the function where you click your button.
I got it working now, many THANKS #VC.One. heres how I did it.
Imported img-2 to stage, converted it into symbol(type:Movie Clip), assigned instance name: img2_mc.
I created a new layer for the mask, drawn a rectangle using rectangle tool, converted it also to symbol(type:Movie Clip), assigned instance name: mask_mc.
Then applied the mask to img2_mc.
/* the code */
img2_mc.mask = mask_mc;
function onEnterFrame(event:Event):void{
var volumeKnob_y = volSliderKnobOn.y + 12; // adjust it to the center of the knob
mask_mc.height = volumeKnob_y;
}

As3 Calculate rotationX and rotationY value

I wonder if there is a formula that i can calculate the rotationX and rotationY value out of a sprite display object.
For example the sprite below. If a user set up all the points and draw out this shape. Is there a way i can find out rotationX and rotationY which can archive the same result?
I would like user to defined the container and whatever sprite drag into this container get auto rotationX and rotationY so the perspective stay the same.
I have found the solution by reading through the following link:
http://zehfernando.com/2010/the-best-drawplane-distortimage-method-ever/

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.