|
-
place loaded swf into movieciip
i´m trying to place a loader content (an swf which is an instance of the class "Nav") into a movieclip (so i can easily refer to its variables, for example, width) but when i trace the movieclip it throws null. why ?
Code:
private function doneNav(e:Event):void
{
trace (navLoader.content); // [Object Nav]. fine!
nav = navLoader.content as MovieClip; // null. why??
trace (navLoader.getChildAt(0)); // [Object Nav]. fine!
nav = navLoader.getChildAt(0) as MovieClip; // null. why??
i´d like to later in the code do this:
Code:
nav.x = (stage.stageWidth - nav.width)/2;
what are the best practices in loading swfs?
thanks!
-
It looks like Nav does not extend MovieClip. If that's the case, then the as operator will return null. Why do you think you need it to be a MovieClip?
-
it was a movieclip.
BUT i´ve changed to Sprite and it worked !
should I keep the nav statement there, or should i move it to the constructor or another function?
does the code runs slow there too?
Code:
public class Home extends Sprite
{
private var nav:Sprite= new Sprite();
thanks!
-
Code:
trace (navLoader.content); // [Object Nav]. fine!
nav = navLoader.content as Sprite;
trace (nav); // [Object Nav]. great!
trace (nav.width); // throws 1700. ( this swf has 1700 width. the actual width of nav.swf is 900. why ?
-
Declaring your class level variables where you have there is correct. But, unless you actually need nav to be non-null from the beginning, you can just declare it and not instantiate it. When you set it in the doneNav function, you throw away whatever was there to begin with. If you never used that, then you didn't need it.
Code:
private var nav:Sprite;
-
I don't know what's up with the incorrect width. Does it display correctly? Did you add anything to nav that might have increased its size before tracing the width?
-
right. i´ve done it like this then in the LoadNav function:
Code:
nav = new Sprite();
nav = navLoader.content as Sprite;
did you see my last post about the width ?
thanks!
-
Saw the width question. Don't know what's going on there.
You don't need to set nav to a new Sprite before setting it to the loader content. All that does is create a new Sprite just to throw it away. Remove that line.
-
thanks again!
so.. it is really weird the width question.
here is the Nav code (it is a 900x90 swf):
Code:
package
{
import flash.display.LoaderInfo;
import flash.display.Sprite;
import flash.display.StageAlign;
import flash.display.StageScaleMode;
import flash.display.StageQuality;
import flash.events.Event;
import SimpleMenu;
public class Nav extends Sprite
{
private var simpleMenu:SimpleMenu;
private var cmd:int = -1;
private var menuXmlParser:XmlParser;
private var menuPath:String = "xml/menu.xml";
public var flashvars:Object = LoaderInfo(root.loaderInfo).parameters;
public function Nav():void
{
addEventListener(Event.ADDED_TO_STAGE, init);
}
public function init(e:Event):void
{
removeEventListener(Event.ADDED_TO_STAGE, init);
stage.scaleMode = StageScaleMode.NO_SCALE;
stage.quality = StageQuality.HIGH;
stage.align = StageAlign.TOP;
menuXmlParser = new XmlParser(menuPath);
menuXmlParser.addEventListener("xmlParsed", criarMenu);
}
public function criarMenu(e:Event):void
{
simpleMenu = new SimpleMenu;
simpleMenu.addEventListener("MENU_COMPLETED", positionMenu);
simpleMenu.criarMenu(menuXmlParser.myXML, 0, cmd - 1, 0);
// parameters: xml, offset, cmd, intro delay
addChild(simpleMenu);
}
private function positionMenu(e:Event):void
{
simpleMenu.x = (stage.stageWidth - simpleMenu.width)/2;
}
}
}
and the Home code that loads Nav:
Code:
private function init(e:Event):void
{
removeEventListener(Event.ADDED_TO_STAGE, init);
initContextMenu();
if(flashvars['cmd'])
navPath = navPath + "?cmd=" + flashvars['cmd'];
stage.align = StageAlign.TOP;
stage.scaleMode = StageScaleMode.NO_SCALE;
stage.quality = StageQuality.HIGH;
// navLoader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, loop);
navLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, doneNav);
navLoader.load(new URLRequest(navPath));
}
private function doneNav(e:Event):void
{
trace (navLoader.content); // [Object Nav]. fine!
nav = new Sprite();
nav = navLoader.content as Sprite;
trace (nav); // [Object Nav]. great!
trace (nav.width); // throws 1700. ( this swf has 1700 width. the actual width of nav.swf is 900. why ?
// navLoader = null;
thanks again man!
-
Just so you know, Nav is not a MovieClip. It is a Sprite. That's why your original cast using the as operator didn't work.
Why do you need nav to be a Sprite? If you never call Sprite-specific methods (like manipulating the graphics property), then just type it as DisplayObjectContainer so that you don't have to cast the loader content at all.
-
hey, i´ve never done that .thanks!
-
oops i´ve got this error:
5000: The class 'Nav' must subclass 'flash.display.MovieClip' since it is linked to a library symbol of that type.
Though it works as a sprite ...
and there´s a library symbol ( Menu Item) set to export to ActionScript, which is used by the simpleMenu class.
-
hey sorry , in Nav.swf there was a reference background with alpha set to 0 ... that´s why the width issue..
but now when I trace width it throws (0). because at this point , the menu had not initialized.
I´ve put that listener on the Nav to position the simpleMenu
Code:
public function criarMenu(e:Event):void
{
simpleMenu.addEventListener("MENU_COMPLETED", positionMenu);
simpleMenu.criarMenu(menuXmlParser.myXML, 0, cmd - 1, 0);
// parameters: xml, offset, cmd, intro delay
addChild(simpleMenu);
}
private function positionMenu(e:Event):void
{
simpleMenu.x = (stage.stageWidth - simpleMenu.width)/2;
}
now I need to set the same thing in the Home.swf.
The reason I have this nav.swf is cause I want to load it in a different swf, ( the other pages of the website , except for home).
But I guess now that I should load the simpleMenu directly in Home.swf , as well as the other swf for the other pages.
right ?
-
I don't know whether you need nav.swf or not. It depends on whether nav does anything for you that just loading the simpleMenu in Home would not. In this case, nav encapsulates the menu path and handles positioning, so you could make a case to keep it around.
Since you calculate the simpleMenu's position based on the stage width, you should be able to just put nav at (0, whatever) and the menu inside will be centered, without having to put the MENU_COMPLETED listener in Home.
I'm not sure what the deal is with the types. Stick with Sprite if that's working out.
Tags for this Thread
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
|