开发者

Dispatching event doesn't fire :(

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;   
        }
    }
}   
}

This is my code for the main.as.

And here my code for my loaded SWF on the maintimeline.

addEventListener(Event.ADDED_TO_STAGE, init);

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

Try dispatch works, but it does not get back to the main to fire up "Back to root". Any idea?

thx!


As long as all movieclips dispatching events are added to the display list, this should work. This makes me think that perhaps the event listener being added is not working. Try adding a trace statement to the block of code as shown below:

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

I expect that this if condition is failing, and thus your listener is never being created. Also, you need to add a function parameter to the callback method "topNavigationClickHandler" to accept the event as the callback parameter. You have not done this, and this is an error that would be thrown at runtime when the event was received and dispatched to the callback method. You havn't seen this yet because your listener has never had to invoke the callback. So you're gonna have to fix this code like so:

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

Also I just want to add that your if condition on setting this listener seems a bit redundant, since you already know you are expecting the navigation swf, because you're explicitly loading it. Also I don't believe the name property would be set like this. Typically the name is only set inside the IDE before compilation, and if it isn't, it gets dynamically generated at runtime. What might be more useful is to check the URL of the loaded SWF to see if it contains "topNavigation" or whatever the swf name is. You can do this like so:

var swfUrl:String = myLoader.contentLoaderInfo.url;
if (swfUrl.search("topNavigation") != -1){
   //Match found, add listener for navigation
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