AS3 event listeners inside loop - actionscript-3

I'm having a bit of a hard time figuring this one out.
I'm creating an Air/Flash app that uploads files from a local directory to the server.(without user interaction).
I'm creating an Array of the files found in the folder and can upload them fine without any issues.
However, I can not keep track of the event listeners; meaning, the for loop obviously completes before the event listeners for each upload.
Is there a way to keep track of the event listeners and find out when the last one finished?
Any help is greatly appreciated...
Here's my code:
public var UPLOAD_URL:String = "http://myhost/dev/uptest.php";
upbtn.addEventListener(MouseEvent.CLICK, uploadme);
public function uploadme(event:MouseEvent):void
{
preloader.visible = true;
var desktop:File = File.applicationStorageDirectory.resolvePath("photo");
var files:Array = desktop.getDirectoryListing();
for (var i:uint = 0; i < files.length; i++)
{
var date:Date = new Date(); // get new date
var mytime:Number = Math.round(date.valueOf()/1000);
var ur:URLRequest = new URLRequest();
var uv:URLVariables = new URLVariables();
uv.filename = files[i].name;
ur.data = uv;
ur.method = URLRequestMethod.POST;
ur.url = UPLOAD_URL;
files[i].addEventListener(ProgressEvent.PROGRESS, updateProgress);
files[i].addEventListener(DataEvent.UPLOAD_COMPLETE_DATA, doneUpload);
files[i].addEventListener(IOErrorEvent.IO_ERROR, fileError);
files[i].upload(ur);
//trace(i)
function updateProgress(event:ProgressEvent):void
{
//wait preloader.visible = true;
//trace(event.currentTarget.name);
}
function doneUpload(event:DataEvent):void
{
//var loader2: URLLoader = URLLoader(event.target);
//trace(i);
trace(event.data);
//preloader.visible = false;
}
function fileError(event:IOErrorEvent):void
{
trace("error");
}
}
}

One approach is to keep track of how many files you're uploading and only act in the doneUpload listener once all files have finished:
private var numFiles:int = 0;
public function uploadme(event:MouseEvent):void
{
...
var files:Array = desktop.getDirectoryListing();
numFiles = files.length;
for (var i:uint = 0; i < files.length; i++)
...
}
function doneUpload(event:DataEvent):void
{
//var loader2: URLLoader = URLLoader(event.target);
//trace(i);
trace(event.data);
numFiles--;
if (numFiles == 0) {
trace("All the files are done uploading.");
}
//preloader.visible = false;
}

Related

AS3 Play & Pause Current (loaded) MP3 (Flash CC)

