|
-
Senior Member
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: 
-
Senior Member
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
My code above uses the same basic method.
Hope this helps
-
Senior Member
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: 
-
Senior Member
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
-
Senior Member
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: 
-
Senior Member
Ah in that case the problem might be to do with AS3. Not sure, though, having never used AS3 myself.
-
Senior Member
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: 
-
Funkalicious
Just shooting a blank here, but have you tried this?
Code:
var theObject = arr[i]
theObject["x"+j]
-
Senior Member
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: 
-
Funkalicious
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|