0

2 つの SWF (ローダーとビューアー、ドキュメント クラスも含む) をロードするメイン クラスがあります。もちろん、ローダーによって埋められ、ビューアによって表示されるコンテンツでダブルバッファを共有する必要があります

LocalConnection クラスを使用することを考えていましたが、PatrickS からの提案を受けて、シングルトン クラスの可能性を評価しています。私は AS でこのパターンを使用したことがなく、かなり偏見があることを告白しなければなりません。しかし、この特定のケースでは、それが役立つと思います。ところで、gskinner ブログの 2 実装例を読んで少し驚いたことがあります。ですから、この問題が Mac 対 PC のような終わりのない戦争であることを知って、私は本当にあなたの意見に感謝します

考慮事項: 1. ハイエンドの Windows PC で数か月間、24 時間 365 日実行されている AIR デスクトップ アプリケーション。ユーザー操作なし 2. ロードされるコンテンツはフル HD 画像であるため、高性能コードが必須

私のもう1つの懸念は、メモリリークに関するものです

前もって感謝します

4

1 に答える 1

1

I'd avoid a Singleton, personally, as its regularly implemented. However, having a single instance of this buffer object makes sense. So what I'd do in this case is have your Main class created this object. When the viewer is loaded, pass it this object. Then do the same for the loader. Now, both of your swfs share a common object that they could use to communicate. Then you can call functions on it and make it dispatch events if you want (extending EventDispatcher or implementing IEventDispatcher). Or you can just register callbacks if you want (should be a bit faster, but not sure if it will make a big difference).

Edit

Having a look at your other question I think your problem was related to getting the right loader and passing data to the loaded content. Here's an skecth of how you could implement what I mentioned above.

PhotoLoader.swf:

package  {
import flash.display.Sprite;
import flash.events.Event;

public class PhotoLoader extends Sprite {

    private var _commonObject:CommonObject;

    public function PhotoLoader() {

    }

    public function setCommonObject(commonObject:CommonObject):void {
        trace("PhotoLoader::setCommonObject", commonObject);
        _commonObject = commonObject;
        _commonObject.addEventListener(Event.INIT,handleInit);
    }

    private function handleInit(e:Event):void {
        trace("PhotoLoader::handleInit");
    }

}

} 

PhotoViewer.swf:

package  {
    import flash.display.Sprite;
    import flash.events.Event;

    public class PhotoViewer extends Sprite {

        private var _commonObject:CommonObject;

        public function PhotoViewer() {

        }

        public function setCommonObject(commonObject:CommonObject):void {
            trace("PhotoLoader::setCommonObject", commonObject);
            _commonObject = commonObject;
            _commonObject.addEventListener(Event.INIT,handleInit);
        }

        private function handleInit(e:Event):void {
            trace("PhotoViewer::handleInit");
        }

    }

}

Main.swf

package {
    import flash.display.Loader;
    import flash.display.LoaderInfo;
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.net.URLRequest;
    import flash.system.ApplicationDomain;
    import flash.system.LoaderContext;

    public class Main extends Sprite {

        private var _photoLoader:PhotoLoader;
        private var _photoViewer:PhotoViewer;
        private var _commonObject:CommonObject;

        private var _viewerLoader:Loader;
        private var _loaderLoader:Loader;

        private var _count:int = 0;

        public function Main():void {
            _commonObject = new CommonObject();
            loadSwfs();

        }

        private function loadSwfs():void {

            _viewerLoader = new Loader();
            _viewerLoader.contentLoaderInfo.addEventListener(Event.INIT,handleInit);
            _viewerLoader.load(new URLRequest("PhotoViewer.swf"));

            _loaderLoader = new Loader();
            _loaderLoader.contentLoaderInfo.addEventListener(Event.INIT,handleInit);
            _loaderLoader.load(new URLRequest("PhotoLoader.swf"));

        }

        private function handleInit(e:Event):void {
            trace("handleInit");
            var loader:Loader = (e.target as LoaderInfo).loader;
            switch(loader) {
                case _viewerLoader:
                    _photoViewer = _viewerLoader.content as PhotoViewer; 
                    _photoViewer.setCommonObject(_commonObject);
                    _count++;
                    break;
                case _loaderLoader:
                    _photoLoader = _loaderLoader.content as PhotoLoader; 
                    _photoLoader.setCommonObject(_commonObject);
                    _count++;
                    break;
            }
            if(_count == 2) { 
                _commonObject.init();
            }

        }


    }

}

And the common object, shared by the 3 swf:

package  {
    import flash.events.Event;
    import flash.events.EventDispatcher;

    public class CommonObject extends EventDispatcher {

        public function CommonObject() {

        }

        public function init():void {
            dispatchEvent(new Event(Event.INIT));
        }
    }

}

Basically when you load both your viewer and loader swfs you pass an instance of this common object. Then you register to listen for an INIT method: this will tell loader and viewer that everything has been setup. So at this point you can start sending messages from your viewer or loader to the other party (you could implement this through event dispatching); basically make a method in the common object to do it for you or just call dispatchEvent on the common object directly.

于 2010-08-14T23:49:52.963 に答える