在 Flex / Actionscript 中加载并播放嵌入的 SWF 文件

发布于 2024-07-14 13:05:02 字数 1138 浏览 15 评论 0原文

我正在尝试在我的 Flex 应用程序中创建/使用预加载器。 预加载器是一个 SWF 文件,有 100 帧(加载器进度的每百分比 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]

请帮忙!

I'm trying to create / use a pre-loader in my flex application. The preloader is a SWF file which has 100 frames (1 for every percent of the loader progress). Basically I am trying to Embed this SWF file in my application, display it on screen and change the frame number being displayed as the progress completes.

The code I have so far is (which extends 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();
}

The error I am getting is:

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]

Please help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

欢你一世 2024-07-21 13:05:02

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

[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();
}

Hope this work
I tried this.

[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();
}
野生奥特曼 2024-07-21 13:05:02

您需要有一个实现 IUIComponentMovieClip 包装器,以便能够传递给 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);
   }
}

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

You need to have a wrapper over MovieClip that implements the IUIComponent in order to be able to pass to addChild(). From the addChild() documentation:

Note: While the child argument to the method is specified as of type DisplayObject, the argument must implement the IUIComponent interface to be added as a child of a container. All Flex components implement this interface.

You will need something like this:

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

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

      // add your own magic

      addChild (mc);
   }
}

Warning: Untested code, should give you an idea only!

苄①跕圉湢 2024-07-21 13:05:02

使用 sprite 而不是 Canvas 作为基类。 这样做的两个原因:

  1. Canvas 有很多依赖项(调整到 100k+ 的 Flex 组件)。 您不想在显示预加载器之前等待所有这些加载完毕

  2. Canvas 是 UIComponent 容器。 当您想要布局 UIComponent 时使用它。 在您的情况下,您不需要复杂的画布布局逻辑 - 您只需要显示一个 MovieClip。 所以不要使用canvas。

为了回答您原来的问题,SWFLoader 和 Image 是知道如何显示位图和影片剪辑的 UIComponent。 做这样的事情:

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

Use sprite instead of Canvas as a base class. Two reasons to do this:

  1. Canvas has a lot of dependencies (to the tune of 100k+ of flex components). You don't want to wait for all this to load before displaying your preloader

  2. Canvas is UIComponent container. Use it when you want to lay out UIComponents. In your case, you do not need complicated canvas layout logic - you just need to display a MovieClip. So don't use a canvas.

To answer your original question, SWFLoader and Image are UIComponents that know how to display Bitmaps and MovieClips. Do something like this instead:

var img:Image = new Image();
img.source = _preloader;
this.addChild(img);
孤独患者 2024-07-21 13:05:02

查看 Preloader 类和Application 类的 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.net/index.cfm/2008/1/17/Flex- NotSo-自定义预加载器

Have a look at the the Preloader class and the preloader property of the Application class.

As the documentation says, you definitely shouldn't extend the Flex UIComponent (or Image or SWFLoader) classes for a preloader.

Here are a few examples of how to go about customising the preloader:

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

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

http://www.webapper.net/index.cfm/2008/1/17/Flex-NotSo-Custom-Preloader

浪漫人生路 2024-07-21 13:05:02

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

i found custom preloader using external swf code from this link
http://askmeflash.com/article_m.php?p=article&id=7

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文