Images after resize not show - actionscript-3

This is part of my code, I'm trying to output images from XML, all things work except one, it's image resize. Maybe I'm doing something wrong, because I'm new in flash ac3 and it's hard for me to understand logic of Flash AC3.
When I launch my flash in "Addobe Flash Pro" I got resized images, but on website in flashplayer there is no images, imahes don't want to display:(
Link with flash - http://flash.leto-templates.com/flash.html
var my_thumb_width: Number;
var my_thumb_height: Number;
var img_height: Number;
var my_images: XMLList;
var my_total: Number;
var my_total_width: Number;
var image:Bitmap;
function processXML(e: Event): void {
var myXML: XML = new XML(e.target.data);
my_thumb_width = myXML.#width;
my_thumb_height = myXML.#height;
my_images = myXML.image;
my_total = my_images.length();
my_total_width = (my_thumb_width + 10) * (my_images.length()) - stage.stageWidth;
createContainer();
callThumbs();
if (my_images.length() > 6) {
myTimer.start();
}
}
////...
var thumb_loader = new Loader();
var box: Product_box = new Product_box();
thumb_loader.load(new URLRequest(thumb_url));
thumb_loader.contentLoaderInfo.parentSandboxBridge = new Object();
thumb_loader.contentLoaderInfo.parentSandboxBridge['url'] = thumb_url;
var handleComplete:Function = buildHandleComplete(i);
thumb_loader.contentLoaderInfo.addEventListener(Event.COMPLETE, handleComplete );
////...
function buildHandleComplete(i:int):Function {
return function(event:Event):void {
thumbLoaded(event, i);
};
}
function thumbLoaded(e: Event, i: Number): void {
image = new Bitmap(e.target.content.bitmapData);
image.width = my_thumb_width;
image.height = 140;
image.y = 47;
image.x = (my_thumb_width + 10) * i;
container_mc.addChild(image);
}

Related

Dynamic Gallery to show download and progress bar in as3

