A Flash Developer Resource Site

Results 1 to 10 of 10

Thread: accessing object's properties problem

  1. #1
    Senior Member bluemagica's Avatar
    Join Date
    Jun 2008
    Posts
    766

    accessing object's properties problem

    I have an array of objects, each of which has a series of x,y properties, like x1,y1,x2,y2,..... now how can I access these dynamically?
    If you like me, add me to your friends list .

    PS: looking for spriters and graphics artists for a RPG and an Arcade fighting project. If you can help out, please pm me!

    My Arcade My Blog

    Add me on twitter:

  2. #2
    Senior Member Orkahm52's Avatar
    Join Date
    Jan 2006
    Location
    Perth, Australia
    Posts
    335
    I assume you know about doing it like this:

    Code:
    array[0].x1 = value;
    array[0].x2 = value;
    array[0].x3 = value;
    ...
    I guess you want to access each value through a for loop or some such:

    Code:
    for (i=0;i<array.length;i++) {
      for (n=0;n<10;n++) {
        trace(array[i]["x"+n]);
        trace(array[i]["y"+n]);
      }
    }
    That code loops through the entirety of the array, then traces the values of each object's first ten x's and y's

    You might have seen code where people use
    Code:
    _root["object"+1]._y
    instead of
    Code:
    _root.object1._y
    My code above uses the same basic method.

    Hope this helps

  3. #3
    Senior Member bluemagica's Avatar
    Join Date
    Jun 2008
    Posts
    766
    I did try that already, but for some reason i am getting "undefined"....that's the reason I asked this! On a side note, is it possible to get the length or number of properties of the object for use in the for loop?
    Last edited by bluemagica; 10-07-2009 at 07:44 AM.
    If you like me, add me to your friends list .

    PS: looking for spriters and graphics artists for a RPG and an Arcade fighting project. If you can help out, please pm me!

    My Arcade My Blog

    Add me on twitter:

  4. #4
    Senior Member Orkahm52's Avatar
    Join Date
    Jan 2006
    Location
    Perth, Australia
    Posts
    335
    I've tried it and it doesn't give me undefined. Have you tried manually accessing it to check that it isn't really undefined? Perhaps you can post up some of your code so I can have a look.

    I don't know of any way to count the number of properties. Not sure exactly what your trying to do but it might be a good idea to have an array instead of lots of properties.

    Code:
    object.xArray = [0,0,0];
    object.yArray = [2,2,2];
    It might make things a bit easier for you

  5. #5
    Senior Member bluemagica's Avatar
    Join Date
    Jun 2008
    Posts
    766
    actually I would need to let the player reference values in game with a name(think a mini programming simulator), so objects work best in my case! And I have no idea why I got the undefined thing....but the same code gives this problem in flashcs4, and works perfectly when I compile it in flex SDK+ Flashdevelop!

    Also the statement I use for checking is: trace(arr[i].x1+" : "+arr[i]["x"+1]),
    If you like me, add me to your friends list .

    PS: looking for spriters and graphics artists for a RPG and an Arcade fighting project. If you can help out, please pm me!

    My Arcade My Blog

    Add me on twitter:

  6. #6
    Senior Member Orkahm52's Avatar
    Join Date
    Jan 2006
    Location
    Perth, Australia
    Posts
    335
    Ah in that case the problem might be to do with AS3. Not sure, though, having never used AS3 myself.

  7. #7
    Senior Member bluemagica's Avatar
    Join Date
    Jun 2008
    Posts
    766
    flex and flash both are as3 actually....just some features and implementations are a bit different
    If you like me, add me to your friends list .

    PS: looking for spriters and graphics artists for a RPG and an Arcade fighting project. If you can help out, please pm me!

    My Arcade My Blog

    Add me on twitter:

  8. #8
    Funkalicious TOdorus's Avatar
    Join Date
    Nov 2006
    Location
    Nijmegen, Netherlands
    Posts
    697
    Just shooting a blank here, but have you tried this?

    Code:
    var theObject = arr[i]
    theObject["x"+j]

  9. #9
    Senior Member bluemagica's Avatar
    Join Date
    Jun 2008
    Posts
    766
    oookay...this one works in both cs4 and fd.....but why?
    If you like me, add me to your friends list .

    PS: looking for spriters and graphics artists for a RPG and an Arcade fighting project. If you can help out, please pm me!

    My Arcade My Blog

    Add me on twitter:

  10. #10
    Funkalicious TOdorus's Avatar
    Join Date
    Nov 2006
    Location
    Nijmegen, Netherlands
    Posts
    697
    Yeah weird. Guess the Flash compiler treats arrays differently then Flex'

Posting Permissions

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




Click Here to Expand Forum to Full Width

HTML5 Development Center