as3 Progress Bar with urlstream - actionscript-3

This progress bar is not working. Any idea why?
import flash.filesystem.*;
import flash.display.*;
var byteArray:ByteArray = new ByteArray();//Bytes from the URLStream will go here
var loader:Loader = new Loader();
var urlString:String = "http://massmediamail.com/testing/Rosary.zip";
var urlReq:URLRequest = new URLRequest(urlString);
var urlStream:URLStream = new URLStream();
var fileData:ByteArray = new ByteArray();
urlStream.addEventListener(Event.COMPLETE, loaded);
urlStream.load(urlReq);
function loaded(event:Event):void
{
urlStream.readBytes(fileData, 0, urlStream.bytesAvailable);
writeAirFile();
}
function writeAirFile():void
{
var file:File = File.applicationStorageDirectory.resolvePath("000Zip/Rosary.zip");
trace(File.applicationStorageDirectory.nativePath);
var fileStream:FileStream = new FileStream();
fileStream.open(file, FileMode.WRITE);
fileStream.writeBytes(fileData, 0, fileData.length);
fileStream.close();
trace("The file is written.");
done.text = "Yahoooo!";
more.text = "Now wait for the \"yippie!\"";
//TotalLoaded.text = "Downloaded Zip";
fileStream.addEventListener(ProgressEvent.PROGRESS, onStreamProgress, false, 0, true);
function onStreamProgress(event:ProgressEvent):void
{
TotalBytes.text = "" + event.bytesTotal + "";
bb.text = "" + event.bytesLoaded + "";
}
}

Attach it to the URL Stream, not the FileStream

Related

How do I create dynamically text in a movie clip [AS3]?

I want click on button not text
btn_start.t1.text = "hello world" // not working
Rough code... You will have to work on it but it seems easy.
You just have to set your text for each states of the SimpleButton for each states when you create it.
That's all.
Now you may embed fonts, change the style of your TextField...
var btn_start:SimpleButton = new SimpleButton();
addChild(btn_start);
btn_start.upState = drawUpstate(0xcc5500,"upstate");
btn_start.downState = drawDownstate(0x444444,"downstate");
btn_start.hitTestState = drawDownHitTeststate();
btn_start.overState = drawOverState(0x888888,"overtstate");
addChild(btn_start);
placeButton(btn_start,50,50);
function drawUpstate(color:int, btnLabel:String):Sprite {
var btn_color:uint = color;
var mc:Sprite = new Sprite();
var textLabel:Sprite = new Sprite();
var tf : TextField = new TextField();
tf.textColor = 0xffffff;
tf.text = btnLabel;
mc.addChild(tf);
var g:Graphics = mc.graphics;
drawButton(g,btn_color);
return mc;
}
function drawDownstate(color:int, btnLabel:String):Sprite {
var btn_color:uint = color;
var mc:Sprite = new Sprite();
var textLabel:Sprite = new Sprite();
var tf : TextField = new TextField();
tf.textColor = 0xffffff;
tf.text = btnLabel;
mc.addChild(tf);
var g:Graphics = mc.graphics;
drawButton(g,btn_color);
return mc;
};
function drawDownHitTeststate():Sprite {
var btn_color:uint = 0x0000000;
var mc:Sprite = new Sprite();
var g:Graphics = mc.graphics;
drawButton(g,btn_color);
return mc;
};
function drawOverState(color:int, btnLabel:String):Sprite {
var btn_color:uint = color;
var mc:Sprite = new Sprite();
var textLabel:Sprite = new Sprite();
var tf : TextField = new TextField();
tf.textColor = 0xffffff;
tf.text = btnLabel;
mc.addChild(tf);
var g:Graphics = mc.graphics;
drawButton(g,btn_color);
return mc;
};
function drawButton(g:Graphics,color:uint) {
g.beginFill(color,1);
g.drawRect(0,0,100,20);
g.endFill();
};
function placeButton(btn:SimpleButton,x:uint,y:uint):void {
btn.x = x;
btn.y = y;
};
Best regards.
Nicolas

AS3/AIR: IOStream Error when trying to upload files