My flash project has specific task to show the dynamic gallery items based on an XML list and there is a download option available for each gallery item.
For this I made a movieclip (imageTile) with a Thumbnail, Title, ProgressBar & ProgressText as shown below.
I have two classes named Main.as and FileRef.as
Main.as
var tileMap:Dictionary = new Dictionary();
public var tile:ImageTile;
addChild(wall);
wallWidth = wall.width;
wallHeight = wall.height;
var columns:Number;
var my_x:Number;
var my_y:Number;
var my_thumb_width:Number;
var my_thumb_height:Number;
var images:XMLList;
var total:Number;
var swipe:Number = 0;
var myXMLLoader:URLLoader = new URLLoader();
myXMLLoader.load(new URLRequest("gallery.xml"));
myXMLLoader.addEventListener(Event.COMPLETE, processXML);
function processXML(e:Event):void {
myXML = new XML(e.target.data);
images = myXML.IMAGE;
total = images.length();
myXMLLoader.removeEventListener(Event.COMPLETE, processXML);
myXMLLoader = null;
var loader:Loader;
for (var i:uint = 0; i < total; i++) {
tile = new ImageTile();
wall.addChild(tile);
tile.x = i % 3 * 400 + 180;
tile.y = Math.floor(i / 3) * 650 + 250;
var imageName:String = images[i].#FULL;
path = images[i].#Path;
var title:String = images[i].#Title;
**var caption:TextField = tile.getChildByName("caption_tf") as TextField;**
caption.text = title;
tile.addEventListener(MouseEvent.CLICK, onTileClick(path));
loader = new Loader();
loader.load(new URLRequest("images/" + imageName));
tileMap[loader] = tile;
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onImageLoad);
}
//trace(myXML);
}
var tileMap:Dictionary = new Dictionary();
function onImageLoad(e:Event):void {
var loader:Loader = e.target.loader;
var tile:ImageTile = tileMap[loader] as ImageTile;
var image:Bitmap = loader.content as Bitmap;
image.x = -100;
image.y = -100;
image.width=366;
image.height=418;
var textField:DisplayObject = tile.getChildByName("caption_tf");
var textFieldDepth:int = tile.getChildIndex(textField);
tile.addChildAt(image, textFieldDepth);
tileMap[loader] = null;
image.smoothing = true;
}
function onTileClick(url:String):Function {
return function(me:MouseEvent):void {
path = url;
var download:FileRef = new FileRef();
download.downloadFile(path);
}
FileRef.as code
public var mc_loaded : MovieClip = Main.gameInstance.tile.getChildByName("mc_loaded") as MovieClip,
mc_progress : MovieClip = Main.gameInstance.tile.getChildByName("mc_progress") as MovieClip,
txt_prog : TextField = Main.gameInstance.tile.getChildByName("txt_prog") as TextField;
public function downloadFile(url:String) : void
{
/// Download the gallery item codes using url
}
public function progressHandler( event : ProgressEvent ) : void
{
//mc_loaded.scaleX = (event.bytesLoaded / event.bytesTotal) ;
}
public function completeHandler( event : Event ) : void
{
//reset progress bar after download is finished
mc_loaded.scaleX = 0; // I want to use the imageTile element
txt_prog.text = "download finished";
}
public function OnZipComplete(evt:flash.events.Event):void
{
txt_prog.text = "download finished";
}
The download works fine, but I can not get the progress bar and progress text for every tile created.
I got the answer
Remove onTileClick(url:String) function from Main.as
Main.as
function processXML(e:Event):void {
//// Codes /////
//// Codes /////
var download:FileRefTut = new FileRefTut();
tile.addEventListener(MouseEvent.CLICK, download.downloadFile(path));
}
FileRef.as
public var txt_prog:TextField, mc_loaded:MovieClip;
public function downloadFile(url:String):Function {
return function(me:MouseEvent):void {
var tile:ImageTile = me.currentTarget as ImageTile;
txt_prog = tile.getChildByName("txt_prog") as TextField;
mc_loaded = tile.getChildByName("mc_loaded") as MovieClip;
}
public function progressHandler( event : ProgressEvent ) : void
{
mc_loaded.scaleX = (event.bytesLoaded / event.bytesTotal) ;
}
public function completeHandler( event : Event ) : void
{
//reset progress bar after download is finished
mc_loaded.scaleX = 0; // I want to use the imageTile element
txt_prog.text = "download finished";
}
public function OnZipComplete(evt:flash.events.Event):void
{
txt_prog.text = "download finished";
}

Flash Webcam Capture

I am trying to build AS3 web audio/video capture. I have successfully tried to display the webcam stream with the help of this great tutorial. The next step is how do i capture the video when the user starts recording. Below is my code:
// video stuff
private var camW:int = 300;
private var camH:int = 300;
private var video:Video;
// block stuff
private var rows:int = 3;
private var cols:int = 3;
private var blockW:int = camW/cols;
private var blockH:int = camH/rows;
private var pointArray:Array = new Array();
public function Main():void {
// Checks if camera is installed
if (Camera.names.length > 0) {
trace("User has at least one camera installed.");
var camera:Camera = Camera.getCamera();
camera.setMode(camW, camH, 30);
camera.addEventListener(StatusEvent.STATUS, statusHandler);
video = new Video(camW, camH);
video.attachCamera(camera);
initBlocks();
addEventListener(Event.ENTER_FRAME, updateBlocks);
}else {
trace("User has no cameras installed.");
var text:TextField = new TextField();
text.text = "Device Not Found! Please check if the camera is installed.";
text.x = 20;
text.y = 20;
text.width = 500;
addChild(text);
}
function statusHandler(event:StatusEvent):void {
// This event gets dispatched when the user clicks the "Allow" or "Deny"
// button in the Flash Player Settings dialog box.
trace(event.code); // "Camera.Muted" or "Camera.Unmuted"
switch (event.code) {
case "Camera.Muted":
trace("User clicked Deny.");
var text:TextField = new TextField();
text.text = "Device Denied Permission! Please provide permission to record video!";
text.x = 20;
text.y = 20;
text.width = 500;
addChild(text);
break;
case "Camera.Unmuted":
trace("User clicked Accept.");
break;
}
}
}
private function initBlocks():void {
for (var r:int = 0; r < rows; r++) {
for (var c:int = 0; c < cols; c++) {
var newBlock:Sprite = new Sprite();
newBlock.name = "block" + r + c;
var p:Point = new Point(c * blockW, r * blockH);
newBlock.x = c * (blockW) + 20;
newBlock.y = r * (blockH) + 20;
pointArray[newBlock.name] = p;
var bmpd:BitmapData = new BitmapData(blockW, blockH);
var bmp:Bitmap = new Bitmap(bmpd);
bmp.name = "myBmp";
newBlock.addChild(bmp);
addChild(newBlock);
}
}
}
private function updateBlocks(e:Event):void {
var srcBmpd:BitmapData = new BitmapData(camW, camH);
srcBmpd.draw(video);
for (var r:int = 0; r < rows; r++) {
for (var c:int = 0; c < cols; c++) {
var b_mc:Sprite = this.getChildByName("block" + r + c) as Sprite;
var bmp:Bitmap = b_mc.getChildByName("myBmp") as Bitmap;
var p:Point = pointArray[b_mc.name];
bmp.bitmapData.copyPixels(srcBmpd, new Rectangle(p.x, p.y, blockW, blockH), new Point());
}
}
}
I am completely new to AS3, just started 2 days ago. What is the logic for audio/video capture in AS3? Any pointers/demo/tutorials/books appreciated
I had a very difficult time getting to work on the flash webcam recording. And as couple of posts here ( this one ) suggested to use some kind of flash servers like the Adobe Flash Server / Red5 Server. I did the same. I installed Red5 Server ( Please make sure you use the right JRE for the server to run smoothly after installation - as i had problems installing the demos ) and used Flex with FlashDevelop IDE ( Use Flex 3 Project to create a new project ). Here's below working sample code:
Main.mxml
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml">
<mx:Script source="Main.as"/>
<mx:VideoDisplay width="300" height="300" autoPlay="true" creationComplete="initCamera()" id="video"></mx:VideoDisplay>
<mx:Button label="Record" click="startRecording()" />
<mx:Button label="Stop" click="stopRecording()" />
</mx:Application>
Main.as
/**
* ...
* #author VishwaKumar
*/
import flash.events.NetStatusEvent;
import flash.media.Microphone;
import flash.net.NetConnection;
import flash.net.NetStream;
import mx.controls.Alert;
import flash.media.Camera;
import flash.media.Video;
import flash.display.Sprite;
public var camera:Camera;
public var nc:NetConnection;
public var ns:NetStream;
public var mic:Microphone;
public function initCamera():void {
var camW:int = 300;
var camH:int = 300;
if (Camera.names.length > 0) {
camera = Camera.getCamera();
camera.setMode(camW, camH, 30);
video.attachCamera(camera);
mic = Microphone.getMicrophone();
}else {
Alert.show("Device Not Found! Please check if the camera is installed.","Hardware Error!");
}
}
public function startRecording():void {
nc = new NetConnection();
nc.addEventListener(NetStatusEvent.NET_STATUS, netStatusHandler);
nc.connect("rtmp://127.0.0.1/oflaDemo");
}
public function netStatusHandler(event:NetStatusEvent):void {
trace(event.info.code);
switch(event.info.code) {
case "NetConnection.Connect.Success":
ns = new NetStream(nc);
ns.publish("test","record");
ns.attachAudio(mic);
ns.attachCamera(camera);
break;
default :
Alert.show("NetConnection.Connect.error");
break;
}
}
public function stopRecording():void {
ns.close();
}

Actionscript3 Clone and Drag object over other

I am building a simple flash game where user can drag objects from a tool-box and drag over to an area. On dragging the object, I create a clone of that object and place it over the area. Here is my code that works somewhat fine..
package {
imports...
public class DocumentClass extends MovieClip {
//variables...
var someArray:Array = new Array();
var totalObjs = 5;
var objOnStage:Array = new Array();
var ogx;
var ogy;
public function DocumentClass() {
// constructor code
stage.scaleMode = StageScaleMode.NO_SCALE;
stage.align = StageAlign.TOP_LEFT;
_width = stage.stageWidth;
_height = stage.stageHeight;
setGameObjects();
}//constructor close
//scaling Functions
public function scalingheight(clip:MovieClip, percent:Number){
var ratio = clip.width / clip.height;
clip.height = percent * _height;
clip.width = clip.height * ratio;
}
public function scalingwidth(clip:MovieClip, percent:Number){
var ratio = clip.height / clip.width;
clip.width = percent * _width;
clip.height = clip.width * ratio;
}
public function setGameObjects():void{
mc_toolbox = new mctoolbox;
addChild(mc_toolbox);
mc_toolbox.x = mc_toolbox.y = 0;
scalingwidth(mc_toolbox, 0.08);
mc_toolbox.height = _height;
mc_board = new mcboard;
addChild(mc_board);
mc_board.x = mc_toolbox.width;
mc_board.y = 0;
scalingwidth(mc_board, 0.75);
mc_board.height = _height;
mc_optbox = new mcoptbox;
addChild(mc_optbox);
scalingwidth(mc_optbox, 0.10);
mc_optbox.height = _height;
mc_optbox.x = _width - mc_optbox.width;
mc_optbox.y = 0;
setobjects();
}
public function setobjects():void{
for(var i = 1; i <= totalObjs; i++){
var className:String = "obj" + i;
var ClassReference:Class = getDefinitionByName(className) as Class;
var myInstance = new ClassReference;
addChild(myInstance);
scalingwidth(myInstance, 0.08);
someArray.push(myInstance);
myInstance.x = stage.stageWidth - 0.09 * _width;
myInstance.y = myInstance.height * (i-1);
}
for(var i = 1; i <= totalObjs; i++){
someArray[i-1].addEventListener(MouseEvent.MOUSE_DOWN, startMove);
someArray[i-1].addEventListener(MouseEvent.MOUSE_UP, stopMove);
}
}
function startMove(evt:MouseEvent):void {
//clone your movieclip here
ogx = evt.currentTarget.x;
ogy = evt.currentTarget.y;
evt.currentTarget.startDrag();
}
function stopMove(evt:MouseEvent):void {
var newobj = new evt.currentTarget.constructor;
addChild(newobj);
newobj.width = evt.currentTarget.width;
newobj.height = evt.currentTarget.height;
newobj.x = evt.currentTarget.x;
newobj.y = evt.currentTarget.y;
evt.currentTarget.x = ogx;
evt.currentTarget.y = ogy;
evt.currentTarget.stopDrag();
objOnStage.push(newobj);
}
}//class close
}//package close
My problem arises when I drag one object on the area and then I drag one more on the previous object. In such case the mouse up event is not called and hence the object movieclip does not clone itself. I think I am making some silly mistake, please guide.
I got this solved. The second movieclip came behind the first so I just brought it in front of all other movieclips while dragging using this
function startMove(evt:MouseEvent):void {
//clone your movieclip here
ogx = evt.currentTarget.x;
ogy = evt.currentTarget.y;
var myobj = evt.currentTarget as DisplayObject;
setChildIndex(myobj, numChildren - 1); // < The solution
evt.currentTarget.startDrag();
}
Try, removing MouseEvent.MOUSE_UP after dropping the object on the stage like so,
evt.currentTarget.removeEventListener(MouseEvent.MOUSE_UP, stopMove);

Child not added, when check index it shows -1

I am not sure why i addedChild but when i check it's index it still shows -1.
What i am trying to do is:
1.)Loop a URLRequest and load bitmap pictures.
2.)Put them in individual _contentHolder
3.)Put everything in a viewport
4.)Check the index of the image when its clicked
5.)Display just the picture that is clicked(With a black background) (Picture viewer)
6.)When the picture or background is clicked again, it closes the "picture viewer"(The single picture with black background), and just display the list from before.
At the moment i can upload the pictures in a loop, then add them into a viewport, but i can not managed to get the index of the image and reload it.
Thanks for your time!
Code:
public var _contentHolder:Sprite = new Sprite;
public var _contentHolder1:Sprite;
public var loadedArray:Array = new Array;
public var blackBox:Sprite = new Sprite();
private var somedata:Array;
protected var Holder:Listing9 = new Listing9;
public var viewport:Viewport = new Viewport();
public var scroller:TouchScroller = new TouchScroller();
var my_url:Array = somedata;
function loadImage():void
{
somedata = SearchVectorTest.lists;
for (var i:int = 5; i < somedata.length; i++)
{
if (somedata[i])
{
var loader:Loader = new Loader();
loader.load(new URLRequest("http://www.rentaid.info/rent/" + somedata[i]));
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onImageLoaded);
}
}
}
function onImageLoaded(e:Event):void
{
loadedArray.push(e.currentTarget.loader.content as Bitmap);
for (var i:int = 0; i < loadedArray.length; i++)
{
var currentY1:int = 200;
e.currentTarget.loader.content.height = 200;
e.currentTarget.loader.content.y += currentY1;
currentY1 += e.currentTarget.loader.content.height + 300;
_contentHolder.mouseChildren = false; // ignore children mouseEvents
_contentHolder.mouseEnabled = true; // enable mouse on the object - normally set to true by default
_contentHolder.useHandCursor = true; // add hand cursor on mouse over
_contentHolder.buttonMode = true;
_contentHolder.addChild(loadedArray[i]);
}
var viewport:Viewport = new Viewport();
viewport.y = 0;
viewport.addChild(_contentHolder);
var scroller:TouchScroller = new TouchScroller();
scroller.width = 300;
scroller.height = 265;
scroller.x = 10;
scroller.y = 100;
scroller.viewport = viewport;
addChild(scroller);
_contentHolder.addEventListener(MouseEvent.CLICK, gotoscene);
}
loadImage();
public function gotoscene(e:MouseEvent):void
{
var searchString = loadedArray;
var index:Number;
index = searchString.indexOf(e.target);
trace(index);
trace(_contentHolder);
trace(_contentHolder.parent);
blackBox.graphics.beginFill(0x000000);
blackBox.graphics.drawRect(-1, -1, stage.width, stage.height);
blackBox.alpha = 0.7;
addChild(blackBox);
Holder.height = 300;
Holder.width = stage.width;
Holder.x = 0;
Holder.y = 100;
trace(blackBox);
trace(blackBox.parent);
addChild(Holder);
}
function gotoscene1(e:MouseEvent):void
{
removeChild(Holder);
removeChild(blackBox);
}
The indexOf you are using strict equality (===). e.target is of type Sprite and your array is filled with Bitmaps.
The strict equality will fail because the objects are not of the same type.
You need to use items of the same type to make the comparison succeed.

