dcsimg
A Flash Developer Resource Site

Results 1 to 7 of 7

Thread: Accessing Methods and Properties via getChildByName

  1. #1
    The New Guy
    Join Date
    Nov 2007
    Posts
    67

    Accessing Methods and Properties via getChildByName

    Hi guys

    I seem to be having a bit of a problem here.
    I'm generating a load of class instances using the following code:

    maxpersons=25;

    for (var i:Number=1;i<=maxpersons;i++)
    {
    var person:Person = new Person;
    person.name="person"+i;
    game.addChild(person);
    }


    OK. I then need to run through each of them and check for a collision with an object from the main stage (called redDot), which I pass to a method (called doDamage) within the class. I'm using the following code:

    for (i=1;i<=maxpersons;i++)
    {
    if (game.getChildByName("person"+i).hitTestObject(red Dot))
    {
    game.getChildByName("person"+i).doDamage(redDot);
    }
    }

    I keep getting an error saying "1061: Call to a possibly undefined method doDamage through a reference with static type flash.displayisplayObject."!

    It just won't work. I've tried altering the code to:

    game["person"+i].doDamage(redDot);

    but I get an unexpected trace output saying "TypeError: Error #1010: A term is undefined and has no properties."

    Maybe I'm going about it the wrong way. Can someone please shed some light on this, cos I haven't got a clue whats going on! Thanks!

  2. #2
    5+5=55 Schfifty Five's Avatar
    Join Date
    Jun 2006
    Posts
    698
    First, it should be:
    var person:Person = new Person();
    not
    var person:Person = new Person;

    And try this instead:

    for (i=1;i<=maxpersons;i++)
    {
    if ((game.getChildByName("person"+i) as Person).hitTestObject(redDot))
    {
    (game.getChildByName("person"+i) as Person).doDamage(redDot);
    }
    }

  3. #3
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    I'd also suggest putting all your persons in an array so that you don't have to use getChildByName. It'll be neater and faster.

    The main problem you were having however was the type mismatch. getChildByName returns a DisplayObject, so if you need to call a method defined in a more specific class, you have to cast it to that class first so the compiler can know what you're trying to do.

  4. #4
    The New Guy
    Join Date
    Nov 2007
    Posts
    67
    Oh man that code correction worked perfectly!!! Thanks guys!!
    Now I can finally continue my game. Thanks again!

  5. #5
    5+5=55 Schfifty Five's Avatar
    Join Date
    Jun 2006
    Posts
    698
    Quote Originally Posted by 5TonsOfFlax
    I'd also suggest putting all your persons in an array so that you don't have to use getChildByName. It'll be neater and faster.

    The main problem you were having however was the type mismatch. getChildByName returns a DisplayObject, so if you need to call a method defined in a more specific class, you have to cast it to that class first so the compiler can know what you're trying to do.
    Right, thanks for explaining it to him, I probably should have :P

  6. #6
    Junior Member
    Join Date
    Feb 2010
    Posts
    1
    I would like to see how you would organize this in an array. I have a similare issue, but it's an extended button class that I have added a bunch of variable data. I have not considered adding each extended button child in a container, and also I would like to see an example of code for the array concept. I tried casing the button with the as class name. The compiler did not complain but the code hangs.

  7. #7
    Junior Member
    Join Date
    Dec 2008
    Posts
    13
    Code:
    for (var i:Number=1;i<=maxpersons;i++)
    {
    //////var person:Person = new Person;///////
    person.name="person"+i;
    game.addChild(person);
    }
    the marked line is a very very bad way to do this. the first time the loop goes through it will create a var named person of type Person. you give it a name. then the 2nd time the loop goes through you overwrite the old variable person with a new one. For some reason tho flash will still have both instances in memory and on stage and certain methods and variables will still be accessible but most will not. To do this you would have to create some kinda object or array to hold all the variables. someone already mentioned using an array which tends to be the most used way of doing this.

    Code:
    var people:Array = new Array();
    people[i] = new Person();
    game.addChild(people[i]);
    or
    Code:
    var people:Object = new Object();
    people["person"+i] = new Person();
    game.addChild(people["person"+i]);

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