A Flash Developer Resource Site

Results 1 to 12 of 12

Thread: Accessing parent's variables

  1. #1
    Member
    Join Date
    Sep 2009
    Posts
    88

    Question Accessing parent's variables

    I'm trying to access the AchievementScreen's textfield from a medal object. Point being to have a mouseover event of the medal, change the content of the textfield shown.

    I tried a trace first: trace(this.parent.parent.parent.parent.textLineCon tainer);

    1119: Access of possibly undefined property textLineContainer through a reference with static type flash.displayisplayObjectContainer.

    trace(this.parent.parent.parent.parent) shows me it is a AchievementScreen object.

    textLineContainer is the instance name of the MC nesting the textfield (I actually drew the achievement screen then exported it to actionscript.

    Some website suggested doing one of the followings:
    1-MovieClip(this.parent.someProperty)
    2-(this.parent as MovieClip).someProperty;
    which worked for some people apparently but not me.

  2. #2
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    To do the MovieClip cast, you need to cast the object on which you want to call the dynamic property. So it would be around all of the .parents.

    But don't do that.

    Instead, just have the interested clip register a listener for MouseEvent.MOUSE_OVER on the thing you want to detect mouseover on. Or, have it dispatch a custom event which will bubble up to that container clip.

    I swear, I want to write a JSFL extension to check for common bad practices.

  3. #3
    Bald By Choice jasondefra's Avatar
    Join Date
    Mar 2008
    Location
    Wisconsin, USA
    Posts
    205
    I want you to write one too, 5Tons. I would love to use it on my work to see if any bad habits from my AS2 days carried over without my knowing.
    Follow me on Twitter: http://twitter.com/jasondefra

  4. #4
    Member
    Join Date
    Sep 2009
    Posts
    88
    Quote Originally Posted by 5TonsOfFlax View Post
    To do the MovieClip cast, you need to cast the object on which you want to call the dynamic property. So it would be around all of the .parents.

    But don't do that.

    Instead, just have the interested clip register a listener for MouseEvent.MOUSE_OVER on the thing you want to detect mouseover on. Or, have it dispatch a custom event which will bubble up to that container clip.

    I swear, I want to write a JSFL extension to check for common bad practices.
    So I'd do this.child.child.child.child.addEventListener(mous eover, changeDisplayText); ?

  5. #5
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    No, that's very nearly as bad.

    Instead, have the thing that is being moused over have a mouseover listener which dispatches a custom event which bubbles up, and have the code which needs to react to that event listen for it.

    in the far down child
    Code:
    whatever.addEventListener(MouseEvent.MOUSE_OVER, textChange);
    function textChange(e:MouseEvent):void{
      dispatchEvent(new Event("changeDisplayText", true));
    }
    in the ancestor
    Code:
    addEventListener("changeDisplayText", changeDisplayText);

  6. #6
    Member
    Join Date
    Sep 2009
    Posts
    88
    Oh that's new to me. Is that the normal AS3 way to have a child event be applied to a different child of the parent?

  7. #7
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    It's a common idiom. Ideally, the logical connections between your objects have very little to do with their organization on the display hierarchy.

  8. #8
    Member
    Join Date
    Sep 2009
    Posts
    88
    Quote Originally Posted by 5TonsOfFlax View Post
    It's a common idiom. Ideally, the logical connections between your objects have very little to do with their organization on the display hierarchy.
    I don't get the meaning of that lol

  9. #9
    Member
    Join Date
    Sep 2009
    Posts
    88
    Quote Originally Posted by 5TonsOfFlax View Post
    Code:
    whatever.addEventListener(MouseEvent.MOUSE_OVER, textChange);
    function textChange(e:MouseEvent):void{
      dispatchEvent(new Event("changeDisplayText", true));
    }
    in the ancestor
    Code:
    addEventListener("changeDisplayText", changeDisplayText);
    What are the imports required for the dispatcher and listener?

    Also the even handler parameter is what?:

    private function customEventHandler(e:?????)
    {
    }
    Last edited by ArenaFlash; 03-09-2010 at 07:09 PM.

  10. #10
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    flash.events.EventDispatcher

    Or technically, flash.events.IEventDispatcher, but you almost certainly do not need to implement that.

    You shouldn't even need to import EventDispatcher, since you are talking about display objects, and all DisplayObjects already extend EventDispatcher and therefore have its methods.

  11. #11
    Member
    Join Date
    Sep 2009
    Posts
    88
    and about the parameter for the event handler

    what kind of event type the parameter has to be?

  12. #12
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    Event.

    If you are expecting a more specific type, you can use that.

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