简体   繁体   English

从已加载的swf的自定义类引用主swf的自定义类的actionscript 3.0

[英]actionscript 3.0 referencing a custom class of a main swf from a loaded swf's custom class

I am writing educational software that utilizes a single "application" swf, which loads external "topic" swf files (containing library items specific to the topic). 我正在编写利用单个“应用程序” swf加载外部“主题” swf文件(包含特定于该主题的库项目)的教育软件。 Each of these external "topic" swf files also utilizes custom classes that solve calculations specific to the topic (ex. trigonometry class for the trigonometry.swf topic file). 这些外部“主题” swf文件中的每个文件都还利用自定义类来解决特定于该主题的计算(例如trigonometry.swf主题文件的trigonometry类)。

I want the trigonometry class of the trigonometry.swf to be able to reference a custom class method that exists in the main "application" swf. 我希望trigonometry.swf的trigonometry类能够引用主“应用程序” swf中存在的自定义类方法。 If it helps to know the context, I want the user working on trigonometry to be able to type in the trig function the believe to be correct (sin, cos, or tan) and "check" to see if they are right. 如果它有助于了解上下文,我希望使用三角函数的用户能够键入触发函数,认为是正确的(正弦,余弦或棕褐色),然后“检查”以查看它们是否正确。 The "indicate_right_wrong" method exists in a "grader" custom class of the main "application" swf (so that any changes I make to this class will be updated throughout all topics). “ indicate_right_wrong”方法存在于主“应用程序” swf的“ grader”自定义类中(这样,我对此类所做的任何更改都会在所有主题中进行更新)。 So...how do I reference a custom class method of a main swf FROM an externally loaded swf file's own custom class? 因此...如何从外部加载的SWF文件自己的自定义类引用主SWF的自定义类方法?

I DO have the result working when I import the "grader" custom class into each "topic" swf and reference it directly, but I don't want to have to re-publish each topic swf if I make a change to the "grader" custom class. 当我将“ grader”自定义类导入每个“ topic” swf并直接引用它时,结果确实可以正常工作,但是如果我对“ grader”进行了更改,则不必重新发布每个主题swf ”自定义类。 Thus, I'm trying to have each topic capable of referencing the "grader" custom class when it exists within the main "application" swf. 因此,我试图使每个主题都能够引用主“应用程序” swf中存在的“ grader”自定义类。

The error I get when I try using variations of parent or parent.parent are property grader_class not found on flash.display.Stage (for parent) and Cannot access a property or method of a null object reference. 我尝试使用parent或parent.parent的变体时遇到的错误是property grader_class not found on flash.display.Stage (对于parent) property grader_class not found on flash.display.Stage ,并且Cannot access a property or method of a null object reference. (when using parent.parent). (使用parent.parent时)。

Thank you for any help you can provide. 感谢您提供任何帮助。

You can't compile code that calls methods not found in the code being compiled. 您不能编译调用在正在编译的代码中找不到的方法的代码。 However, you can make runtime calls by evaluating a method. 但是,您可以通过评估方法来进行运行时调用。 For example: 例如:

stage["myfunction"](args);

However, while you could create some sort of pathing logic to reference your methods, as George Profenza indicated, your success will be much higher (and easier to work with) if you simply pass a function listener to a dispatched event or method call. 但是,尽管您可以创建某种路径逻辑来引用您的方法,如George Profenza所指出的,但是,只要将函数侦听器传递给分派的事件或方法调用,您的成功将更高(并且更容易使用)。

For example, let's assume you have a custom CHECK_ANSWER event that fires when the user enters an answer. 例如,假设您有一个自定义CHECK_ANSWER事件,当用户输入答案时将触发该事件。 Your gradeAnswer() function (that exists in your "application.swf") would need to register for that event after the child "trigonometry.swf" was loaded. 在加载子项“ trigonometry.swf”之后,您的gradeAnswer()函数(存在于“ application.swf”中)将需要注册该事件。 Subsequently, and the data would be passed through. 随后,数据将通过。

This, however, may not be ideal as it can cause the child SWF to not unload from memory properly due to a parent registration to a child object (all registrations must be removed first). 但是,这可能并不理想,因为由于父级对子对象的注册,子SWF不能正确地从内存中卸载(必须首先删除所有注册)。

Personally, I'd recommend creating a static class which serves as a directory for your application. 就个人而言,我建议创建一个静态类作为您的应用程序的目录。 Each swf loads this class, and lists their pertinent methods as needed. 每个swf均加载该类,并根据需要列出其相关方法。 For example: 例如:

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

        }
    }
}

When the application.swf loads, it populates the methods object with your grading function. 加载application.swf时,它将使用分级功能填充method对象。 Subsequent child swfs also load this static class, and expect to find your grade method pre-populated. 随后的子swfs也会加载此静态类,并希望找到预先填充的成绩方法。

Application.swf Application.swf

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

Child.swf 儿童.swf

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

Don't know if the quiet is because this kind of question has been answered many times before on Stack Overflow. 不知道是否安静是因为在Stack Overflow上已经多次回答了这种问题。 Anyways here is my take on it.. 无论如何,这是我的看法。

I want the user working on trigonometry to be able to type in the trig function the believe to be correct (sin, cos, or tan) and "check" to see if they are right. 我希望使用三角函数的用户能够输入认为正确的Trig函数(正弦,余弦或棕褐色)并“检查”以查看它们是否正确。

I'm assuming since they have to type the answer you have an input textbox? 我假设因为他们必须输入答案,所以您有输入文本框? Also when they have answered they press a button to "submit" their answer? 另外,当他们回答后,他们会按下按钮“提交”他们的答案吗? Assuming I'm on the right lines we can do that this way... 假设我走对了,我们可以这样做

note: answer_txt (string) = answer given in external class by user, input_txt = textfield for user input 注意:answer_txt(字符串)=用户在外部类中给出的答案,input_txt =用户输入的文本字段

In the main class have something like 在主要班级有类似

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

Now in the external class (Trigonometry.as??) you can set up something like this. 现在,在外部类(Trigonometry.as ??)中,您可以进行如下设置。 Make sure answer_txt exists ( public var answer_txt:String; ).. 确保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
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM