The forums have permanently moved to This forum will be kept around in read-only mode for archival purposes. To learn how to continue using your existing account on the new forums, check out this thread.

Results 1 to 14 of 14

Thread: AS3 Array

Hybrid View

  1. #1

    AS3 Array

    Hey! just wondering.. is there any way of reading the index from an array object?

    if I have done something like this:

    var array:Array = new Array;
    array.push "moose";
    var arrayObject = array[1];
    trace (arrayObject.index) //this is the part i am curious about.
    Cause i have written another function where i need that kind of information.

    Could anyone help me?!


  2. #2
    Holosuite User
    Even though parenthesis are not mandatory for calling the constructor it's recommended that you use them. I.e.:
    var array:Array = new Array();
    Other way of creating an empty array is:
    var array:Array = [];
    However you must enclose function arguments with parenthesis:
    Also, it's considered a good practice to always put type declaration for variable declaration:
    var arrayObject:String = array[0] as String;
    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.
    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
    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

  3. #3
    For clarity:
    () = parenthesis
    {} = braces
    [] = brackets
    I used to be a paranoid-psychotic-schizophrenic, but don't worry, he's dead now.

  4. #4
    Holosuite User
    Yes, 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

  5. #5
    creating an Array with brackets ( var a:Array = [] ) is faster than using parenthesis syntax.
    thats my 2 cent

  6. #6
    Thank you very much.. this works fine for that sort of use.
    but if I would make an array as a loader
    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){; // how about here then??
    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..


  7. #7
    ( ) <-- these are brackets
    " " <-- these are parenthesese

  8. #8
    Quote Originally Posted by randygland2 View Post
    ( ) <-- these are brackets
    " " <-- these are parenthesese

    ( ) are Parenthesis
    " " are Quotes

    [ ] ( ) and { } can be called Brackets under an umbrella term, but don't help when programming.
    [ ] = brackets
    ( ) = Parens.
    { } = Braces
    Last edited by canazza; October 16th, 2008 at 04:14 AM.

  9. #9
    Quote Originally Posted by randygland2 View Post
    ( ) <-- these are brackets
    " " <-- these are parenthesese

    () <-- those are paranthese
    " " <-- those are quotation marks!

  10. #10
    You'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("...or maybe e.currentTarget?

  11. #11
    Registered User
    private function findInArray(arr:*, obj:*):int {
    var i:int = 0;
    for each (var vo:* in arr) {
    if (vo == obj) return i;
    return -1;

  12. #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

  13. #13
    Registered User
    Thanks for these tips!

  14. #14
    Quote Originally Posted by Pase View Post
    Thanks for these tips!
    I 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:
    //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());
    When I tried indexOf I just kept getting -1. Maybe I was using it wrong.
    Last edited by mrhoden; March 3rd, 2009 at 06:59 PM. Reason: forgot to take all the extra spacing off the code

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Home About Meet the Moderators Advertise

 Link to Us


Copyright 1999 - 2012