How to declare a filled Vector? - actionscript-3

I am using Vectors in Flash 10 for the first time, and I want to create it in the same way I used to do with Arrays, e.g:
var urlList : Array = [url1, url2, url3];
I have tried various different methods but none seem to work, and I have settled on the following as a solution:
var urlList : Vector.<String> = new Vector.<String>();
urlList.push(url1, url2, url3);
Is this even possible?

When it doubt, check the AS3 docs. :)
var urlList : Vector.<String> = new <String>["str1", "str2", "str3"];
trace(urlList);
http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/Vector.html#Vector()
Direct quote of the line I adapted this from in the documentation:
To create a pre-populated Vector instance, use the following syntax instead of using the parameters specified below:
// var v:Vector.<T> = new <T>[E0, ..., En-1 ,];
// For example:
var v:Vector.<int> = new <int>[0,1,2,];

You coerce an array to a Vector:
var urlList:Vector.<String> = Vector.<String>([url1, url2, url3]);

Related

Array of stagewebviews

I am in the need of multiple staged WebViews for holding multiple loaded websites at the same time.
I was hoping to manage this by making an array of webviews object, so i could call them later as view[i].
var view:Array=[webview0, webview1, webview2];
for each (var v in view){
var v:StageWebView = new StageWebView();
This gives error: 1086: Syntax error: expecting semicolon before left bracket.
Does someone know how to make an array like that?
You're doing something really weird there in terms of syntax. If you just want an Array of freshly created instances, it goes like that:
// Initialize the array.
var Views:Array = new Array;
// This loop counts 0,1,2.
for (var i:int = 0; i < 3; i++)
{
// Create a new instance.
// Yes, you can omit () with new operator if there are no arguments.
var aView:StageWebView = new StageWebView;
// Assign the new element to your array.
Views[i] = aView;
}
Or, if you need only 3 then you don't need to go algorithmic.
var Views:Array = [new StageWebView, new StageWebView, new StageWebView];
Not on topic but related:
Here is an example of one HTML page hold multiple StageWebViews
https://www.w3schools.com/graphics/tryit.asp?filename=trymap_basic_many

ActionScript: How to assign arrays by VALUE rather than REFERENCE?

I find it unfamiliar to work with ActionScript's array assignment by reference methodology. I understand what it's doing, but I somehow find it confusing to manage many arrays with this methodology. Is there a simple way to work with ActionScript arrays where the array assignment is by VALUE rather than REFERENCE? For example, if I want to assign oneArray to twoArray without linking the two arrays to each other forever in the future, how to do it? Would this work?
var oneArray:Array = new Array("a", "b", "c");
var twoArray:Array(3);
for (ii=0; ii<3; ii++) { twoArray[ii] = oneArray[ii]; }
The intent is to be able to change twoArray without seeing a change in oneArray.
Any advice how to assign arrays by VALUE instead of REFERENCE?
---- for reference ----
http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/Array.html
Array assignment is by reference rather than by value. When you assign one array variable to another array variable, both refer to the same array:
var oneArray:Array = new Array("a", "b", "c");
var twoArray:Array = oneArray; // Both array variables refer to the same array.
twoArray[0] = "z";
trace(oneArray); // Output: z,b,c.
Looks like you are looking for slice method. It returns a new array that consists of a range of elements from the original array.
var oneArray:Array = new Array("a", "b", "c");
var twoArray:Array = oneArray.slice();
twoArray[0] = "z";
trace(oneArray);
EDIT: Note that slice does a shallow copy, not a deep copy. If you are looking for a deep copy then please follow the link specified in the comment.
You can clone the array to guarantee two seperate copies with the same values in each Array element:
var oneArray:Array = new Array("a", "b", "c");
var twoArray:Array = oneArray.concat();
twoArray[0] = "z";
trace(oneArray); // Output: a,b,c
Hope this is what you're looking for.
If I understand the question correctly, you could do this:
var collection= new ArrayCollection(["a", "b", "c"]);
var clonedCollection = new ArrayCollection(ObjectUtil.copy(collection.source) as Array);
// a, b, c
trace(collection.toString());
// a, b, c
trace(clonedCollection .toString());
clonedCollection.removeItemAt(0);
// a, b, c
trace(collection.toString());
// b, c
trace(clonedCollection .toString());
You can create a clone function to copy the object using the ByteArray.writeObject and then out to a new object using ByteArray.readObject, as described in livedocs.adobe.com - Cloning arrays.
Note that writeObject and readObject will not understand objects more complex than Object and Array.

Parsing a path in Actionscript 3?

I'm using a URLLoader to load a photo and I want to be able to display the filename of the photo based on the URLLoader's loaderInfo.url property.
Given a loader named photoLoader, what the string called fileName be?
I would take the .url property and split it into an array using the / as the delimiter. Then just grab the last item in that array to get the filename.
Code:
var pathArray:Array = photoLoader.url.split('/')
var FileName:String = pathArray[pathArray.length()-1]
with
s:String = "http:/somedomain/someurl/somefilename";
You could do
fileName = s.split('/').pop()
to return the top of the array from splitting the url at '/'
var pathArray:Array = photoLoader.url.split('/')
var FileName:String = pathArray[pathArray.length-1]
Please note that the keyword "length" is not followed by parenthesis. For arrays, it is not supposed to be a function, it is a property. On the other hand, XML lists can use the length() function.

AS3 How to make a kind of array that index things based on a object? but not being strict like dictionary

How to make a kind of array that index things based on a object? but not being strict like dictionary.
What I mean:
var a:Object = {a:3};
var b:Object = {a:3};
var dict:Dictionary = new Dictionary();
dict[a] = 'value for a';
// now I want to get the value for the last assignment
var value = dict[b];
// value doesn't exits :s
How to make something like that. TO not be to heavy as a lot of data will be flowing there.
I have an idea to use the toString() method but I would have to make custom classes.. I would like something fast..
Why not make a special class that encapsulates an array, put methods in there to add and remove elements from the array, and then you could make a special method (maybe getValueByObject(), whatever makes sense). Then you could do:
var mySpecialArrayClass:MySpecialArrayClass = MySpecialArrayClass();
var a:Object = {a:3};
var b:Object = {a:3};
mySpecialArrayClass.addElement(a,'value for a');
var value = mySpecialArrayClass.getValueByObject(a);
I could probably cook up a simple example of such a class if you don't follow.
Update:
Would something like this help?
http://snipplr.com/view/6494/action-script-to-string-serialization-and-deserialization/
Update:
Could you use the === functionality? if you say
if ( object === object )
it compares the underlying memory address to see if two objects are the same reference...

Dynamic variables in ActionScript 3.0

so.... eval() out of the question, any idea to do this? I also don't know how to use "this" expression or set() in actionscript 3 ( i seem couldn't find any complete reference on it ), just say through php file a multiple variable (test1, test2, test3,...) sent by "echo", how the flash aplication recieved it? I'm trying not to use xml on mysql to php to flash aplication. Simply how to change a string to a variable ?
example
(in as3-actions frame panel)
function datagridfill(event:MouseEvent):void{
var varfill:URLVariables = new URLVariables();
varfill.tell = "do it";
var filler:URLRequest = new URLRequest();
filler.url = "http://127.0.0.1/flashdbas3/sendin.php";
filler.data = varfill;
var filling:URLLoader = new URLLoader();
filling.dataFormat = URLLoaderDataFormat.VARIABLES;
filling.load(filler);
filling.addEventListener(Event.COMPLETE, datain);
function datain(evt:Event){
var arraygrid:Array = new Array();
testing.text = evt.target.Name2 // worked
// just say i = 1
i=1;
arraygrid.push({Name:this["evt.target.Name"+i],
Test:this.["evt.target.Test"+i]}); // error
//or
arraygrid.push({Name:this["Name"+i],
Test:this.["Test"+i]}); // error too
// eval() noexistent, set() didn't worked on actions frame panel
//?????
}
};
I hope it's very clear.
You could use this[varName] if I understand your question right.
So if varName is a variable containing a string which should be a variables name, you could set and read that variable like this:
this[varName] = "someValue";
trace(this[varName]);
Update:
In your example, you could try: evt.target["Test"+i] instead of Test:this.["evt.target.Test"+i]
If you have a set of strings that you'd like to associate with values, the standard AS3 approach is to use an object as a hash table:
var o = {}
o["test1"] = 7
o["test2"] = "fish"
print(o["test1"])