Skip to main content
Participant
August 12, 2011
Question

Loaded to main swf communication question

  • August 12, 2011
  • 2 replies
  • 613 views

I'm upgrading some elearning code to AS3 and having trouble with a "Jump Menu" that loads at 3 in the main swf. here is the main swf code:

//Main swf shell
var menuLoader:Loader = new Loader();
menuLoader.contentLoaderInfo.addEventListener(Event.INIT, doneLoading3);

var menuMC:MovieClip = new MovieClip();
stage.addChild(menuMC);

function doneLoading3(e:Event):void {
menuMC = MovieClip(menuLoader.content);
stage.addChildAt(menuMC, 3);
menuMC.visible = false;
};

function thirdClip():void{
menuLoader.load(new URLRequest("jump_menu_as3.swf"));
};

thirdClip();

The menu gets it's content from an XML file and builds the links dynamically:

//Loaded swf jump_menu_as3

var textFormat:TextFormat = new TextFormat("Verdana", 12);
var xml:XML;

var xmlLoader:URLLoader = new URLLoader();
var url:URLRequest = new URLRequest("media.xml");
xmlLoader.load(url);
xmlLoader.addEventListener(Event.COMPLETE, onXmlLoad);

function onXmlLoad(e:Event):void { 
xml = new XML(xmlLoader.data); 
var mediaElements2:XMLList = xml.level2.media2;
var len:int = mediaElements2.length();
for (var i:int = 0; i < len; i++) { 
var tf:TextField = new TextField()
tf.defaultTextFormat = textFormat
tf.htmlText = mediaElements2
  tf.x = 10
tf.y = 20*i + 10
tf.width = 300
tf.selectable = false;
  jump_menu.megaMan.addChild(tf)
tf.addEventListener(TextEvent.LINK, linkEvent);
function linkEvent(event:TextEvent):void {
switch (true) {
case (event.text == "myText"):
   MovieClip(this.parent.parent).widget1 = 0;
   MovieClip(this.parent.parent).nextClip();
break;

case (event.text == "myText1"):
   MovieClip(this.parent.parent).widget1 = 1;
   MovieClip(this.parent.parent).nextClip();
break;

case (event.text == "myText2"):
   MovieClip(this.parent.parent).widget1 = 2;
   MovieClip(this.parent.parent).nextClip();
break;
default :
trace("curious");
}
}

I can trace that the XML links are loading and that they trigger a trace, but I can't get them to talk to the main swf. Please help me figure out what I'm missing.

Thanks

This topic has been closed for replies.

2 replies

kglad
Community Expert
Community Expert
August 12, 2011

use:


//Main swf shell
var menuLoader:Loader = new Loader();
menuLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, doneLoading3);


function doneLoading3(e:Event):void {

this.addChildAt(menuLoader,3);


menuLoader.visible=false;
};

function thirdClip():void{
menuLoader.load(new URLRequest("jump_menu_as3.swf"));
};

thirdClip();

The menu gets it's content from an XML file and builds the links dynamically:

//Loaded swf jump_menu_as3

var textFormat:TextFormat = new TextFormat("Verdana", 12);
var xml:XML;

var xmlLoader:URLLoader = new URLLoader();
var url:URLRequest = new URLRequest("media.xml");
xmlLoader.load(url);
xmlLoader.addEventListener(Event.COMPLETE, onXmlLoad);

function onXmlLoad(e:Event):void { 
xml = new XML(xmlLoader.data); 
var mediaElements2:XMLList = xml.level2.media2;
var len:int = mediaElements2.length();
for (var i:int = 0; i < len; i++) { 
var tf:TextField = new TextField()
tf.defaultTextFormat = textFormat
tf.htmlText = mediaElements2
  tf.x = 10
tf.y = 20*i + 10
tf.width = 300
tf.selectable = false;
  jump_menu.megaMan.addChild(tf)
tf.addEventListener(TextEvent.LINK, linkEvent);
function linkEvent(event:TextEvent):void {
switch (true) {
case (event.text == "myText"):
   MovieClip(this.parent.parent).widget1 = 0;
   MovieClip(this.parent.parent).nextClip();
break;

case (event.text == "myText1"):
   MovieClip(this.parent.parent).widget1 = 1;
   MovieClip(this.parent.parent).nextClip();
break;

case (event.text == "myText2"):
   MovieClip(this.parent.parent).widget1 = 2;
   MovieClip(this.parent.parent).nextClip();
break;
default :
trace("curious");
}
}


Participant
August 12, 2011

Getting closer, but that loads the menu behind the clip loaded at 1 so I can't test it - because it's supposed to control the next movie loading

at 1. I really appreciate your help - don't blame you if you are running out of patience. If you have another idea, I'll take it.

kglad
Community Expert
Community Expert
August 12, 2011

just use addChild() (and not addChildAt) if you want to add your loaded swf to the top-most depth.

kglad
Community Expert
Community Expert
August 12, 2011

you need to add your loader to the stage (and, you can't get a meaningful reference to your loader's content until loading is complete).

Participant
August 12, 2011

So even though I can click a button that makes the menu visible and loads the xml content, I still need to add the loader to the stage?

Just because I can see it and click the links doesn't mean it's loaded?

kglad
Community Expert
Community Expert
August 12, 2011

if you want MovieClip(loader.content).parent.parent to reference the loading movieclip's main timeline, you need to add your loader to the main timeline.

if you add the loader's content to the main timeline, you shouldn't use .parent.parent.