我正在尝试在我的flex应用程序中创建/使用预加载器。预加载器是一个SWF文件,有100帧(每1%的加载程序进度为1)。基本上我试图在我的应用程序中嵌入这个SWF文件,在屏幕上显示它并在进度完成时更改显示的帧编号。

到目前为止我的代码是(扩展了Canvas):

[Embed("/../assets/preLoader.swf")]
private var SWFClass:Class;

private var _preLoader:MovieClip;

private var _progress:Number;

public function set progress(value:Number) : void {
    _progress = value;

    if(progress < 100) {
        _preLoader.gotoAndPlay(progress, null);
    }else {
        _preLoader.gotoAndStop(0, null);
    }
}   

[Bindable]
public function get progress() : Number {
    return _progress;
}



(Called on creationComplete event)          
private function init() : void {
    _preLoader = MovieClip(new SWFClass());

    this.addChild(_preLoader);

    _preLoader.play();
}

我得到的错误是:

TypeError: Error #1034: Type Coercion failed: cannot convert widgets::PreLoader_SWFClass@30b3be51 to mx.core.IUIComponent.at mx.core::Container/http://www.adobe.com/2006/flex/mx/internal::addingChild()[C:\autobuild\3.2.0\frameworks\projects\framework\src\mx\core\Container.as:3259]

请帮忙!

有帮助吗?

解决方案

使用sprite代替Canvas作为基类。这样做有两个原因:

  1. Canvas有很多依赖项(对于100k +灵活组件的调整)。在显示预加载器

  2. 之前,您不希望等待所有这些加载
  3. Canvas是UIComponent容器。当您想要布局UIComponents时使用它。在您的情况下,您不需要复杂的画布布局逻辑 - 您只需要显示MovieClip。所以不要使用画布。

  4. 要回答您的原始问题,SWFLoader和Image是知道如何显示位图和MovieClip的UIComponents。做这样的事情:

    var img:Image = new Image();
    img.source = _preloader;
    this.addChild(img);
    

其他提示

您需要在 MovieClip 上有一个包装器,它实现 IUIComponent ,以便能够传递给 addChild()。来自 addChild() 文档

  

注意:虽然方法的子参数指定为DisplayObject类型,但参数必须实现IUIComponent接口才能添加为容器的子级。所有Flex组件都实现了此接口。

你需要这样的东西:

public class MovieClipUIComponent extends UIComponent {
   public function MovieClipUIComponent (mc:MovieClip) {
      super ();

      mcHeight = mc.height;
      mcWidth = mc.width;

      // add your own magic

      addChild (mc);
   }
}

警告:未经测试的代码,应该只给你一个想法!

希望这项工作 我试过这个。

[Embed(source="assets/yourSWF.swf", mimeType="application/octet-stream")]
public var SWF:Class;

_swfLoader = new Loader();
//nothing to do onComplete or onProgress method just to debug
// Add complete event listener
_swfLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, onComplete);
// Add progress event listener
_swfLoader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, onProgress);


// Add error event listener. Critical if you don't want run time errors if there
// are problems loading the file.
_swfLoader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, onError);

// Incase of loading Flex. Very important.
_swfLoader.addEventListener("mx.managers.SystemManager.isBootstrapRoot", systemManagerHandler);
_swfLoader.addEventListener("mx.managers.SystemManager.isStageRoot", systemManagerHandler);

// Load on the loader with a new URLRequest instance passing the path to
// it's constructor.
_swfLoader.loadBytes(new SWF());

// We have to addd the loader so it creation is done.
addChild(_swfLoader);

private function systemManagerHandler(e:Event):void {
            // Prevent default stops default behaviour here and thus stops some potential
            // run time errors.         
            e.preventDefault();
}

查看预加载器类和应用程序的 preloader 属性类。

正如文档所说,你绝对不应该为预加载器扩展Flex UIComponent(或Image或SWFLoader)类。

以下是一些如何自定义预加载器的示例:

http: //www.pathf.com/blogs/2008/08/custom-flex-3-lightweight-preloader-with-source-code/

http://groups.adobe.com/posts/15d371c71d

http://www.webapper达网络/ index.cfm / 2008/1/17 / Flex的NotSo-定制预载

我发现使用此链接的外部swf代码的自定义预加载器
http://askmeflash.com/article_m.php?p=article&id=7

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top