0
package com.fladev.background
{
    //import all classes
    import caurina.transitions.Tweener;
    import flash.display.Loader;
    import flash.display.LoaderInfo;
    import flash.display.MovieClip;
    import flash.display.Sprite;
    import flash.display.DisplayObject;
    import flash.display.StageAlign;
    import flash.display.StageDisplayState;
    import flash.display.StageScaleMode;
    import flash.events.Event;
    import flash.events.FullScreenEvent;
    import flash.events.MouseEvent;
    import flash.net.URLLoader;
    import flash.net.URLRequest;


public class MainClass extends Sprite 
{
    //create variables
    private var loaderMenu:Loader;
    private var loaderNames:Array = new Array ();
    private var loaderContents:Array = new Array ();
    private var loaderSlide:Loader;
    private var swfDisplayObject:DisplayObject;
    private var swfComObject:Object;
    private var xmlLoader:URLLoader = new URLLoader(); 
    private var xmlSlideLoader:URLLoader = new URLLoader();

    public function MainClass() 
    {
        stage.align = StageAlign.TOP_LEFT;
        stage.scaleMode = StageScaleMode.NO_SCALE;          
        stage.addEventListener(Event.RESIZE, stageResize);

        xmlLoader.addEventListener(Event.COMPLETE, showXML);
        xmlLoader.load(new URLRequest("navigation.xml"));

        //xmlSlideLoader.addEventListener(Event.COMPLETE, showSlideXML);
        //xmlSlideLoader.load(new URLRequest("slides.xml"));
    }

    function showXML(e:Event):void 
    {
        XML.ignoreWhitespace = true;

        var menuBtns:XML = new XML(e.target.data);
        var i:Number = 0;

        for ( i = 0; i < menuBtns.navItem.length(); i++ )
        {
            loaderMenu = new Loader();
            loaderMenu.name = menuBtns.navItem[i].name ;
            loaderMenu.load(new URLRequest(menuBtns.navItem[i].swfURL));
            loaderMenu.contentLoaderInfo.addEventListener(Event.COMPLETE, createSwfObjects);
        }
    }

    private function createSwfObjects(event:Event):void
    {
        var swfContent = event.currentTarget.content as MovieClip ;
        var swfName = event.currentTarget.loader ;

        navigationContainer.addChild(event.target.loader);
        showImage(swfContent);

        if ( swfName.name == 'topNavigation' )
        {
            swfContent.addEventListener("clickHandle",topNavigationClickHandler);
        }
    }

    private function topNavigationClickHandler():void
    {
        trace('Back to root');
    }

    private function showImage(navigationItem):void 
    {
        try
            {                       
                navigationItem.alpha = 0;
                Tweener.addTween(navigationItem, { alpha:1, time:1, transition:"easeOutSine" } );               
                navigationItem.smoothing = true;                        

            } catch (e:Error) { trace('Error no tweening'); };

        stageResize();
    }

    private function stageResize(e:Event=null):void
    {
        var centerImages:Array = new Array ( contentContainer, navigationContainer, backgroundImage ) ;

        backgroundImage.x = 0;
        backgroundImage.y = 0;
        backgroundImage.scaleX = backgroundImage.scaleY = 1;            

        if ((stage.stageHeight / stage.stageWidth) < backgroundImage.height / backgroundImage.width) {
            backgroundImage.width = stage.stageWidth;
            backgroundImage.scaleY = backgroundImage.scaleX;
        } else {
            backgroundImage.height = stage.stageHeight;             
            backgroundImage.scaleX = backgroundImage.scaleY;
        }

        for each ( var centered:MovieClip in centerImages )
        {
            centered.x = stage.stageWidth / 2 - centered.width / 2;
            centered.y = stage.stageHeight / 2 - centered.height / 2;   
        }
    }
}   
}

这是我的 main.as 代码。

这里是我在主时间线上加载的 SWF 的代码。

addEventListener(Event.ADDED_TO_STAGE, init);

function init(event:Event):void 
{
    trace('try dispatch');
    dispatchEvent(new Event("clickHandle",true));
}

尝试调度工作,但它不会回到主启动“回到根目录”。任何的想法?

谢谢!

4

1 回答 1

1

只要将所有动画剪辑调度事件都添加到显示列表中,这应该可以工作。这让我觉得可能正在添加的事件侦听器不起作用。尝试在代码块中添加跟踪语句,如下所示:

if ( swfName.name == 'topNavigation' )
{
     trace("adding listener");
     swfContent.addEventListener("clickHandle",topNavigationClickHandler);
}

我希望 if 条件失败,因此永远不会创建您的侦听器。此外,您需要在回调方法“topNavigationClickHandler”中添加一个函数参数,以接受该事件作为回调参数。您还没有这样做,这是在运行时接收到事件并将其分派到回调方法时抛出的错误。您还没有看到这一点,因为您的侦听器从未调用回调。所以你必须像这样修复这段代码:

private function topNavigationClickHandler(e:Event):void
{
    trace('Back to root');
}

另外,我只想补充一点,设置此侦听器的 if 条件似乎有点多余,因为您已经知道您正在期待导航 swf,因为您正在显式加载它。另外我不相信 name 属性会这样设置。通常,该名称仅在编译之前在 IDE 中设置,如果没有,它会在运行时动态生成。可能更有用的是检查加载的 SWF 的 URL 以查看它是否包含“topNavigation”或任何 swf 名称。你可以这样做:

var swfUrl:String = myLoader.contentLoaderInfo.url;
if (swfUrl.search("topNavigation") != -1){
   //Match found, add listener for navigation
}
于 2011-05-19T10:50:08.747 回答