Using AS3 Air for Android, What code can I use to list all or count the files from a chosen directory on local machine? In this case I am using Air For Android.
Then I would like to put them in an array
I am willing to give 50 pts for this answer. As you can see for my rep it's a lot but this question is HUGE for me. I would like it if possible if we don't use a Class Package.
The following code outputs a list of files and directories contained in the user's desktop directory.
import flash.filesystem.File;
var desktop:File = File.applicationDirectory.resolvePath("images");
var files:Array = desktop.getDirectoryListing();
for (var i:uint = 0; i < files.length; i++)
{
trace(files[i].nativePath); // gets the path of the files
trace(files[i].name);// gets the name
/// PUT ARRAY CODE HERE to add each "files[i].name" into an array for later use.
}
You can recurse through directories by checking if each File in the directory listing is a directory. The following function will return the count of all files recursively (excluding directories).
function recursiveDirectoryListing( directory:File ):int
{
var count:int = 0;
if (directory.isDirectory)
{
var files:Array = directory.getDirectoryListing();
for (var i:int = 0; i < files.length; i++)
{
if (files[i].isDirectory)
{
count += recursiveDirectoryListing( files[i] );
}
else
{
trace("file found: " + files[i].nativePath );
count += 1;
}
}
}
return count;
}
Then you can just call this function on whatever location you require to list:
recursiveDirectoryListing( File.applicationStorageDirectory );
Related
It shows an error when written this script has a package that cannot be nested how can I resolve the problem.
If not can anyone give me a new code so that I can try to make the new file?
I have this file in previous stack AS3 Bingo ticket generator
but i couldnt understan how to use it.
package {
import flash.display.Sprite;
import flash.text.TextField;
public class Main extends Sprite{
public var boards:Array = new Array();
private static const AMAUNT_BOARDS:uint = 6;
private static const NUMBER_FIELD_SIZE:uint = 20;
public function Main() {
for(var i:uint = 0; i < AMAUNT_BOARDS; i++)
{
var numbers:Array = genNumbers();
numbers = deleteFields(numbers);
var board:Sprite = getBoard(numbers);
board.y = NUMBER_FIELD_SIZE * 4 * i;
boards.push(board);
addChild(board);
}
}
//generates a 2 Dimensional Array (3x9) with TextFields
//populates the according Numbers and returns a board Sprite
private function getBoard(n:Array):Sprite
{
var s:Sprite = new Sprite();
var a:Array = new Array();
for(var i:uint = 0; i < 3; i++)
{
var b:Array = new Array();
for(var k:uint = 0; k < 9; k++)
{
//create TextFields
var tf:TextField = new TextField();
tf.x = k * NUMBER_FIELD_SIZE;
tf.y = i * NUMBER_FIELD_SIZE;
tf.border = true;
tf.width = NUMBER_FIELD_SIZE;
tf.height = NUMBER_FIELD_SIZE;
if(n[k][i] != 0) // adds the number if the value isn't 0
tf.text = n[k][i]; // Note that i am switching k & i because the number Array is 9x3
b.push(tf);
s.addChild(tf);
}
}
return s;
}
// Turns 4 random numbers out of the lines into 0 And returns the Array
private function deleteFields(a:Array):Array
{
for(var i:uint = 0; i < 3; i++)
{
var r:RandomPlus = new RandomPlus(8,0);
for(var k:uint = 0; k < 4; k++)
{
var t:uint = r.getNum();
a[t][i] = 0;
}
}
return a;
}
// Generates and returns a 2 Dimensional Array (9x3) with random numbers
private function genNumbers():Array
{
var a:Array = new Array();
var st:uint = 1;
var en:uint = 9;
for(var i:uint = 0; i < 9; i++)
{
var line:Array = new Array();
var ran:RandomPlus = new RandomPlus(en,st);
if(i == 0)//accounting for the number differnenz at start
st--;
if(i==7)//accounting for the number differnenz at end
en ++;
st += 10;
en += 10;
for(var e:uint = 0; e < 3; e++)
line[e] = ran.getNum();
a.push(line);
}
return a;
}
}
}
TL;DR: "Main" in the class definition may have to be changed conflict of already existing name. Extends sprite may need to be changed to extends movieclip. RandomPlus either needs an import you don't have or needs to be made into a symbol and exported to Actionscript.
Ok, so a few things:
If you are really putting the code into the stage code area, then you can't use the private identifier.
Unless RandomPlus is an object that you have defined in the stage it is probably either not a method you can use or you don't have the right imports for it. Look up what imports you need for RandomPlus or if it is an object defined in the stage then you may need to turn it into a symbol or if you have already export it to flash ActionScript I think. To do this you have to check the checkbox in the middle of the symbol creation page. It will always give you the error when you do this, but don't worry it is fine that way.
Third thing is that I never extend sprite in the class definition, I always do extends movieclip(not sure of the capitalization, but you can look that up). You may also be running into an error from using "Main" as the name because it may be a conflict with a name or method already defined in flash in general.
One last thing is for the variable declaration(mostly just for making the code more readable). While it is good to not declare variables as global when you don't have to, I tend to like having most of the variables up at the top, because I like to be able to see most of the declarations in one space. It's not necessary, really just a personal opinion, and I know a lot of experienced coders will say to do what you did, but if you want to use the arrays in multiple functions then sometimes it is easier to just declare them globally rather than having to pass a million things in the function call and then having to figure out later where all the variable declarations are called and where they are being passed as arguments. Again it's more of a coder's choice, but you can also just do whatever you feel more comfortable with rather than just following the already laid out rules by people who have more experience coding.
Another optional fix to the organization might be to name variables something meaningful so as to not forget what they are all doing. I am bad at naming as well, but I think it's really better to name them something better than just a single letter or two.
Another thing that can help is placing trace(code or "text"); in different places to see what's going wrong once you have the compiler happy.
i need help with my adobe flash assignment , i m still new to adobe flash :( how to tell the user if they input the wrong keyword how to link them to a frame .
var i:int = 0;
var names:Array = new Array("html","head","body");
var frames:Array = new Array("6","7","8");
searchhtmlll.text ="";
searchhtml.addEventListener(MouseEvent.CLICK, fl_MouseClickHandler_2);
function fl_MouseClickHandler_2(event:MouseEvent):void
{
findInArray(searchhtmlll.text);
gotoAndStop(frames[i]);
}
function findInArray(str:String):int
{
for(i = 0; i < names.length; i++)
{
if(names[i] == str)
{
return i;
}
}
return 0;
}
I am answering based on whatever limited information you have provided in your question, and from what I understand.
Your method findInArray has a return type of int, where as you are just calling it without assigning it to a variable.
So your code should be:
var result:int = findInArray(searchhtmlll.text);
gotoAndStop(frames[result]);
Hope this helps.
I'm really new to AS3 and I was wondering how I can apply a function to all instances of a class I'll show you guys what I have, is there anything I can do?
var cloud:Cloud;
for (i=0; i<5; i++)
{
cloud = new Cloud();
addChild(cloud);
}
this makes many instances of the Cloud class.
Later on I want to execute a function on all of these instances at once, how would I do that?
Store them in an Array or Vector:
var clouds:Vector.<Cloud> = new <Cloud>[];
for(var i:int = 0; i < 5; i++)
{
var cloud:Cloud = new Cloud();
clouds.push(cloud);
addChild(cloud);
}
Then iterate over that list and call a function on each item:
for each(var cloud:Cloud in clouds)
{
cloud.do();
}
There's my problem: I want to load the data from a text file (Named "myText.txt") with Flash CS5.5 . It contains some lines, and I want to store these lines in an Array. This is what I've got from now:
var myLoader:URLLoader = new URLLoader(new URLRequest("myText.txt");
var myArray:Array = new Array();
myLoader.addEventListener(Event.COMPLETE, loadComplete(myArray));
function loadComplete(myArray:Array):Function {
return function(e:Event):void {
myArray = myLoader.data.split("\n");
for(var i:int = 0; i < myArray.length; ++i){
trace(myArray[i]); // To check if it works at this point
}
}
}
for(var i:int = 0; i < myArray.length; ++i){
trace(myArray[i]); // To check if it gets modified
}
The fact is that the fist part works, it loads the text file and it stores in myArray, and traces it; but it stores only in the local version of myArray, it doesn't modify the reference, so the for outside of the function doesn't trace anything.
I had read that Arrays were passed by reference in flash, so I don't understand why it doesn't work.
I would appreciate the help.
EDIT
The thing now is that this is just a test file, I want this code to be in a function that I will use a lot. The ideal would be to have a static function in an AS Class File named "Utils", with other useful functions. The code of the "Utils.as" file would be like this:
package Include {
import flash.net.URLRequest;
import flash.net.URLLoader;
import flash.events.Event;
public class Utils {
public function Utils() {
}
public static function fileToArray(path:String):Array {
var linesArray = new Array();
// Code to load the file stored in 'path' (the 'path' String
// also has the name of the file in it), split by '\n' and store every line
// in the 'linesArray' Array.
// Example: path = "file:////Users/PauTorrents/Desktop/program/text.txt"
return linesArray;
}
// other functions
}
}
Thanks for the help.
A few things need addressing here.
First, your for loop at the end will always run before the load completes, so it will never trace anything. AS3 does not lock the thread when a URLoader loads, so it will move on with the rest of the code in the block while it waits to load the file.
Second, it's really ugly returning an annonumous function as the result of your load complete handler.
Here is how I would do this:
var myLoader:URLLoader = new URLLoader(new URLRequest("myText.txt");
var myArray:Array = new Array();
myLoader.addEventListener(Event.COMPLETE, loadComplete, false, 0, true);
function loadComplete(e:Event):void{
myArray = myLoader.data.split("\n");
for(var i:int = 0; i < myArray.length; ++i){
trace(myArray[i]); // To check if it works at this point
}
//now move on with the rest of your program/code
}
I know how to get Flash CS4 symbols into Flash Builder via .swc.
The class names become available in the main class, but I can only instantiate those one by one, writing each name into the code.
How can I loop through the .swc and load its assets in an array without mentioning their name, then obtain and use these names for instantiation? ideally, something like (half-assed pseudocode):
the_instances: = new Array
for(i=0; i<the_SWC.length; i++)
{
tmp = new eval( the_SWC[i].name + '\(\)' )
the_instances.push( tmp )
}
or anything else to get those names in a loop.
You have at least 2 options.
Option1:
Given the fact that a SWC file is a zip file that contains a swf with the embedded assets and an xml file describing the contents, you can load the swc as a zip, get the xml and parse it.
var swcLoader:URLLoader = new URLLoader(new URLRequest('assets/assetsLib.swc'));
swcLoader.dataFormat = URLLoaderDataFormat.BINARY;
swcLoader.addEventListener(Event.COMPLETE, swcLoaded);
function swcLoaded(event:Event):void{
var zipFile:ZipFile = new ZipFile(event.target.data);
for(var i:int = 0; i < zipFile.entries.length; i++) {
var entry:ZipEntry = zipFile.entries[i];
if(entry.name == 'catalog.xml'){
var data:ByteArray = zipFile.getInput(entry);
var list:XML = new XML(zipFile.getInput(entry));
var nodes:XMLList = list.children();
for (var j:int = 0; j < nodes.length(); j++) {
if (nodes[j].name().localName == "libraries") {
var libraries:XML = nodes[j];
var libList:XMLList = libraries.children();
for(var k:int = 0 ; k < libList.length(); k++){
var library:XML = libList[k];
var classList:XMLList = library.children();
for(var l:int = 0 ; l < classList.length(); l++){
var classDef:XML = classList[l];
trace('class name: ' + classDef.#name);
//var LibClass:Class = this.loaderInfo.applicationDomain.getDefinition(classDef.#name) as Class;
}
}
}
}
}
}
}
I am using the nochump library.
Option2:
Since you only need the class names to make your like easier and you mentioned using Flash CS4(which makes me assume you have access to the .fla file generating the swc), you can write a simple jsfl script that will write that line of code for you.
var doc = fl.getDocumentDOM();
var libItems = doc.library.items;
var libItemsNum = libItems.length;
var classesString = 'var '+doc.name.substr(0,doc.name.length-4)+'Classes = [';
var classesNum = 0;
var classes = [];
fl.outputPanel.clear();
for(var i = 0 ; i < libItemsNum; i++){
if(libItems[i].linkageExportForAS){
classes[classesNum] = libItems[i].linkageClassName;
classesNum++;
}
}
for(i = 0; i < classesNum; i++){
if(i < classesNum-1) classesString += '"'+classes[i]+'",';
else classesString += '"'+classes[i]+'"];';
}
fl.clipCopyString(classesString);
fl.trace(classesString);
All you need to do is:
File > New > Flash Javascript File and paste the code.
Save it in the Commands folder with a descriptive name, like: listExportClasses.
Since it's in the Commands menu, if you use this often enough you could add a keyboard shortcut.
What the command will is generate an array with the name of the fla file and contain the exported classes' names and conveniently place it in your clipboard.
e.g
var assetsLibClasses = ["Start1","Start2","Start3","Button","ColorBox","GameBackground","MenuBackground"];