-
Calling function in root movieclip
Hi, I've got a movieclip on the root of the stage called "bgImg", in that clip I've made this function:
Actionscript Code:
function loadNew(e:Event) { trace("working"); }
When you click a button in the nav it loads a movieclip to the stage from the library, inside this movieclip I have a row of buttons, when you click one of these I want to call the function in the movieclip on the root (ie. the function inside "bgImg"
I tried this:
Actionscript Code:
var _root = root; empty.addEventListener(MouseEvent.CLICK, loadNewBG);
function loadNewBG(e:MouseEvent){ MovieClip(_root.bgImg).loadNew(); }
I also tried this:
Actionscript Code:
var _root = root; empty.addEventListener(MouseEvent.CLICK, loadNewBG);
function loadNewBG(e:MouseEvent){ _root.bgImg.loadNew(); }
but both throw up errors - any help please?
Thanks
-
Do you think error messages are meaningless? No? Then why wouldn't you tell us what errors you got?
The error you got was probably something like "possibly undefined property bgImg". The reason it happens is because root is typed as DisplayObject, which does not define a bgImg property. To get the compiler to know that there is a bgImg property, you need to cast the root object to some class which either does declare bgImg, or is dynamic. MovieClip is dynamic.
There's no need to create a _root variable (and if there were, you should declare a type for it).
Code:
function loadNewBG(e:MouseEvent):void{
MovieClip(root).bgImg.loadNew();
}
Doing it this way (having the child movie access methods and properties in the parent) is not recommended. Instead, you could dispatch an event from your child instance and listen for it in the parent/root. This would decouple the child from having to know the structure of the rest of the movie.
This is THE most common question we get on this forum.
-
no need to get ****ty with me :|
thanks for the help anyway. I can't remember what errors it was throwing up and don't have the files with me at the moment, hence why I didn't post them. I'll try this later
-
no need to get ****ty with me :|
You're right. I'm not having a great day. Good luck with your project.
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
|