I'm making an AIR app in which the user drags and drops image files onto the stage, the app then uploads those dropped images to the site imgur.com, and then returns with links to the images (in XML format).
I am having two issues:
the program works successfully with a very small file, however anything larger (1mb or more), I get an IOStream error 2023. Imgur supports uploads of up to 10mb so I know it must be something on my end.
I would like to be able to drop multiple image files at once into the program, and have it upload all of them (either synchronously or asynchronously, it does not matter). Right now the program gives errors when I attempt to do this (something along the lines of the byteArray having to be greater than 0).
Below is the code in question which is giving me issues. (thank you for your help in advanced)
private function doDragDrop(e:NativeDragEvent):void
{
trace("doDragDrop() called.");
var dropFiles:Array = e.clipboard.getData(ClipboardFormats.FILE_LIST_FORMAT) as Array;
for each (var file:File in dropFiles)
{
switch (file.extension.toLowerCase())
{
case "jpg" :
case "jpeg" :
case "gif" :
case "png" :
case "apng" :
case "tiff" :
case "bmp" :
case "pdf" :
case "xcf" :
trace("file Extension Check = passed");
addImage(file.nativePath);
break;
//error handling for non-supported filetypes
default :
trace("ERROR: Unsupported File Type Detected!");
}
}
}
private function addImage(nativePath:String):void
{
trace("addImage() called.");
trace("NativePath is: " + nativePath);
//var file:File = new File(nativePath);
var file:File = File.desktopDirectory.resolvePath(nativePath);
var ba:ByteArray = new ByteArray();
var stream:FileStream = new FileStream();
stream.open(file, FileMode.READ);
stream.readBytes(ba);
stream.close();
var loader:Loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, fileLoaded);
loader.loadBytes(ba);
}
private function fileLoaded(e:Event):void
{
trace("fileLoaded() called.");
var bitmap:Bitmap = Bitmap(e.target.content);
var bitmapData:BitmapData = bitmap.bitmapData;
var png:ByteArray = PNGEncoder.encode(bitmapData);
urlLoader = new URLLoader();
urlLoader.dataFormat = URLLoaderDataFormat.VARIABLES;
urlLoader.addEventListener(Event.COMPLETE, onCookieSent);
urlLoader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, onSecurityError);
urlLoader.addEventListener(IOErrorEvent.IO_ERROR, onIOError);
var vars:String = "?key=" + API_KEY + "&name=name&title=title";
var request:URLRequest = new URLRequest(UPLOAD_URL + vars);
request.contentType = "application/octet-stream";
request.method = URLRequestMethod.POST;
request.data = png;
urlLoader.load(request);
}
private function onCookieSent(e:Event):void
{
trace("onCookieSent() called.");
var res:XML = new XML(unescape(urlLoader.data));
var resultsList:XMLList = res.links;
trace(resultsList);
}
private function onIOError(e:IOErrorEvent):void
{
trace("ioErrorHandler: " + e);
TweenLite.to(tv_mc, 2, {alpha:0.5, scaleX:1.0, scaleY:1.0, ease:Elastic.easeOut});
// Handle error
}
private function onSecurityError(e:SecurityErrorEvent):void
{
trace("securityErrorHandler: " + e);
TweenLite.to(tv_mc, 2, {alpha:0.5, scaleX:1.0, scaleY:1.0, ease:Elastic.easeOut});
// handle error
}
//When the dragged object leaves the drop point, do this
private function doDragExit(e:NativeDragEvent):void
{
trace("doDragExit() called.");
TweenLite.to(tv_mc, 2, {alpha:0.5, scaleX:1.0, scaleY:1.0, ease:Elastic.easeOut});
}
after doing this:
var ba:ByteArray = new ByteArray();
var stream:FileStream = new FileStream();
stream.open(file, FileMode.READ);
stream.readBytes(ba);
stream.close();
you already have a ByteArray of the file/image. can't you just send this one...
private function addImage(nativePath:String):void
{
trace("addImage() called.");
trace("NativePath is: " + nativePath);
//var file:File = new File(nativePath);
var file:File = File.desktopDirectory.resolvePath(nativePath);
var ba:ByteArray = new ByteArray();
var stream:FileStream = new FileStream();
stream.open(file, FileMode.READ);
stream.readBytes(ba);
stream.close();
urlLoader = new URLLoader();
urlLoader.dataFormat = URLLoaderDataFormat.VARIABLES;
urlLoader.addEventListener(Event.COMPLETE, onCookieSent);
urlLoader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, onSecurityError);
urlLoader.addEventListener(IOErrorEvent.IO_ERROR, onIOError);
var vars:String = "?key=" + API_KEY + "&name=name&title=title";
var request:URLRequest = new URLRequest(UPLOAD_URL + vars);
request.contentType = "application/octet-stream";
request.method = URLRequestMethod.POST;
request.data = ba;
urlLoader.load(request);
}

