Flixel - 如何加载和播放嵌入的swf文件

时间:2011-03-28 15:42:12

标签: actionscript-3 flash embedding playback

我一直在网上搜索,我找到的所有代码都是播放具有时间轴的外部swf文件。我尝试加载的文件没有时间轴。我正在为这个项目使用Flixel框架,我想要播放的文件也是在Flixel中制作的(没有源文件只是swf文件)。

我所拥有的大部分代码都来自于Flixel论坛上的过场动画模板。以下是我到目前为止的情况:

package  
{
import org.flixel.FlxState;
import org.flixel.FlxG;
import flash.display.MovieClip;
import flash.media.SoundMixer;
import flash.events.Event;

public class SponsorsState extends FlxState 
{

    //Embed the cutscene swf relative to the root of the Flixel project here
    [Embed(source='assets/DirtPileLogo.swf', mimeType='application/octet-stream')] private var SwfClass:Class;   
    //This is the MovieClip container for your cutscene
    private var movie:MovieClip;
    //This is the length of the cutscene in frames
    private var length:Number;

    override public function create():void
    {
        movie = new SwfClass();
        //Set your zoom factor of the FlxGame here (default is 2)
        var zoomFactor:int = 2;
        movie.scaleX = 1.0/zoomFactor;
        movie.scaleY = 1.0 / zoomFactor;
        //Add the MovieClip container to the FlxState
        addChildAt(movie, 0);
        //Set the length of the cutscene here (frames)
        length = 100;
        //Adds a listener to the cutscene to call next() after each frame.
        movie.addEventListener(Event.EXIT_FRAME, next);
    }
    private function next(e:Event):void
    {
        //After each frame, length decreases by one
        length--;
        //Length is 0 at the end of the movie
        if (length <= 0)
        {
            //Removes the listener
            movie.removeEventListener(Event.EXIT_FRAME, next);              
            //Stops all overlaying sounds before state switch
            SoundMixer.stopAll();
            //Enter the next FlxState to switch to
            FlxG.state = new PlayState();
        }           
    }

}

}

当我运行此操作时,我收到此错误:Type Coercion failed: cannot convert SponsorsState_SwfClass@fb5161 to flash.display.MovieClip.,我想要做的就是播放swf文件以获得设定的帧数,然后转到下一个状态。

关于如何做到这一点的任何想法?

3 个答案:

答案 0 :(得分:0)

尝试替换以下内容:

[Embed(source='assets/DirtPileLogo.swf', mimeType='application/octet-stream')] 
private var   SwfClass:Class;   
//This is the MovieClip container for your cutscene
private var movie:MovieClip;

//Mark your symbol for export and name it => MyExportedSymbol
[Embed(source='assets/DirtPileLogo.swf', symbol = "MyExportedSymbol")] 
private var   SwfSymbol:Class;   
//Make sure that MyExportedSymbol base class is MovieClip
private var movie:MovieClip = new SwfSymbol;

基本上,您将符号标记为导出,为其命名并在嵌入代码中使用该符号。您只会嵌入该符号。

答案 1 :(得分:0)

您在嵌入时错误地设置了mimeType。删除mimeType,它应该正常工作。有关详细信息,请参阅embedding assets上的文档。

答案 2 :(得分:0)

我相信您正在寻找的解决方案是使用Loader类。

[Embed (source = "assets/DirtPileLogo.swf", mimeType = "application/octet-stream")]
private var content:Class;

private var loader:Loader;      

public function Main():void 
{
    var data:ByteArray = new content();
    loader = new Loader();
    addChild( loader  );
    loader.loadBytes( data, new LoaderContext(false, new ApplicationDomain() ) );   
    // ... add listener to loader if necessary, etc...
}