A Flash Developer Resource Site

Results 1 to 8 of 8

Thread: [AS3] Access dynamic name of object

  1. #1
    Member
    Join Date
    Aug 2003
    Posts
    70

    [AS3] Access dynamic name of object

    I know this may be a very easy problem for some, but I can't find the solution anywhere...

    I have an object, and I want to access it's property, but I'm passing the name dynamically... so instead of referencing person1Properties I'm trying to reference ("person"+id+"Properties)... but I have no luck...

    Any help greatly appreciated!

    Actionscript Code:
    var person1Properties:Object = {   
                            bodyParts:["full","body","heart","head","cloth"],
                            bodyDamage:[34,50,100,100,10],
                            drawTime:[1,3],
                            shotDelay:3,
                            health:100
                         };

    createPerson(1);                     
                         
    function createPerson(id:int):void{
        trace (Object("person" + id + "Properties").bodyParts);
    }
    Learning flash, one frame at a time

  2. #2
    Member
    Join Date
    Aug 2003
    Posts
    70
    I have found a way:

    Actionscript Code:
    trace (this["person" + id + "Properties"].bodyParts);

    and this works fine. Is there any other way?

    Thanks!
    Learning flash, one frame at a time

  3. #3
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    That is the way to access a property by a String. But you should never have to do that. If you used a real data structure instead of a naming scheme, you wouldn't have to.

  4. #4
    Member
    Join Date
    Aug 2003
    Posts
    70
    Care to extend a bit the explanation for "a real data structure"? More precisely what you mean by it and how would one of those be setup.

    Also any links would help.

    Thanks!
    Learning flash, one frame at a time

  5. #5
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    Well, the particulars will vary with the project. But in general, any time you feel like you want to name something with a number in it, you should use an Array or Vector instead.
    And instead of a plain Object with fields for bodyParts, bodyDamage, drawTime, shotDelay, and health, you could create a Class to represent that structure. It also looks like your bodyParts and bodyDamage arrays are parallel and you are trying to use them together to say something like the "full" part has 34 damage, the "body" part has 50, etc. If those should be linked logically, they should be part of a structure that actually links them. This could be another class.

    So, first change person1Properties, person2Properties, person3Properties, etc into
    Code:
    var peopleProperties:Array =[];
    
    peopleProperties[0] = {    
                            bodyParts:["full","body","heart","head","cloth"],
                            bodyDamage:[34,50,100,100,10],
                            drawTime:[1,3],
                            shotDelay:3,
                            health:100
                         };
    This lets you just access peopleProperties[0] instead of having to build a variable name by String.

    Then you can worry about turning that object into a class, so you could make it:
    Code:
    var peopleProperties:Array =[];
    
    peopleProperties[0] = new PersonProperties(["full","body","heart","head","cloth"],
                            [34,50,100,100,10],
                            [1,3],
                            3,
                            100);
    This ensures that you have type-checking on your PersonProperties fields, and you don't have to worry about typo-ing "bodyParts" as "bootyParts" or something.

    And then you can further refine it by creating a BodyPart class
    Code:
    peopleProperties[0] = new PersonProperties([new BodyPart(BodyPart.FULL, 34), new BodyPart(BodyPart.BODY, 50), new BodyPart(BodyPart.HEART, 100), new BodyPart(BodyPart.HEAD, 100), new BodyPart(BodyPart.CLOTH, 10)],
                            [1,3],
                            3,
                            100);
    Here, I've implied that you've created constants for various BodyPart types (HEAD, BODY, etc) so that you get compiler-time checking on the validity of those as well. And I've implied that your BodyPart class has a damage field which is actually associated with the part type.

    I still don't know what drawTime, shotDelay, or health are.

    Now, remember that I have no idea what you're actually using this stuff for, so some suggestions may not be in the right direction. But elaborate a bit more on what you're doing and I might be able to sketch out a design.

  6. #6
    Member
    Join Date
    Aug 2003
    Posts
    70
    Thank you very much for your explanation 5TonsOfFlax. Makes total sense.

    Basically my people are bandits. drawTime represents the time till they take out their gun and shoot(an interval between [0] and [1]), and shotDelay represents the time between shots. Health is the health of the bandit.

    So what I have is different scenes and different bandits. From PHP I'm going to post over to Flash what scene to load and what types of bandits there are on that specific scene.

    So body parts can be different from bandit to bandit, but indeed the damage is linked to the body part.

    That's why I went with the object model, so I could pass objects from PHP. Using arrays also makes sense, that was my first impulse to use, but then I switched to what you've seen.

    I'm all for making the code better and learning in the process, so thank you for your time in helping me out here and explaining some of the concepts.

    Eddie
    Learning flash, one frame at a time

  7. #7
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    It sounds like you don't need a separate properties object from your Bandit object. I still don't get drawTime. How is the array [1,3] an interval between 0 and 1?
    In the following code, I use separate set methods, but these could be passed in the constructor as above.

    Code:
    var people:Array = [];
    
    //could loop through php data for each bandit
    
    var b:Bandit = new Bandit();
    //these lines could be in a loop processing arrays/values from php
    b.addBodyPart(BodyPart.FULL, 34);
    b.addBodyPart(BodyPart.BODY, 50);
    b.addBodyPart(BodyPart.HEART, 100);
    b.addBodyPart(BodyPart.HEAD, 100);
    b.addBodyPart(BodyPart.CLOTH, 10);
    
    b.setDrawTime(1, 3);
    b.shotDelay = 3;
    b.health = 100;
    
    people.push(b);
    I'm not entirely familiar with what values you can get from php. Are you using flashvars, or a javascript function with ExternalInterface, or some amf encoding?

  8. #8
    Member
    Join Date
    Aug 2003
    Posts
    70
    Thanks for the explanation. the 1,3 is an interval for my randomize function

    Actionscript Code:
    function randomRange(min:Number, max:Number):Number {
        return (Math.floor(Math.random() * (max - min + 1)) + min);
    }

    which I feed the two values and it returns the time when he'll draw his gun.

    I'm not really sure for the PHP loading, as I haven't gotten that far, for now I'm programming the frontend with static data, and then will replace the static data with data from PHP. As for a decision, will probably go for AMF encoding.

    Now my biggest problem is understanding the new AS3 concepts versus the AS1 medium I come from, so that is my biggest challenge. I've gotten things to work as expected, the problem is the code isn't as clean and written as it should be.

    Thank you for your help so far, helps me understand some of the basic concepts that I lack.

    Eddie
    Learning flash, one frame at a time

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