-
[RESOLVED] Accessing MC on stage from external SWF
OK, I load an external SWF on stage.
I have a mc on there already, it's called 'darkOverlay' (instance name).
This is the code I'm trying to use to access it:
Code:
trace(stage.getChildByName("darkOverlay"));
if(stage.getChildByName("darkOverlay"))
{
MovieClip(stage.getChildByName("darkOverlay")).gotoAndPlay(21);
}
It returns null even when it's loaded in the main swf. What am I overlooking here?
-
darkOverlay may not be a direct child of the stage. Unless you put it on there with stage.addChild, darkOverlay will be a child of the root, not the stage.
Also, if darkOverlay is a clip which is on the root, and has the instancename "darkOverlay", flash automatically creates a matching property so you shouldn't have to use getChildByName to access it.
Basically, you should be able to just do
Code:
darkOverlay.gotoAndPlay(21);
If that doesn't work, try replacing "stage" with "root" in your code.
-
The MC is on the root of the main SWF whereas the code is in the externally loaded SWF. I can't call the root since it's the root of the external SWF.
Anyway to get to the root of the main swf?
-
Or could I dispatch an event to stage and have a listener for it? Would that be a viable workaround?
This is what I've tried with the dispatch event:
Code:
dispatchEvent(new Event("darkenStage")); // In external SWF
And in the main swf:
Code:
addEventListener("darkenStage", darkenOverlay);
function darkenOverlay(e:Event):void
{
if(darkOverlay) darkOverlay.gotoAndPlay(21);
}
But the stage never seems to recieve the event.
-
You could use loaderInfo.loader.parent.root, but it's generally better to not have children directly manipulate parents. Have you considered using event dispatch to handle this communication?
Upon loading the content, you can set an event listener for your custom event. In the loaded movie, instead of trying to access darkOverlay directly, dispatch an event. The loading movie then handles that event by telling darkOverlay what to do.
-
You need to add your listener to the loaded content.
Code:
loadedswf.addEventListener("darkenStage", darkenOverlay);
-
Originally Posted by 5TonsOfFlax
You could use loaderInfo.loader.parent.root, but it's generally better to not have children directly manipulate parents. Have you considered using event dispatch to handle this communication?
Upon loading the content, you can set an event listener for your custom event. In the loaded movie, instead of trying to access darkOverlay directly, dispatch an event. The loading movie then handles that event by telling darkOverlay what to do.
LoL. Beat you to it :P
-
Originally Posted by 5TonsOfFlax
You need to add your listener to the loaded content.
Code:
loadedswf.addEventListener("darkenStage", darkenOverlay);
Still not catching the event for some reason. I'm trying to dispatch this event when a button is clicked.
Code:
function aNews (e:MouseEvent):void
{
modalArchives.gotoAndPlay(2);
var theDispatcher:EventDispatcher = new EventDispatcher;
theDispatcher.dispatchEvent(new Event("darkenStage"));
}
And this is the part in the main SWF where the external swf gets displayed:
Code:
function showL(e:TimerEvent):void
{
removeChild(loadingDialog);
l.y = 0;
addChild(l);
l.addEventListener("darkenStage", darkenOverlay);
l.addEventListener("lightenStage", lightenOverlay);
stopClip.gotoAndPlay(1);
l.x = (stage.stageWidth / 2) - (l.width / 2) + 212;
t.stop();
t = null;
}
And this is the function and how I know it's not catching the event.
Code:
function darkenOverlay(e:Event):void
{
trace (darkOverlay);
if(darkOverlay) darkOverlay.gotoAndPlay(21);
}
Last edited by Beathoven; 05-07-2009 at 10:52 AM.
-
You don't need to create a new EventDispatcher. Just call dispatchEvent. Creating a new EventDispatcher separates that event from the flow of other events. To catch that event, the listener would have to be added to that dispatcher. Since DisplayObjects are already EventDispatchers, you can just use the inherited dispatchEvent method.
Code:
function aNews (e:MouseEvent):void
{
modalArchives.gotoAndPlay(2);
dispatchEvent(new Event("darkenStage"));
}
I'm not sure if the event will bubble through the loader, which is why I thought you might want to add the listener to the content rather than the loader itself. If it does bubble, then you're fine.
-
Originally Posted by 5TonsOfFlax
You don't need to create a new EventDispatcher. Just call dispatchEvent. Creating a new EventDispatcher separates that event from the flow of other events. To catch that event, the listener would have to be added to that dispatcher. Since DisplayObjects are already EventDispatchers, you can just use the inherited dispatchEvent method.
Code:
function aNews (e:MouseEvent):void
{
modalArchives.gotoAndPlay(2);
dispatchEvent(new Event("darkenStage"));
}
I'm not sure if the event will bubble through the loader, which is why I thought you might want to add the listener to the content rather than the loader itself. If it does bubble, then you're fine.
Figured it out. You got me thinking there, when you said 'I'm not sure if the event will bubble through the loader'.
I added the event listener to the content of the loader this way:
Code:
l.content.addEventListener("darkenStage", darkenOverlay);
Works!
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
|