-
AS3 - how do i call a funtion on main timeline, from a different timeline
so here's the function on main stage:
function slideImage(btnNum:Number)
{
trace("scrolling image");
trace(btnNum);
var myTween2:Tween = new Tween(imageObjects, "x", Strong.easeOut, imageObjects.x, imageObjects.x - 600, 1.5, true);
}
and here's the code that wants to call this function in a another movie clip. I've attached this clip dynamicaly to the main stage as well.
var btnNumber;
this.addEventListener(MouseEvent.CLICK, gotoLink);
function gotoLink(event:MouseEvent)
{
trace(btnNumber);
slideImage(btnNumber);
}
How can i acess slideImage function on the main stage. in AS2 _root.slideImage(btnNumber); would work. How Would I do this in AS3?
Apreciate your help. Thank you.
-
The quick and dirty way is a direct translation from your AS2 code:
Code:
MovieClip(root).slideImage(btnNumber);
But it's better to avoid tight coupling like that. The usual methods of doing this are to either 1) pass in a reference to the function to call or the root movie to avoid traversing the displayList. or 2) dispatch an event from the child that the parent will listen for and act on accordingly.
-
when I try MovieClip(root).slideImage(btnNumber); I get the error below.
TypeError: Error #1034: Type Coercion failed: cannot convert flash.display::Stage@2f6cf99 to flash.display.MovieClip.
how can i dispatch an event from the child?, Sorry I'm new to as3.
Thanks
-
That's funky. root should be the document class, not the stage. Unless you added the child directly to the Stage?
The dispatchEvent function will dispatch a new event that you can listen for in the main timeline. You can search this forum for examples.
post the code you are using to load and add the child movie. Something there is odd.
-
function createSlides(images:XML):void
{
stage.addChild(btnSm);
}
this is in maintimeline
i'm adding the child by
stage.addChild(btnSm);
var btnNumber;
this.addEventListener(MouseEvent.CLICK, gotoLink);
function gotoLink(event:MouseEvent)
{
trace(btnNumber);
slideImage(btnNumber);
}
this is in every instance of btnSm
I think I added the child directly to the stage.
Last edited by wraj; 07-15-2009 at 05:42 PM.
-
You did. Just remove the "stage." in "stage.addChild" so that it's just addChild or this.addChild. Or even root.addChild.
-
Hey Thank you very much, it works great. but I have a couple of questions
1) Why do you say its not a good idea to use MovieClip(root).slideImage(btnNumber); ?
2) where is addChild(someObj) or this.addChild(someObj) placing the objects?
again thank you for your help.
-
When you use the displaylist to get objects of a certain type, you are conflating function and display. If you decided to put this movie in another, then root would change and your code would break. You might think that you can use parent instead, but then any change in the display hierarchy like inserting a container would break it. It is more robust, and cleaner, to separate logic from presentation.
addChild(someObj) is exactly equivalent to this.addChild(someObj), and both of those add someObj as a display child to the object which calls that function. In your case, they add to the document instance, which is the same as root.
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
|