AS3: finding an object by it Instance name in a dynamic added child - actionscript-3

I am doing an Adobe AIR Kiosk app but I am having a little problem.
First step is to generate a webcam container:
var bandwidth:int = 0;
var quality:int = 100;
var camera:Camera = Camera.getCamera();
camera.setQuality(bandwidth, quality);
camera.setMode(885,575,30,true);
var video:Video = new Video(885,575);
video.attachCamera(camera);
video.name = "camara";
webcam.addChild(video);
It works ok, the problem is that I want to apply to it a custom filter
It works ok if I write it this way:
MovieClip(parent).contenedor_postal.postal.webcam.filters = [filter];
But I want to affect only the child inside the clip "webcam" without affecting other MC's, so I write it like this:
MovieClip(parent).contenedor_postal.postal.webcam.camara.filters = [filter];
and does not work. I used to program in AS2, so maybe the trick is very simple but I can't find anything that works. Thanks in advance!

If the video has a name property "camara" then this should work:
MovieClip(parent).contenedor_postal.postal.webcam.getChildByName("camara").filters = [filter];

Related

How can I remove this child image Flash / AS3

Following tutorials I managed to cobble the following together that imports an image from an external XML file.
But how do I remove it?
I've read up on removeChild but it looks like it needs to have a ref passed to it and I'm not sure what that ref is. I've tried a few things that I thought it might be, including (image) but all of them throw up Access of undefined property errors.
This is the code I'm using to import :
var imgrequest:URLRequest = new URLRequest(artwork);
var loader:Loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, on_loadComplete);
function on_loadComplete(e:Event):void {
var image:DisplayObject = loader.content;
image.x = 0;
image.y = 4;
image.width = 150;
image.height = 150;
addChild(image);
Bitmap(image).smoothing = true;
}
loader.load(imgrequest);
What do I need to do to remove it?
When you declare a variable inside a set of curly brackets, that variable only exists within those brackets. So when you try to remove the image (which DOES still exist outside the curly brackets) using that variable... no dice.
So you need to find a way to store that image with a method that results in a more permanently accessible object such as a global variable or putting it in an array (which is likewise stored globally). Now you can do
image.parent.removeChild(image);
or if it's in an array
array[ind].parent.removeChild(array[ind]);
where ind is the index of that array element you want to remove.
After using addChild(image); does later on using removeChild(image); not work?
Also try declaring var image:DisplayObject; outside of any functions (now can be universal to all functions) then in your on_loadComplete the first line can simply be image = loader.content;. Also removeChild will understand image when it's used later in another function.

Edit movie clip definition as3

I'm a bit new to as3, so forgive me if these are dumb questions. Two questions...
Premise:
I'm loading a character from a swf file, and want to add avatar to it. I have him animated walking and also standing (stand_mc, walk_mc). I also have his body parts separated out, so inside each of the animations mc's is a head_mc, body_mc, etc etc.
First question, how can I access the body parts for any animation? here's my code so far:
var WalkAnim:Class = SWFLoader.getClass('walk_mc'); //Using Greensock loader; but it's the same as using appDomain.getDefinition();
var walkAnim:MovieClip = new WalkAnim();
addChild(walkAnim);
Second question, adding walkAnim just creates an instance of the mc definition. How can I edit the definition in the library to do something like..
var Hat:Class = SWFLoader.getClass('accessory_hat_mc');
var hat = new Hat();
WalkAnim:addChild(Hat)//???
So that if I have multiple instances on stage, they'll all be updated. Thanks in advance for the help!
Basically, to access child elements, you use dot syntax. That would look something like this (last line):
var WalkAnim:Class = SWFLoader.getClass('walk_mc');
var walkAnim:WalkAnim = new WalkAnim(); // I have typed your var as WalkAnim, not MovieClip.
addChild(walkAnim);
walkAnim.head_mc.rotation += 5;
To answer your second question, you wont be able to edit the definition at runtime. You can add an item to each instance though:
var Hat:Class = SWFLoader.getClass('accessory_hat_mc');
var myHat:Hat = new Hat();
walkAnim.head_mc.addChild(myHat);

Get first frame of an flv video into a jpg image

