Everybody! This is important. In a few days, these forums will be moving over to using the totally sweet Discourse platform. To ensure this migration happens smoothly with no loss of content, these forums are currently in a read-only mode. I do apologize for the inconvenience.

There is never a good time to turn the forums off for an extended period of time, but I promise the new forums will be a billion times better. I'm pretty sure of it.

See you all on the other side in a few days, and if you have any (non-technical) questions, please e-mail me at kirupa@kirupa.com. For technical questions, try to find a tutorial that corresponds to what you are looking for and post in the comments section of that page.

Cheers,
Kirupa

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:

    Code:
    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?!

    Cheers

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

  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
    1,596
    posts
    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
    eg:
    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??
    }
    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..

    Thanks!

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

  8. #8
    Quote Originally Posted by randygland2 View Post
    ( ) <-- these are brackets
    " " <-- these are parenthesese
    http://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.
    { } = 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
    No..

    () <-- 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(e.target)"...or maybe e.currentTarget?

  11. #11
    14
    posts
    Registered User
    private function findInArray(arr:*, obj:*):int {
    var i:int = 0;
    for each (var vo:* in arr) {
    if (vo == obj) return i;
    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
    1
    posts
    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:
    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());
    			}
    		}
    	}
    }
    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 kirupa.com Meet the Moderators Advertise

 Link to Us

 Credits

Copyright 1999 - 2012