问题
I am developing mobile app in ActionScript.
Im importing .swf file but get error:
Type was not found or was not a compile-time constant: MySwf.
Here is my code:
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);
}
}
}
What I am doing wrong?
(p.s. my swf file is made with non-adobe program) I'm using Flash Builder
回答1:
I don't think you can set the embedded class as the variable type, try this:
var p3:MovieClip = new MySwf();
回答2:
I'm assuming that you want to be able to call custom methods of the embedded SWF. If you just want to use graphics from this SWF, then @MickMalone1983's answer is what you need, except that the contents of the SWF may be not MovieClip
but Sprite
, for example, so it's safer to use DisplayObject
type: var p3:DisplayObject = new MySwf()
.
The problem with calling custom methods is that you cannot refer to a class defined in an embedded (or loaded) SWF because compiler won't be able to link against this class. So you'll have to write an interface with methods that should be accessible from the outside, and then
implement this interface by the main class of the embedded SWF:
public class MySwf extends Sprite implements MyInterface ...
use
Loader
object to instantiate the embedded SWF and cast the instance to the same interface:[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); }
This way, you'll be able to call custom methods of the embedded SWF, because they will be defined both in the embedded SWF and in the main application.
Also, usually you'll want to specify -static-link-runtime-shared-libraries=true
compiler flag when compiling main application.
来源:https://stackoverflow.com/questions/14636906/embedding-swf-into-flash-builder-type-was-not-found