I am creating an app with flex for recording a video and i want to capture the first image of the video recorded, by now i use the following function in order to capture the
first image but its rather slow
private function capture():void
{
var bitmapData:BitmapData = new BitmapData(videoDisplay.width, videoDisplay.height);
bitmapData.draw(videoDisplay);
var jpg:JPEGEncoder = new JPEGEncoder();
var ba:ByteArray = jpg.encode(bitmapData);
var base64_enc: Base64Encoder = new Base64Encoder();
base64_enc.encodeBytes(ba,0,ba.length);
imgEncoded = base64_enc.toString();
}
and i would like to get the first frame of the recorded video as a thumb after it is recorded
is there any idea on how to achieve that?
Thanks in advance!
LoaderMax videoloader does it for you, you can check out source codes.
http://www.greensock.com/loadermax/

Actionscript 3.0 Embed Font - Text Not Appearing

I am creating dynamic TextFields in actionscript 3.0. Like many others, my text disappears when I set .embedFonts = true;
ArialSlim is embedded and exported for actionscript. I have successfully tested with trace(Font.enumerateFonts());
Interestingly enough, when I comment out the embed line (as shown below), the font works properly.
Alternatively, .setTextFormat(); also works properly without the .embedFonts line.
So my questions is, why? Will I run into any issues in this case?
var divArray = new Array();
var x_Lbl_Array:Array = new Array();
var entries:int = 10;
var labelFormat:TextFormat = new TextFormat();
var arial:Font = new ArialSlim();
labelFormat.font = arial.fontName;
labelFormat.size = 10;
var xVar:int = 0;
for(var loop:int = 0; loop < entries; loop++){
x_Lbl_Array[loop] = new TextField();
//x_Lbl_Array[loop].embedFonts = true;
x_Lbl_Array[loop].antiAliasType = AntiAliasType.NORMAL;
x_Lbl_Array[loop].defaultTextFormat = labelFormat;
x_Lbl_Array[loop].x = xVar;
x_Lbl_Array[loop].y = 165;
x_Lbl_Array[loop].text = "test";
mc.addChild(x_Lbl_Array[loop]);
xVar++;
}
Edit:
I just ran this code from frame 1 with .embedFonts = true; and it worked...
Maybe I should mention that I'm having trouble running this code in a method inside an instantiated actionscript class. The class is located in an external .as file. Does this help answer my question?
I tried your code and it worked for me (with the embedded font)
check if you have all the characters included.
You can either set the character range, or create a textfield that has all the characters.
It turns out that I at some point clicked "TLF (DF4)" in the outline format options for my embedded font. When I corrected this, and chose "Classic (DF3)," it fixed my problem.
I guess what I find to be really weird is that the font was showing properly without .embedFonts being set to true
Thank you, Daniel. I appreciate the help.

flex: Create a screenshot of UI element

I want to make a screenshot of custom as3 ui element (which was extended from Canvas), and then upload the image on the server.
Could you help me with a small example of this?
I am trying to do the following:
// This is UI component extended from Canvas
var chessBoard:ChessBoard = new ChessBoard();
// I displayed pieces on the board, but didn't add it to the stage (I just need a
// a screenshot, not need them on the canvas)
chessBoard.displayPosition(DataStorage.getInstance().getStoredPosition(0));
var img:ImageSnapshot = ImageSnapshot.captureImage(chessBoard, 300, new PNGEncoder());
var obj:Object = new Object();
obj.complexity = complexity.value;
obj.img = img;
httpService.send(obj);
and getting this error:
ArgumentError: Error #2015: Invalid
BitmapData. at
flash.display::BitmapData() at
mx.graphics::ImageSnapshot$/captureBitmapData()[C:\autobuild\3.2.0\frameworks\projects\framework\src\mx\graphics\ImageSnapshot.as:186]
at
mx.graphics::ImageSnapshot$/captureImage()[C:\autobuild\3.2.0\frameworks\projects\framework\src\mx\graphics\ImageSnapshot.as:282]
at
ui::SendForm/send()[/Users/oleg/Documents/chess_problems/problemme/src/ui/SendForm.mxml:53]
at
> ui::SendForm/___SendForm_Button1_click()[/Users/oleg/Documents/chess_problems/problemme/src/ui/SendForm.mxml:16]
This error was caused by missing width and height of the Canvas. I set them and it helped to create bitmap data this way:
var bitmapData:BitmapData = new BitmapData(chessBoard.width,chessBoard.height);
var encoder:PNGEncoder = new PNGEncoder();
var data:ByteArray = encoder.encode(bitmapData);
I wonder how do I send the image to the server via httpService? Is there a way. Also is data = image file?
Here is a post on someone who has done this exact thing in AS3:
Dynamically Create an Image in Flash and Save it to the Desktop or Server
It is possible, and although I have never done it, this may be useful to you: http://www.flash-db.com/Tutorials/snapshot/