I have 8 external mp3s that are loaded and will be played sequencially. I cannot figure how to pause the mp3 currently playing and start playing from the point it was paused. I did have code that would pause it but when clicking the play button, it would play the first mp3 from the beginning.
var s1:Sound = new Sound(new URLRequest("Audio Files/1.mp3"));
var s2:Sound = new Sound(new URLRequest("Audio Files/2.mp3"));
var s3:Sound = new Sound(new URLRequest("Audio Files/3.mp3"));
var s4:Sound = new Sound(new URLRequest("Audio Files/4.mp3"));
var s5:Sound = new Sound(new URLRequest("Audio Files/5.mp3"));
var s6:Sound = new Sound(new URLRequest("Audio Files/6.mp3"));
var s7:Sound = new Sound(new URLRequest("Audio Files/7.mp3"));
var s8:Sound = new Sound(new URLRequest("Audio Files/8.mp3"));
s1.addEventListener(Event.COMPLETE, doLoadComplete);
s2.addEventListener(Event.COMPLETE, doLoadComplete);
s3.addEventListener(Event.COMPLETE, doLoadComplete);
s4.addEventListener(Event.COMPLETE, doLoadComplete);
s5.addEventListener(Event.COMPLETE, doLoadComplete);
s6.addEventListener(Event.COMPLETE, doLoadComplete);
s7.addEventListener(Event.COMPLETE, doLoadComplete);
s8.addEventListener(Event.COMPLETE, doLoadComplete);
var channel:SoundChannel = new SoundChannel();
channel = s1.play();
channel.addEventListener(Event.SOUND_COMPLETE, doSoundComplete);
function doLoadComplete($evt:Event):void
{
trace("Song loaded.");
}
function doSoundComplete($evt:Event):void
{
trace("1 done.");
channel = s2.play();
channel.addEventListener(Event.SOUND_COMPLETE, doSoundComplete2)
}
function doSoundComplete2($evt:Event):void
{
trace("2 done.");
channel = s3.play();
channel.addEventListener(Event.SOUND_COMPLETE, doSoundComplete3);
}`
Here is what I have so far:
This loads the mp3s and plays them. The pause btn works but the play button to resume the audio gives me an error : ReferenceError: Error #1069: Property 0 not found on flash.media.Sound and there is no default value. at mp3sequence_fla::MainTimeline/playSound()
My guess is that the value for the current position or last position is incorrect.
var myArray:Array=[0,1,2,3,4,5,6,7];
var i:uint=1;
var req:URLRequest = new URLRequest("mp3/"+myArray[i]+".mp3");
var VSound:Sound = new Sound();
var channel:SoundChannel = new SoundChannel();
var lastPosition:Number = 0; //last position of the sound
var curSoundIndex:int = 0; //var to store the current sound that is playing
VSound.load(req);
channel = VSound.play();
function playSound(e:Event = null) {
//if no sound channel, load the current sound into it
channel = VSound[curSoundIndex].play(lastPosition);
channel.addEventListener(Event.SOUND_COMPLETE, doSoundComplete, false, 0, true);
lastPosition = channel.position;
}
function pauseSound(e:Event = null) {
if (channel) {
lastPosition = channel.position;
channel.stop();
}
}
function doSoundComplete($evt:Event):void {
curSoundIndex++;
if (curSoundIndex >= VSound.length) curSoundIndex = 0;
}
play_btn.addEventListener(MouseEvent.CLICK, playSound);
pause_btn.addEventListener(MouseEvent.CLICK, pauseSound);
To pause a sound you can store it's position when you pause it, and use that variable to replay from that position.
var s:Sound = new Sound(new URLRequest("Audio Files/1.mp3"));
var channel:SoundChannel = new SoundChannel();
channel = s.play();
var pausePosition:int;
function pause():void {
soundPosition = channel.position;
channel.stop();
}
function resume():void {
channel = s.play(soundPosition);
}
You can store the position property of the corresponding SoundChannel. I've added some code to make the whole thing less redundant
var curSoundIndex:int = 0; //var to store the current sound that is playing
var lastPosition:Number = 0; //last position of the sound
var soundChannel:SoundChannel;
var sounds:Vector.<Sound> = new Vector.<Sound>(); //array of all sounds
loadNextSound();
function loadNextSound(e:Event = null):void {
//check if all sounds are loaded
if (sounds.length >= 8) {
curSoundIndex = 0; //select first sound
resume(); //start playing
return;
}
//if not, load the next sound and add it to the array/vector
var sound:Sound = new Sound(new URLRequest("Audio Files/" + (sounds.length + 1) + ".mp3"));
sounds.push(sound);
if(sound.bytesLoaded < sound.bytesTotal){ //check if already loaded
sound.addEventListener(Event.COMPLETE, loadNextSound);
}else{
loadNextSound();
}
}
function pause(e:Event = null) {
if (soundChannel) {
lastPosition = soundChannel.position;
soundChannel.stop();
}
}
function resume(e:Event = null) {
//if no sound channel, load the current sound into it
soundChannel = sounds[curSoundIndex].play(lastPosition);
soundChannel.addEventListener(Event.SOUND_COMPLETE, doSoundComplete, false, 0, true); //use weak listener to avoid memory leaks
lastPosition = 0;
}
}
function doSoundComplete($evt:Event):void {
curSoundIndex++;
if (curSoundIndex >= sounds.length) curSoundIndex = 0;
}

Match two strings from a text file and user input? (AS3)

I was able to load a text file in a Flash file, but I am unable to match two strings from a text file and the user input.
The purpose of this AS3 code: to match the text file and user input, and if it matches, the score will increase by 1. Else, the score will increase by 0.
Here is my code:
var uScore :Number = 0;
stop();
var textLoader:URLLoader = new URLLoader();
var textURLRequest:URLRequest = new URLRequest("q1.txt");
textLoader.addEventListener(Event.COMPLETE, completeHandler);
function completeHandler(event:Event):void
{
var textData:String = new String(textLoader.data);
dy1.text = textData;
}
textLoader.load(textURLRequest);
function goURL(event:MouseEvent):void {
var textLoader2:URLLoader = new URLLoader();
var textURLRequest2:URLRequest = new URLRequest("answer1.txt");
var textData2:String = new String(textLoader2.data);
var name1 = trace(textData2);
textLoader2.load(textURLRequest2);
var myURL = url1.text;
if(myURL == name1){
uScore += 1;
uScoreURL.text = uScore+"";
nextFrame();
}
else{
uScore+=0;
uScoreURL.text = uScore+"";
nextFrame();
}
}
trace(uScore);
You code has a strange assignment:
var name1 = trace(textData2);
replace it with
var name1 =textData2;
it should work then if there isn't a bug in some other place.
And you don't need to uScore+=0;. Just delete it.
I checked out your code, you were doing a few things out of order - here is the revised code that should get you where you need to be
var uScore :Number = 0;
stop();
var textLoader:URLLoader = new URLLoader();
var textURLRequest:URLRequest = new URLRequest("q1.txt");
textLoader.addEventListener(Event.COMPLETE, completeHandler);
function completeHandler(event:Event):void
{
var textData:String = new String(textLoader.data);
dy1.text = textData;
}
textLoader.load(textURLRequest);
btn.addEventListener(MouseEvent.CLICK,getNumber);
var textLoader2:URLLoader = new URLLoader();
textLoader2.addEventListener(Event.COMPLETE, completeHandler2);
function completeHandler2(event:Event):void
{
var textData2:String = new String(textLoader2.data);
var name1 = textData2;
trace(name1);
var myURL = url1.text;
if(myURL == name1){
uScore += 1;
uScoreURL.text = uScore+"";
nextFrame();
}
else{
uScore+=0;
uScoreURL.text = uScore+"";
nextFrame();
}
}
function getNumber(event:MouseEvent){
var textURLRequest2:URLRequest = new URLRequest("answer1.txt");
textLoader2.load(textURLRequest2);
}
trace(uScore);
The only thing that I really added that you didn't have is a button with the variable name btn to check the answer - you can rework that to however you want to check for the answer.

as3 air check if file is loaded

How can I run code then the array is ready?
When I run the following code I get an error saying:
TypeError: Error #1010: A term is undefined and has no properties.
import flash.filesystem.File;
var desktop:File = File.applicationDirectory.resolvePath("sounds/drums");
var sounds:Array = desktop.getDirectoryListing();
for (var i:uint = 0; i < sounds.length; i++)
{
trace(sounds[i].nativePath); // gets the path of the files
trace(sounds[i].name);// gets the name
}
var mySound:Sound = new Sound();
var myChannel:SoundChannel = new SoundChannel();
mySound.load(new URLRequest("sounds/drums/"+sounds[i].name+""));
myChannel = mySound.play();
I usually use something like the below, each time a sound is loaded it is stored and a counter is incremented, once all sounds are loaded you can dispatch an event or start playing any of the sounds stored in loadedSounds.
var sounds:Array = desktop.getDirectoryListing();
var loadedSounds:Object = {};
var soundsLoaded:int = 0;
for (var i:uint = 0; i < sounds.length; i++)
{
var mySound:Sound = new Sound();
mySound.addEventListener(Event.COMPLETE, onSoundLoaded);
mySound.load(new URLRequest("sounds/drums/"+sounds[i].name));
}
private function onSoundLoaded(e:Event):void
{
var loadedSound = e.target as Sound;
// just get the file name without path and use it as key
var lastIndex:int = loadedSound.url.lastIndexOf("/");
var key:String = loadedSound.url.substr(lastIndex+1);
// store sounds for later reference
loadedSounds[key] = loadedSound ;
soundsLoaded++;
if (soundsLoaded == sounds.length)
{
//all sounds loaded, can start playing
}
}

AS3 Adobe AIR: Return xml string

I'm working in Flash CS6 with Adobe AIR 3.3 (for iOS) and having trouble returning an XML string to a textField.
It is tracing the proper information, and I've tried a few ways to return the trace but can't seem to quite get it... Here is my most recent try. Any suggestions? Thanks in advance.
var myLoader:URLLoader = new URLLoader();
myLoader.load(new URLRequest("http://www.someURL.php"));
//php file that echos xml
myLoader.addEventListener(Event.COMPLETE, init);
var fadedText:TextField;
var touchList:TouchList;
var textOutput:TextField;
var animateLeft:Tween;
var listArray:Array;
var item:TouchListItemRenderer;
var theXML:XML;
var days:Array = new Array("monday", "tuesday", "wednesday", "thursday", "friday", "saturday", "sunday");
var daySelected;
var businessNameArray:Array = new Array();
var businessLogoArray:Array = new Array();
var businessAddress:Array = new Array();
var distanceArrayDisplay:Array = new Array();
var distanceArrayCount = 0;
function init(e:Event = null):void
{
trace(myLoader.data);
theXML = new XML(e.target.data);
theXML.ignoreWhitespace = true;
myLoader.close();
// add our list and listener
var itemSizeCalculator = stage.stageHeight / 6;
touchList = new TouchList(stage.stageWidth, stage.stageHeight-itemSizeCalculator);
touchList.addEventListener(ListItemEvent.ITEM_SELECTED, handlelistItemSelected);
addChild(touchList);
touchList.x = stage.stageWidth;
// Fill our list with item rendreres that extend ITouchListRenderer.
for(var i:int = 0; i < theXML.food.length(); i++) {
if(theXML.food[i].monday != "un")
{
item = new TouchListItemRenderer();
item.index = i;
item.data = theXML.food[i].business;
item.name = theXML.food[i].business;
item.addEventListener(MouseEvent.CLICK, itemWasClicked);
item.itemHeight = itemSizeCalculator;
businessNameArray[i]= theXML.food[i].business;
businessLogoArray[i]=("http://www.logosURL.com/"+theXML.food[i].logo);
businessAddress[i]= theXML.food[i].address;
var fadedTextFormat:TextFormat = new TextFormat();
fadedTextFormat.bold = true;
fadedTextFormat.color = 0x999999;
fadedTextFormat.size = 14;
fadedTextFormat.font = "Helvetica";
fadedText = new TextField();
fadedText.height = 30;
fadedText.mouseEnabled = false;
fadedText.defaultTextFormat = fadedTextFormat;
item.addChild(fadedText);
fadedText.x = 75;
fadedText.y = 10;
distanceArrayDisplay.push(fadedText);
var distanceLoader:URLLoader = new URLLoader();
distanceLoader.load(new URLRequest("http://maps.googleapis.com&origins=someAddress&destinations="+businessAddress[i]+"&mode=walking&language=en-en&sensor=false"));
distanceLoader.addEventListener(Event.COMPLETE, distanceCalculated);
var logoLoader:Loader = new Loader();
item.addChild(logoLoader);
var logoURL:URLRequest = new URLRequest("http://www.myLogos.com/"+theXML.food[i].logo);
logoLoader.load(logoURL);
logoLoader.scaleX = .4;
logoLoader.scaleY = .4;
logoLoader.y = logoLoader.y + 5;
logoLoader.mouseEnabled = false;
var arrowGraphic:rightArrow = new rightArrow();
item.addChild(arrowGraphic);
arrowGraphic.x = stage.stageWidth - 5;
arrowGraphic.y = item.height/2;
touchList.addListItem(item);
}
}
}
function distanceCalculated(e:Event)
{
// trace(e.currentTarget.data);
var distanceXML:XML = new XML(e.target.data);
distanceXML.ignoreWhitespace = true;
var returnVar:String = (distanceXML.row.element.distance.text);
distanceArrayDisplay[distanceArrayCount].text = returnVar;
trace(returnVar);
distanceArrayCount++;
}
I am guessing that you are correctly reading the first XML, and that XML has a list of URLs that you want to load and then display some info from those on TextFields. Without knowing the structure of that XML I can't suggest you any working code, but I can point you on the right direction. For more info on reading/iterating XML on flash as3, please read: http://www.kirupa.com/developer/flashcs3/using_xml_as3_pg1.htm
//iterator var
var xml_read:uint=0;
//array of textfields for reference
var array_textFields:Array;
//config XML complete
function init(e:Event = null):void
{
array_textFields = new Array();
theXML = new XML(e.target.data);
theXML.ignoreWhitespace = true;
//this depends on the XML structure, please look at the article I linked
//for more info on how to iterate an XML
var i:uint=0;
for(someUrl in theXML..url)
{
var fadedText:TextField = new TextField();
//you should place each Textfield on different coord, otherwise
//they will all stack on top of each other and you will only see one
//for example:
fadedText.y = (fadedText.height+10)*i;
item.addChild(fadedText);
array_textFields.push(fadedText);
var distanceLoader:URLLoader = new URLLoader();
distanceLoader.load(new URLRequest(someUrl));
distanceLoader.addEventListener(Event.COMPLETE, distanceCalculated);
i++;
}
}
function distanceCalculated(e:Event):void
{
var distanceXML:XML = new XML(e.target.data);
distanceXML.ignoreWhitespace = true;
//retrieve information
var returnVar:String = (distanceXML.row.element.distance.text);
//set to textfield
TextField(array_textFields[xml_read]) = returnVar;
//increase iterator
xml_read++;
}
Please bear in mind that in ActionScript3, all network I/O is asynchronous. Usually EventListener functions don't return any value because you don't know when the data is ready. What you do is store a reference to where you want the data to go (in your case, a TextField variable) when the EventListener function is called asynchronously.

How to pass a variable into an Event.COMPLETE function?

I am running a loop to pull thumbs into a containing movieclip from an xml list. What I want to do is have the thumb's parent movieclip fade in after they are done loading, but I can't figure out how to reference the parent once it's loaded.
My code(which currently doesn't work the way I want it):
var vsThumb:articleBox;
var currentarticleX:Number = 0;
var articleLinkURL:String;
var articleImageURL:String;
var articleText:String;
var vsThumbLoader:Loader;
var next_x:Number;
next_x = 9;
var thumbAlphaTween:Tween;
var articlevsThumb:Array = new Array();
function loadarticleHeadlines():void
{
for (var i:int = 0; i < egarticleXml.articlelist.articleitem.length(); i++)
{
vsThumb = new articleBox();
vsThumb.alpha = 0;
vsThumbLoader = new Loader();
vsThumbLoader.load(new URLRequest(egarticleXml.articlelist.articleitem[i].articlethumbnail));
articleListContainter.addChild(vsThumb);
vsThumb.articleImage.addChild(vsThumbLoader);
vsThumb.articleTitle.text = egarticleXml.articlelist.articleitem[i].articletitle;
titleAutosize(vsThumb.articleTitle);
vsThumb.x = next_x;
next_x += 260;
articlevsThumb[i] = vsThumb;
vsThumbLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, showBox);
vsThumb.clickBtn.buttonMode = true;
}
function showBox(event:Event):void
{
thumbAlphaTween = new Tween(articlevsThumb[i],"alpha",None.easeNone,0,1,.25,true);
}
}
So how do I refer back to the loader's parent so I can fade in the whole movieclip? Can I pass a variable into the showBox function?
Don't use nested functions. They tend to make things more complicated.
i will always have the end value (articleitem.length()-1) in all of the event handlers you create, because its scope is the outer function, loadarticleHeadlines (it will increase by 1 on every iteration). That's probably why your code doesn't work.
The event will be fired on the loaderInfo of your loader, so you can find the loader's parent by using event.target.loader.parent:
function loadarticleHeadlines() : void
{
for (var i:int = 0; i < egarticleXml.articlelist.articleitem.length(); i++)
{
vsThumb = new articleBox();
vsThumb.alpha = 0;
vsThumbLoader = new Loader();
vsThumbLoader.load(new URLRequest(egarticleXml.articlelist.articleitem[i].articlethumbnail));
articleListContainter.addChild(vsThumb);
vsThumb.articleImage.addChild(vsThumbLoader);
vsThumb.articleTitle.text = egarticleXml.articlelist.articleitem[i].articletitle;
titleAutosize(vsThumb.articleTitle);
vsThumb.x = next_x;
next_x += 260;
articlevsThumb[i] = vsThumb;
vsThumbLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, showBox);
vsThumb.clickBtn.buttonMode = true;
}
}
function showBox(event:Event):void
{
thumbAlphaTween = new Tween(event.target.loader.parent,"alpha",None.easeNone,0,1,.25,true);
}
You don't need to pass a variable to your showBox, use the target property of the Event to retrieve the Loader:
function showBox(event:Event):void
{
var li:LoaderInfo=LoaderInfo(event.target);
// be nice remove your listener when your are done
li.removeEventListener(Event.COMPLETE, showBox);
var ldr:Loader=li.loader; // here is your loader
// do whatever you want with loader
thumbAlphaTween = new Tween(articlevsThumb[i],"alpha",None.easeNone,0,1,.25,true);
}