2011-03-28 89 views
0

我一直在搜索网页,我发现的所有代码都是播放具有时间轴的外部swf文件。我正在尝试加载的文件没有时间轴。我正在为这个项目使用Flixel框架,我想要播放的文件也是在Flixel中制作的(没有源文件只是swf文件)。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 [email protected] to flash.display.MovieClip.,所有我想做的是播放SWF文件的一组帧计数,然后移动到下一个状态。

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

回答

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; 

基本上,你标记你的符号用于出口,给它一个名称,并使用在嵌入代码。您只会嵌入该符号。

0

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

+0

我试过了,我得到这个错误:无法访问空对象引用的属性或方法。在DirtPileLogo()。 – WAC0020 2011-03-29 12:00:01

+0

@ WAC0020你在游戏中使用预加载器吗? – 2011-03-29 17:28:51

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... 
}