Actionscript 3 - DataGridColumn and having a specific column automatically sorted

I am using the DataGrid component in Flash, that is loaded with data via a external XML file. I have a column, A (Serial), which once loaded, I'd like for the information to be sorted Ascendingly, automatically. Does anyone have any idea on how to do this?
Here is my code:
import fl.controls.DataGrid;
import fl.data.DataProvider;
import fl.controls.dataGridClasses.DataGridColumn;
import fl.controls.ScrollPolicy;
import fl.events.DataGridEvent;
var dp:DataProvider;
var A:DataGridColumn = new DataGridColumn("Serial");
A.headerText = "Serial No.";
A.width = 100;
A.resizable = false;
var B:DataGridColumn = new DataGridColumn("Mold");
B.headerText = "Mold No.";
B.width = 150;
B.resizable = false;
var C:DataGridColumn = new DataGridColumn("Type");
C.headerText = "Grid Type: ";
C.width = 350;
C.resizable = false;
var myDataGrid:DataGrid = new DataGrid();
myDataGrid.addColumn(A);
myDataGrid.addColumn(B);
myDataGrid.addColumn(C);
myDataGrid.verticalScrollPolicy = ScrollPolicy.ON;
myDataGrid.setSize(600, 800);
myDataGrid.move(0, 0);
myDataGrid.addEventListener(DataGridEvent.HEADER_RELEASE, headerReleaseHandler);
addChild(myDataGrid);
var url:String = "xml/TEST.xml";
var req:URLRequest = new URLRequest(url);
var uLdr:URLLoader = new URLLoader();
uLdr.addEventListener(Event.COMPLETE, completeHandler);
uLdr.load(req);
function completeHandler(event:Event):void {
var ldr:URLLoader = event.currentTarget as URLLoader;
var xmlDP:XML = new XML(ldr.data);
dp = new DataProvider(xmlDP);
myDataGrid.dataProvider = dp;
}
function headerReleaseHandler(event:DataGridEvent):void {
var dg:DataGrid = event.currentTarget as DataGrid;
trace("column: " + String(event.dataField));
trace("descending: " + String(dg.sortDescending));
}
Thanx
Try the sortItemsOn() method on the DataGrid.
See http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/fl/controls/SelectableList.html#sortItemsOn() for an example how to use it.

AS3 - Remove Child Problem

I'm using the following code to display an advertisement in a mobile game that I'm working on. It works fine but I don't know what to put in the clickStart function to remove the advertisement from the stage before the game plays. I've been playing around with removeChild but can't seem to get it to work.
stop();
startButton.addEventListener(MouseEvent.CLICK,clickStart);
function clickStart(event:MouseEvent) {
gotoAndStop("play");
}
var request:URLRequest = new URLRequest("http://soma.smaato.com/oapi/reqAd.jsp");
var variables:URLVariables = new URLVariables();
variables.adspace = "0";
variables.pub = "0";
variables.devip = "127.0.0.1";
variables.format = "IMG";
variables.adcount = "1";
variables.response = "XML";
request.data = variables;
var loader:URLLoader = new URLLoader();
loader.addEventListener(Event.COMPLETE, onComplete);
loader.load(request);
function onComplete(e:Event):void
{
var data:XML = new XML(loader.data as String);
var status:String = data.*::status.toString();
if(status == "success")
{
var ad:XMLList = data.*::ads.*::ad;
var link:String = ad.*::link.toString();
var l:Loader = new Loader();
l.load(new URLRequest(link));
addChild(l);
l.x = 135;
l.y = 265;
var clickurl:String = ad.*::action.#target.toString();
l.addEventListener(MouseEvent.CLICK, onAdClick);
function onAdClick(e:MouseEvent):void
{
var request:URLRequest = new URLRequest(clickurl);
navigateToURL(request);
}
}
}
You need to move the declaration of the Loader outside of the onComplete function, since the variable runs out of scope.
var l:Loader = new Loader();
You could try placing it on the same scope as these:
var request:URLRequest = new URLRequest("http://soma.smaato.com/oapi/reqAd.jsp");
var variables:URLVariables = new URLVariables();
then you should be able to update your clickStart function to something like this:
function clickStart(event:MouseEvent) {
removeChild(l);
gotoAndStop("play");
}

