Results 1 to 14 of 14
Thread: AS3 Array
Hybrid View
-
September 15th, 2008, 12:46 PM #140of Theidan
postsAS3 Array
Hey! just wondering.. is there any way of reading the index from an array object?
if I have done something like this:
Cause i have written another function where i need that kind of information.Code:var array:Array = new Array; array.push "moose"; var arrayObject = array[1]; trace (arrayObject.index) //this is the part i am curious about.
Could anyone help me?!
Cheers
-
September 15th, 2008, 01:48 PM #21,596Holosuite User
postsEven though parenthesis are not mandatory for calling the constructor it's recommended that you use them. I.e.:
Other way of creating an empty array is:Code:var array:Array = new Array();
However you must enclose function arguments with parenthesis:Code:var array:Array = [];
Also, it's considered a good practice to always put type declaration for variable declaration:Code:array.push("moose");
The type casting isn't mandatory, but, there are a lot of cases where you will need this, because arrays use untyped access to the data, so, any value returned by array[] is assumed to be of type * (i.e. any type). however, in FP10 there are typed arrays (Vectors), where array access returns typed value.Code:var arrayObject:String = array[0] as String;
I'm not sure what you wanted to get by doing arrayObject.index, was it a position of the object inside an array? If so, you'd need
Code:array.indexOf(arrayObject);
Last edited by wvxvw; September 15th, 2008 at 03:04 PM.
I support FlashDevelop (the .NET open source editor for Flash and web developers)
couchsurfing if you need it
-
September 15th, 2008, 02:08 PM #3
-
September 15th, 2008, 03:04 PM #41,596Holosuite User
postsYes, sorry, I meant parenthesis. Edited the previous post.
I support FlashDevelop (the .NET open source editor for Flash and web developers)
couchsurfing if you need it
-
September 15th, 2008, 03:10 PM #5479the unafork bomber
postscreating an Array with brackets ( var a:Array = [] ) is faster than using parenthesis syntax.
thats my 2 cent
-
September 15th, 2008, 03:49 PM #640of Theidan
postsThank you very much.. this works fine for that sort of use.
but if I would make an array as a loader
eg:
would it be possible to get the index from here? i haven't gotten that to work.. and i really need it for a image gallery i'm working on, so that the pictures get in the right place..Code:function main { var array:Array = new Array(); //forgot the parentheses last time. array.push (new Loader); array[0].load(new URLRequest("somefile.html")); //skipping alot of stuff.. but i figure you will understand the meaning of it.. array[0].addEventListener(Event.COMPLETE, completeFunction); } function completeFunction(e:Event){ e.target.indexOf(); // how about here then?? }
Thanks!
-
October 15th, 2008, 12:10 PM #74Registered User
posts( ) <-- these are brackets
" " <-- these are parenthesese
-
October 16th, 2008, 04:11 AM #846Registered User
postshttp://en.wikipedia.org/wiki/Bracket#Computing
( ) are Parenthesis
" " are Quotes
[ ] ( ) and { } can be called Brackets under an umbrella term, but don't help when programming.
[ ] = brackets
( ) = Parens.
{ } = BracesLast edited by canazza; October 16th, 2008 at 04:14 AM.
Useful Resources
DataListener | KeyBinding | 'as2MovieClip'
Google Maps API for Flash CS3
David 'Canazza' McQuillan
-
June 4th, 2009, 07:46 AM #940of Theidan
posts
-
October 15th, 2008, 02:21 PM #104Registered User
postsYou're going to have some scope issues there because "array" is defined in main. It needs to be defined at the global or class scope. Then you can call "array.indexOf(e.target)"...or maybe e.currentTarget?
-
October 16th, 2008, 04:45 AM #1114Registered User
postsprivate function findInArray(arr:*, obj:*):int {
var i:int = 0;
for each (var vo:* in arr) {
if (vo == obj) return i;
i++
}
return -1;
}
-
October 16th, 2008, 09:07 AM #12
I think it should be new Loader()
You need to new an instance with a calling to the constructor function.My blog (mainly in Chinese, some in English)
If you wanna know more about China and the web/RIA dev in China, feel free to visit and ask me anything
-
October 16th, 2008, 09:48 AM #131Registered User
postsThanks for these tips!
-
March 3rd, 2009, 05:56 PM #141Registered User
postsI wanted the index of the object because I was recreating a battleship program we had to make for my first class at college. I wanted to find the position of the button so that I could create an effect. It would show the area hit and radiate outward, like how a rock splash's the water.
This allowed me to grab the index of it:
When I tried indexOf I just kept getting -1. Maybe I was using it wrong.Code://poorly named stuff and no return type but you get the point public function wasClicked(e:MouseEvent):void { for(var i:Number = 0; i < 10; i++) { for(var j:Number = 0; j < 10; j++) { if(e.currentTarget == nArray[i][j]) { trace(i.toString() + " " + j.toString()); } } } }Last edited by mrhoden; March 3rd, 2009 at 05:59 PM. Reason: forgot to take all the extra spacing off the code

Reply With Quote



Bookmarks