【问题标题】:Embedding swf into flash builder--- "Type was not found"?将 swf 嵌入到 flash builder 中---“找不到类型”?
【发布时间】:2013-01-31 23:15:16
【问题描述】:

我正在使用 ActionScript 开发移动应用程序。

我正在导入 .swf 文件但出现错误:

Type was not found or was not a compile-time constant: MySwf.

这是我的代码:

package
{
import flash.display.Sprite;
import flash.display.StageAlign;

    public class gyyyyppp extends Sprite
 {
    [Embed(source='assets/g1.swf')] public static const MySwf : Class;

    public function gyyyyppp()
    {
        stage.align = StageAlign.TOP_LEFT;

        var p3:MySwf= new MySwf();

        addChild(p3);
    }
  }
}

我做错了什么?

(p.s. 我的 swf 文件是用非 ​​adobe 程序制作的) 我正在使用 Flash Builder

【问题讨论】:

    标签: flash actionscript air flash-builder


    【解决方案1】:

    我不认为你可以将嵌入类设置为变量类型,试试这个:

    var p3:MovieClip = new MySwf();
    

    【讨论】:

      【解决方案2】:

      我假设您希望能够调用嵌入式 SWF 的自定义方法。如果你只是想使用这个 SWF 中的图形,那么 @MickMalone1983 的答案就是你需要的,只是 SWF 的内容可能不是MovieClip 而是Sprite,例如,所以使用DisplayObject 更安全,输入:var p3:DisplayObject = new MySwf()

      调用自定义方法的问题在于,您无法引用在嵌入(或加载)SWF 中定义的类,因为编译器无法链接到此类。所以你必须编写一个接口,其中包含应该可以从外部访问的方法,然后

      1. 通过嵌入式SWF的主类实现该接口:

        public class MySwf extends Sprite implements MyInterface ...
        
      2. 使用Loader 对象实例化嵌入的 SWF 并将实例转换为相同的接口:

        [Embed(source='assets/g1.swf', mimeType='application/octet-stream')]
        public static const MySwfData : Class;
        
        public function load()
        {
            var loader:Loader = new Loader();
                loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onSwfLoaded);
                // we need the loaded code to be in the same (main) application domain
                loader.loadBytes(new MySwfData() as ByteArray, new LoaderContext(false, loaderInfo.applicationDomain));
        }
        
        private function onSwfLoaded(e:Event):void
        {
            var p3:MyInterface = (e.target as LoaderInfo).content as MyInterface;
                p3.myCustomMethod(); // myCustomMethod is defined in MyInterface
        
            addChild(p3 as DisplayObject);
        }
        

      这样,您将能够调用嵌入式 SWF 的自定义方法,因为它们将在嵌入式 SWF 和主应用程序中定义。

      另外,通常您在编译主应用程序时需要指定-static-link-runtime-shared-libraries=true 编译器标志。

      【讨论】:

      • 这次只需要简单的图形,但可能很快也需要自定义方法,谢谢!
      • +1 因为我不知道,我只是假设您必须使用 MovieClip,因为它是一个动态类,您可以引用未声明的成员而不会出现编译时错误,但随后必须类型检查等,这要好得多!
      猜你喜欢
      • 2014-06-15
      • 1970-01-01
      • 2012-02-27
      • 1970-01-01
      • 2014-07-18
      • 2015-08-05
      • 1970-01-01
      • 2014-07-04
      • 1970-01-01
      相关资源
      最近更新 更多