簡體   English   中英

ActionScript / Flash - 加載外部,以編程方式創建的SWF?

[英]ActionScript / Flash - Loading External, Programatically Created SWF?

到目前為止我從來沒有做過任何交叉腳本,而且我在開始時遇到了(可能是非常愚蠢的)錯誤。

外部SWF:我在Flash Professional CS5中創建了一個新的ActionScript 3.0項目。 在第一幀我添加了以下腳本:

//Square.fla frame script

import flash.display.Shape;
import flash.events.Event;

var s:Shape = new Shape();
s.graphics.beginFill(0x0000FF, 1.0);
s.graphics.drawRect(-100, -100, 200, 200);
s.graphics.endFill();

s.x = stage.stageWidth / 2;
s.y = stage.stageHeight / 2;

addChild(s);

addEventListener(Event.ENTER_FRAME, enterFrameEventHandler);

function enterFrameEventHandler(evt:Event):void
{
    s.rotation += 2;
}

保存,編譯,完成。 這可以作為一個獨立的swf工作,它只是在中心舞台上顯示一個旋轉的藍色方塊。

主要SWF:我在Flash Professional CS5中創建了一個新的ActionScript 3.0文件,該文件有一個名為CrossScriptTest的文檔類:

//CrossScriptTest.as

package
{
//Imports
import flash.display.StageScaleMode;
import flash.display.StageAlign;
import flash.display.Sprite
import flash.display.Loader;
import flash.events.IOErrorEvent;
import flash.net.URLRequest;
import flash.events.Event;

//Class
[SWF(width = "1000", height = "500", backgroundColor = "0x444444")]
public class CrossScriptTest extends Sprite
{
    //Constants
    private static const SQUARE_SWF_URL:String = "Square.swf";

    //Variables
    private var SWFLoader:Loader;

    //Constructor
    public function CrossScriptTest()
    {
        stage.scaleMode = StageScaleMode.NO_SCALE;
        stage.align = StageAlign.TOP_LEFT;
        stage.frameRate = 60;

        init();
    }

    //Initialize
    private function init():void
    {
        SWFLoader = new Loader();
        SWFLoader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, IOErrorEventHandler);
        SWFLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, loaderCompleteEventHandler);
        SWFLoader.load(new URLRequest(SQUARE_SWF_URL));
    }

    //IOError Event Handler
    private function IOErrorEventHandler(evt:IOErrorEvent):void
    {
        trace(evt);
    }

    //Loader Complete Event Handler
    private function loaderCompleteEventHandler(evt:Event):void
    {
        evt.currentTarget.removeEventListener(IOErrorEvent.IO_ERROR, IOErrorEventHandler);
        evt.currentTarget.removeEventListener(Event.COMPLETE, loaderCompleteEventHandler);

        var squareSWF:Sprite = Sprite(evt.currentTarget.content);
        addChild(squareSWF);
    }
}
}

錯誤:我收到以下錯誤:

TypeError:錯誤#1009:無法訪問空對象引用的屬性或方法。 在Square_fla :: MainTimeline / frame1()

也許我誤解了交叉腳本或加載外部swf文件的本質,但是如果我在舞台上手動繪制顯示對象而不是外部swf的顯示對象是由代碼生成的話,我似乎只能使這個工作。

是不是可以加載以編程方式創建的外部swfs並將它們添加到主swf的顯示列表中?

事后,解決方案總是如此明顯。 當然,解決方案是在外部swf文檔類的構造函數或初始化方法中分配Event.ADDED_TO_STAGE事件偵聽器。

在我的辯護中,這被忽略了,因為在創建標准(內部?)文檔類時不需要也不常見。

主SWF的文件類別:

