How to refer to 'parent object' of an instance variable?

5 posts

Flag Post

I have different ‘teamMember’ classes which adds a movieclip portrait, say ‘memberPortrait’.

So teamMember1.memberPortrait, teamMember2.memberPortrait, etc.

I need to add an eventListener to ‘memberPortrait’ which will dynamically call up the teamMember it belongs to, so I can then make that teamMember do something else.

For example…


teamMember1.memberPortrait.addEventListener(MouseEvent.CLICK, addToTeam);
public function addToTeam(e:MouseEvent):void
{
var activeTeamMember= e.currentTarget.parent;
stage.addChild(activeTeamMember.memberPortrait2);
}

e.currentTarget.parent is the line I’m concerned with… I’m trying to refer to the object that contains the e.currentTarget.

 
Flag Post

Add the listener within the teamMember class.
Now you dispatch an(other) Event that you can listen for.

This way you do not have to deal with things like parent etc.

Also, don’t ad stuff to the stage.
Just add them to your main class.

 
Flag Post

Hm I’m not sure how that would solve referencing the parent class from the instance variable.

But I think I found a good solution?

In the teamMember class, I added

memberA.parentMember = this;

So now I can reference the class memberA is in, with memberA.parentMember. It seemed to have worked, so far..

 
Flag Post

You could have the portrait class have a .member property which points to the member. That gives you instant access. Edit: Which you just did apparently, you ninja!

As long as you’re using it to display the proper portrait, which happens to be referenced on the member, a class that only works as data storage and performs no action, I could see it working. If you want that reference because you want to access the member from that button to make it perform some song-and-dance number that the button really shouldn’t know about, then don’t. Go with Nine’s suggestion instead.

 
Flag Post
Originally posted by Levgre:

Hm I’m not sure how that would solve referencing the parent class from the instance variable.

The parent would be dispatching the Event.