调度事件不会触发:(

时间:2011-05-19 10:01:10

标签: actionscript-3 movieclip stage dispatchevent

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代码。

这是我在maintimeline上加载SWF的代码。

addEventListener(Event.ADDED_TO_STAGE, init);

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

尝试调度工作,但它没有回到主要启动“返回root”。 有什么想法吗?

THX!

1 个答案:

答案 0 :(得分:1)

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

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
}