package
{
    //Imports
    import flash.display.StageScaleMode;
    import flash.display.StageAlign;
    import flash.display.Sprite
    import flash.display.Loader;
    import flash.events.IOErrorEvent;
    import flash.net.URLRequest;
    import flash.events.Event;

    //Class
    [SWF(width = "1000", height = "500", backgroundColor = "0x444444")]
    public class CrossScriptTest extends Sprite
    {
        //Constants
        private static const SQUARE_SWF_URL:String = "Square.swf";

        //Variables
        private var swfLoader:Loader;

        //Constructor
        public function CrossScriptTest()
        {
            stage.scaleMode = StageScaleMode.NO_SCALE;
            stage.align = StageAlign.TOP_LEFT;
            stage.frameRate = 60;

            init();
        }

        //Initialize
        private function init():void
        {
            swfLoader = new Loader();
            swfLoader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, IOErrorEventHandler);
            swfLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, loaderCompleteEventHandler);
            swfLoader.load(new URLRequest(SQUARE_SWF_URL));
        }

        //IOError Event Handler
        private function IOErrorEventHandler(evt:IOErrorEvent):void
        {
            trace(evt);
        }

        //Loader Complete Event Handler
        private function loaderCompleteEventHandler(evt:Event):void
        {
            evt.currentTarget.removeEventListener(IOErrorEvent.IO_ERROR, IOErrorEventHandler);
            evt.currentTarget.removeEventListener(Event.COMPLETE, loaderCompleteEventHandler);

            addChild(evt.currentTarget.content);
        }
    }
}

外部SWF的文件類別:

package
{
    //Imports
    import flash.display.Sprite;
    import flash.display.Shape;
    import flash.events.Event;

    //Class
    public class Square extends Sprite
    {
        //Constructor
        public function Square()
        {
            init();
        }

        //Initialize
        private function init():void
        {
            addEventListener(Event.ADDED_TO_STAGE, addedToStageEventHandler);
        }

        //Added To Stage Event Handler
        private function addedToStageEventHandler(evt:Event):void
        {
            removeEventListener(Event.ADDED_TO_STAGE, addedToStageEventHandler);

            var s:Shape = new Shape();
            s.graphics.beginFill(0x0000FF, 1.0);
            s.graphics.drawRect(-100, -100, 200, 200);
            s.graphics.endFill();

            s.x = stage.stageWidth / 2;
            s.y = stage.stageHeight / 2;

            addChild(s);

            s.addEventListener(Event.ENTER_FRAME, enterFrameEventHandler);
        }

        //Enter Frame Event Handler
        function enterFrameEventHandler(evt:Event):void
        {
            Shape(evt.currentTarget).rotation += 2;
        }
    }
}

是的,它可以做到,但你這樣做的方式似乎並不正確。 你可以簡單地使用一個加載器:

var loader:Loader = new Loader();
loader.contentLoaderInfo.addEventListener("complete", loader_complete);
loader.load(new URLRequest("yourfile.swf"));

function loader_complete(event:*):void {
    var loadedObject:* = event.target;
    // Access the properties of the loaded SWF here
}

編輯:

通常,盡量不要使用框架腳本,尤其是在SWF中加載SWF時,因為它們的工作方式並不總是很明顯。 例如,我不知道你的腳本中的root是什么 - 它是你的SWF的根還是父SWF的根? (我不知道因為,為了避免這種問題,我從不使用框架腳本)。 為避免這種情況,您只需使用文檔類並將所有代碼放在那里。

也許嘗試類似的東西,看看它是否有效:

package some.unique.path {

    import flash.display.MovieClip;
    import flash.display.Shape;
    import flash.events.Event;

    public class YourClass extends MovieClip {

        public function YourClass():void {
            var s:Shape = new Shape();
            s.graphics.beginFill(0x0000FF, 1.0);
            s.graphics.drawRect(-100, -100, 200, 200);
            s.graphics.endFill();

            s.x = stage.stageWidth / 2;
            s.y = stage.stageHeight / 2;

            addChild(s);

            addEventListener(Event.ENTER_FRAME, enterFrameEventHandler);
        }

        function enterFrameEventHandler(evt:Event):void
        {
            s.rotation += 2;
        }

    }

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM