2011-08-23 88 views
0

我從來沒有做任何交叉腳本到現在爲止,我遇到了,在啓動時(可能是非常愚蠢的)錯誤的權利。ActionScript/Flash - 加載外部,以編程方式創建的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,其中有一個文檔類叫做CrossScriptTest創建一個新的ActionScript 3.0文件:

//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: Error #1009: Cannot access a property or method of a null object reference. at Square_fla::MainTimeline/frame1()

也許我誤解了n跨腳本或加載外部SWF文件的ATURE,但我只能似乎使這項工作,如果我手動繪製在舞臺上顯示對象,而不是如果外部SWF的顯示對象由代碼生成。

是無法加載的編程方式創建的外部swf文件,並將它們添加到主SWF的顯示列表?

回答

2

的解決方案總是在事後如此明顯。該解決方案是,當然,在構造函數中指定一個Event.ADDED_TO_STAGE事件偵聽器或初始化外部SWF的文檔類的方法。

在我的防守這個創建標準時(內部?)文件類被忽略,因爲它不要求也不常見。

主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; 
     } 
    } 
} 
+2

是的,在將DisplayObject添加到DisplayList之前,DisplayObject的stage屬性爲null。 –

0

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

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的根? (我不知道,因爲,爲了避免這種問題,我從不使用框架腳本)。爲了避免這種情況,您可以簡單地使用文檔類並將所有代碼放在那裏。

也許嘗試類似的東西,看看它的工作原理:

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

    } 

} 
+0

(???)我使用一個加載器。 – TheDarkIn1978

+0

哦,對不起,我沒有意識到,代碼框滾動,我沒有看到加載代碼。您是否在您的項目上啓用了調試模式?那會告訴你在哪條線上發生了錯誤。 –

+0

調試模式不起作用,因爲我正在加載從幀腳本生成的swf。它只是說「無法在此位置顯示源代碼」。和錯誤是一樣的。 – TheDarkIn1978