将swf嵌入flash构建器---“找不到类型”?

时间:2013-01-31 23:15:16

标签: flash actionscript air flash-builder

我正在使用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

2 个答案:

答案 0 :(得分:3)

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

var p3:MovieClip = new MySwf();

答案 1 :(得分:3)

我假设您希望能够调用嵌入式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);
    }
    
  3. 这样,您就可以调用嵌入式SWF的自定义方法,因为它们将在嵌入式SWF和主应用程序中定义。

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