How can I access the width of a JPG loaded inside a movieclip in AS3?

i'm trying to reproduce the bookshelf similar the one in this site: http://www.callis.com.br/
The books are PNG files that comes in several different sizes.
What I'm trying to accomplish is, after loading in several different movieclips (book0, book1, book2, ...) read their widths, calculate the gap necessary to push it, and then arrange the x value of each book.
Every time I try to access the width value, it says it's 0.
Here's a portion of my code, that's trying to do the trick.
Thanks in advance.
for (j=0; j < dados.livro.length(); j++){
if(j<8){
targetLivro = MovieClip(MovieClip(root).livros.getChildByName("livro"+(j)));
}else{
targetLivro = MovieClip(MovieClip(root).getChildByName("livro"+(j)));
}
preparaLivro(targetLivro,dados.livro[j].imagem.text(),dados.livro[j].link.text());
//larguraTotalLivrosSup += targetLivro.width;
//trace(larguraTotalLivrosSup);
trace("livro"+j+": ");
trace("título:"+dados.livro[j].titulo.text());
trace("imagem:"+dados.livro[j].imagem.text());
trace("link:"+dados.livro[j].link.text());
}
//organiza a primeira estante
//
/*
for (i=0; i<largurasArray.length; i++){
trace(largurasArray[i]);
}*/
var offset:Number = 20;
for (j=0; j<livros.numChildren; j++){
//targetLivro = MovieClip(livros).getChildByName("livro"+(j));
trace(livros.getChildAt(j).width);
}
function preparaLivro(livro:MovieClip, capa:String, url:String){
var capaLoader:Loader = new Loader();
var capaImageRequest:URLRequest = new URLRequest(capa);
var capaBitmapData:BitmapData;
var capaBitmap:Bitmap;
var capaLargura:Number = 0;
capaLoader.load(capaImageRequest);
capaLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, function(e:Event):void{
capaBitmapData = new BitmapData(e.target.content.width, e.target.content.height);
capaBitmapData.draw(capaLoader);
capaBitmap = new Bitmap;
capaBitmap.bitmapData = capaBitmapData;
livro.y -= e.target.content.height;
trace(e.target.content.width, e.target.content.height);
livro.buttonMode = true;
livro.addChild(capaBitmap);
livro.addEventListener(MouseEvent.CLICK, function(e:MouseEvent):void{
navigateToURL(new URLRequest(url), "_blank");
});
//trace(e.target.content.width);
});
}
Instantiate bitmap from LoaderInfo(event.target).content like:
var loader:Loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onComplete);
loader.load(new URLRequest("http://www.google.com/images/srpr/logo4w.png"));
function onComplete(event:Event):void
{
var bitmapData:BitmapData =
Bitmap(LoaderInfo(event.target).content).bitmapData;
}
Tracing bitmapData.width and bitmapData.height outputs:
width: 550, height: 190
Otherwise, if you want width / height from the loader, access loader's contentLoaderInfo:
var width:Number = loader.contentLoaderInfo.width;
var height:Number = loader.contentLoaderInfo.height;