AS3 - positon advertisment that is dynamically loaded

I'm using the code below to place an ad inside a mobile game that I'm building. My problem is that the ad appears in the top left corner of the stage and I need to be able to move it centered near the bottom. I got this code from a book and I understand about 70% of what it's doing. Thanks for any direction you can offer.
Rich
// Smaato Advertising Code for Start Screen
var request:URLRequest = new URLRequest("http://soma.smaato.com/oapi/reqAd.jsp");
var variables:URLVariables = new URLVariables();
variables.adspace = "0";
variables.pub = "0";
variables.devip = "127.0.0.1";
variables.format = "IMG";
variables.adcount = "1";
variables.response = "XML";
request.data = variables;
var loader:URLLoader = new URLLoader();
loader.addEventListener(Event.COMPLETE, onComplete);
loader.load(request);
function onComplete(e:Event):void
{
var data:XML = new XML(loader.data as String);
var status:String = data.*::status.toString();
if(status == "success")
{
var ad:XMLList = data.*::ads.*::ad;
var link:String = ad.*::link.toString();
var l:Loader = new Loader();
l.load(new URLRequest(link));
addChild(l);
var clickurl:String = ad.*::action.#target.toString();
l.addEventListener(MouseEvent.CLICK, onAdClick);
function onAdClick(e:MouseEvent):void
{
var request:URLRequest = new URLRequest(clickurl);
navigateToURL(request);
}
}
}
The problem is that you are never actually positioning the image, so the default is [0,0], the top-left corner of the screen.
var request:URLRequest = new URLRequest("http://soma.smaato.com/oapi/reqAd.jsp");
var variables:URLVariables = new URLVariables();
variables.adspace = "0";
variables.pub = "0";
variables.devip = "127.0.0.1";
variables.format = "IMG";
variables.adcount = "1";
variables.response = "XML";
request.data = variables;
var loader:URLLoader = new URLLoader();
loader.addEventListener(Event.COMPLETE, onComplete);
loader.load(request);
function onComplete(e:Event):void
{
var data:XML = new XML(loader.data as String);
var status:String = data.*::status.toString();
if(status == "success")
{
var ad:XMLList = data.*::ads.*::ad;
var link:String = ad.*::link.toString();
var l:Loader = new Loader();
l.load(new URLRequest(link));
l.contentLoaderInfo.addEventListener(Event.COMPLETE, onAdLoaded);
function onAdLoaded(e:Event):void
{
var ad:DisplayObject = e.target.content;
addChild(ad);
ad.x = (ad.stage.stageWidth - ad.width) / 2;
ad.y = (ad.stage.stageHeight - ad.height) / 2 + 50;
}
var clickurl:String = ad.*::action.#target.toString();
l.addEventListener(MouseEvent.CLICK, onAdClick);
function onAdClick(e:MouseEvent):void
{
var request:URLRequest = new URLRequest(clickurl);
navigateToURL(request);
}
}
}
That will center the loaded file. The 50 number is because if you don't want it to be in the exact center, you can use the number to adjust the y-value accordingly.
By the way, I recommend you to grab another book, becase the code you are using seems a bit old and have poor performance.
If you have any other issue regarding this, comment it and I can expand my answer.