2013-03-29 66 views
0

我正在編寫一個教育軟件,它利用單個「應用程序」swf加載外部「主題」swf文件(包含特定於該主題的庫項目) 。這些外部「主題」swf文件中的每一個還使用自定義類來解決特定於該主題的計算(例如trigonometry.swf主題文件的三角類)。actionscript 3.0從加載的swf的自定義類中引用主swf的自定義類

我希望trigonometry.swf的三角類能夠引用存在於主「應用程序」swf中的自定義類方法。如果有助於瞭解上下文,我希望使用三角函數的用戶能夠輸入相信是正確的三角函數(sin,cos或tan)和「檢查」以查看它們是否正確。 「indication_right_wrong」方法存在於主「應用程序」swf的「grader」自定義類中(以便我對此類所做的任何更改都將在所有主題中更新)。所以...我如何從外部加載的swf文件的自定義類中引用主swf的自定義類方法?

當我將「grader」自定義類導入每個「主題」swf並直接引用它時,我確實有工作結果,但是如果我進行更改,我不想重新發布每個主題swf到「分級」習俗班。因此,我試圖讓每個主題能夠引用「grader」自定義類,當它存在於主「應用程序」swf中時。

我嘗試使用parent.parent的變體時得到的錯誤是property grader_class not found on flash.display.Stage(對於父級)和Cannot access a property or method of a null object reference.(當使用parent.parent時)。

感謝您提供任何幫助。

+0

您應該能夠通過ApplicationDomain中的[getDefinition方法()來訪問父類(http://help.adobe.com/en_US/FlashPlatform/reference/actionscript /3/flash/system/ApplicationDomain.html#getDefinition())方法。類似於:'yourMasterSWFReference.loaderInfo.applicationDomain.getDefinition(「the.qualified.ClassYouNeed」)'。另一個選項,也許更清潔是你的加載的SWF調度一個事件或一個信號與檢查/測試所需的數據和主瑞士法郎監聽,並在處理程序處理請求和調度結果。 –

回答

1

您無法編譯調用正在編譯的代碼中找不到的方法的代碼。但是,您可以通過評估方法進行運行時調用。例如:

stage["myfunction"](args); 

不過,雖然你可以建立某種形式的尋路邏輯來引用您的方法,爲George Profenza表明,你的成功將是更高的(和更容易使用),如果你簡單地傳遞一個函數監聽器到派遣的事件或方法調用。

例如,我們假設您有一個自定義的CHECK_ANSWER事件,當用戶輸入答案時觸發。您的gradeAnswer()函數(存在於您的「application.swf」中)需要在加載子「trigonometry.swf」後註冊該事件。隨後,數據將通過。

但是,這可能並不理想,因爲它可能會導致子SWF無法正確從內存中卸載,因爲父對子對象的註冊(必須先刪除所有註冊)。

就個人而言,我建議創建一個靜態類作爲您的應用程序的目錄。每個swf都會加載這個類,並根據需要列出它們的相關方法。例如:

package com.atriace { 
    public class Directory { 
     public static var methods:Object = { 

     } 
    } 
} 

當application.swf加載時,它會使用您的grading函數填充方法對象。隨後的子swf也會加載這個靜態類,並期望找到你的等級方法預填充。

Application.swf

import com.atriace.Directory; 
Directory.methods["grade"] = gradeAnswer; 

兒童。SWF

import com.atriace.Directory; 
Directory.methods["grade"](answers); 
+0

好戲。以前從未這樣嘗試過,所以今天我學到了一些新東西。因此,我對你的答案進行了投票。 –

0

不知道安靜是因爲這樣的問題已經在堆棧溢出了很多次回答。反正這裏是我對吧..

我希望用戶對三角合作,能夠在Trig 功能鍵入認爲是正確的(正弦,餘弦,或棕褐色)和「檢查」 看他們是否正確。

我假設,因爲他們必須輸入答案,你有一個輸入文本框?當他們回答時,他們按下一個按鈕來「提交」他們的答案?假設我在右邊線我們可以做到這樣......

注:answer_txt(串)=用戶,input_txt =文本字段用戶輸入的外部類給出答案

在主類中有類似

import flash.display.Stage; 
import flash.display.MovieClip; 
import flash.events.*; 

import ExternalClass; //your class file attached to relevant topic swf 

public class Main extends MovieClip 
{ 
    private var _extClass = new ExternalClass(); //create reference to external Class 

    public function Main() 
    { 
    //Ref 1 means do that function when trigger event received from external class 
    stage.addChild(_extClass); //gives access to "stage" for ExternalClass.as 
    _extClass.addEventListener("Check_Answer", indicate_right_wrong); //see Ref 1 
    } 

private function indicate_right_wrong (e:Event) 
{ 
//your answer checking code. 
//Access answer_txt variable from external class like example below 
//_extClass.answer_txt.text = " "; 
} 

現在在外部類(Trigonometry.as ??)中可以設置類似這樣的東西。確保answer_txt存在(public var answer_txt:String;)..

public function Question_1() : void 
{ 
//your code for question + set submit button 
submit_BTN.addEventListener(MouseEvent.CLICK, send_Answer); 
} 

function send_Answer(e:MouseEvent):void 
{ 
answer_txt = input_txt.text; //set input text as final answer 
trace("final answer is..." + answer_txt); //just to test it's there 

dispatchEvent (new Event ("Check_Answer")); //Main class is waiting for this event (ref 1) 
trace ("event dispatched.." + dispatchEvent); //to confirm it was despatched 
} 
+0

Doh ..我現在有點更清醒了,我看到George Profenza和Atriace基本上說了同樣的事情(下顎關於「Check_Answer as event」的建議)。我的答案顯示了他們的意思的示例代碼。 Atriace也可能會做些什麼。明智地聆聽並感謝您的最佳幫助。和平 –