-
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.
-
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.
-
Bald By Choice
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.
-
Originally Posted by 5TonsOfFlax
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); ?
-
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);
-
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?
-
It's a common idiom. Ideally, the logical connections between your objects have very little to do with their organization on the display hierarchy.
-
Originally Posted by 5TonsOfFlax
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
-
Originally Posted by 5TonsOfFlax
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.
-
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.
-
and about the parameter for the event handler
what kind of event type the parameter has to be?
-
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|