AS3加载swf作为Movieclip

时间:2013-06-02 10:31:26

标签: actionscript-3 flash load movieclip flashdevelop

我需要加载外部swf并能够将其用作FlashDevelop中的Movieclip,即我需要能够转到特定的关键帧,启动和停止播放等等。一些简单的工作示例代码将非常感激因为我无法通过谷歌找到任何令人满意的教程。

修改 的 我现在有了这段代码

package 
{
import flash.net.*;
import flash.display.*;
import flash.events.*;
import flash.utils.getQualifiedClassName;

public class Main extends MovieClip 
{

    var animatedBox:MovieClip = new MovieClip();
    var ldr:Loader = new Loader();
    var frames:int = 0;

    public function Main():void 
    {
        if (stage) init();
        else addEventListener(Event.ADDED_TO_STAGE, init);
    }

    private function init(e:Event = null):void 
    {
        removeEventListener(Event.ADDED_TO_STAGE, init);
        ldr.contentLoaderInfo.addEventListener(Event.COMPLETE, onload);
        ldr.load(new URLRequest("../lib/test.swf"));
    }

    function onload(e:Event)
    {
        if ( !e.target )
        return;

        if( e.target.content is MovieClip )
        {
            animatedBox = e.target.content as MovieClip;

            animatedBox.gotoAndPlay("Start");
        }
        else
        {
            trace( getQualifiedClassName( e.target.content ) );
        }
    }

}

}

尝试运行后,我得到[Fault]异常,information = TypeError:错误#1009:无法访问空对象引用的属性或方法。 有什么想法吗?

1 个答案:

答案 0 :(得分:2)

import flash.utils.getQualifiedClassName;

var mc: MovieClip;

var ldr: Loader = new Loader();
ldr.contentLoaderInfo.addEventListener( Event.COMPLETE, onLoad );
ldr.load( new URLRequest("your.swf") );


function onLoad( e:Event ):void 
{
    if( !e.target )
        return;

    trace( getQualifiedClassName( e.target.content ) );
    /* if you get: flash.display::AVM1Movie
       it means you are trying to load an AS1 or AS2 SWF
       into AS3 SWF. They both need to be AS3 */

    mc = e.target.content as MovieClip;
    mc.gotoAndPlay( 2 );    
    // or  mc.gotoAndPlay( 'yourLabel' ); 

}