开发者

flash as3 loader question

What I am trying to do is load 2 different swf's using 2 different buttons.

What I want to happen is when you click on button 1 it loads the first swf and button 2 loads the second swf removing any other 开发者_如何学Pythonswf from the stage first.

The problem I seem to be running into is with the loader. I cannot seem to load the images into the loader without putting them on the stage. And when I try to load the images dynamically it keeps on recreating the swf's by placing another one in the loader even though I am using :

stage.removeChild(loader);
loader = new Loader();. 

Any help or tutorials on this information would be great.


I would use separate Loader's for each image you want to load. here's a quick example of how you could implement it:

*edit:It's cutting off the package part, please forgive me for not understanding stackoverflow's code parser. *

`package
{ import flash.display.Loader; import flash.display.Sprite; import flash.events.Event; import flash.events.MouseEvent; import flash.net.URLRequest;

public class LoaderTest extends Sprite
{
    //two loaders
    private var _firstLoader:Loader = new  Loader();
    private var _secondLoader:Loader = new  Loader();

    //just assuming you already have the buttons you want setup, use these as theoretical buttons
    private var _buttonOne:Sprite;
    private var _buttonTwo:Sprite;

    public function LoaderTest() 
    {
        _firstLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, imageLoaded);
        _secondLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, imageLoaded);  
        _firstLoader.load(new URLRequest("path/to/image.jpg"));
        _secondLoader.load(new URLRequest("path/to/image.jpg"));

        _buttonOne.addEventListener(MouseEvent.CLICK, showImage);
        _buttonTwo.addEventListener(MouseEvent.CLICK, showImage);
    }

    private function imageLoaded(e:Event):void
    {
        //do something if you want
    }

    private function showImage(e:MouseEvent):void
    {
        switch(e.target)
        {
            case _buttonOne :
                if (!contains(_firstLoader))
                {
                    if (contains(_secondLoader))
                        removeChild(_secondLoader);

                        addChild(_firstLoader);
                }
            break;
            case _buttonTwo :
                if (!contains(_secondLoader))
                {
                    if (contains(_firstLoader))
                        removeChild(_firstLoader);

                        addChild(_secondLoader);
                }
            break;              
        }
    }


}

} `

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