【问题标题】:Flixel - How to load and play an embedded swf fileFlixel - 如何加载和播放嵌入式 swf 文件
【发布时间】:2011-03-28 15:42:11
【问题描述】:

我一直在网上搜索,我发现的所有代码都是播放具有时间线的外部 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 文件以设置帧数,然后进入下一个状态。

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

【问题讨论】:

    标签: actionscript-3 flash embedding playback


    【解决方案1】:

    尝试替换以下内容:

    [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;
    

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

    【讨论】:

      【解决方案2】:

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

      【讨论】:

      • 我试过了,我得到了这个错误:无法访问空对象引用的属性或方法。在 DirtPileLogo()。
      • @WAC0020 您是否在游戏中使用了预加载器?
      【解决方案3】:

      我相信您正在寻找的解决方案是使用 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...
      }
      

      【讨论】:

        猜你喜欢
        • 2010-10-10
        • 2018-06-15
        • 1970-01-01
        • 1970-01-01
        • 2013-11-21
        • 1970-01-01
        • 1970-01-01
        • 2012-12-08
        • 1970-01-01
        相关资源
        最近更新 更多