user2223772
user2223772

Reputation: 1

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). 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).

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. 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). So...how do I reference a custom class method of a main swf FROM an externally loaded swf file's own custom class?

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. Thus, I'm trying to have each topic capable of referencing the "grader" custom class when it exists within the main "application" swf.

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. (when using parent.parent).

Thank you for any help you can provide.

Upvotes: 0

Views: 184

Answers (2)

VC.One
VC.One

Reputation: 15936

Don't know if the quiet is because this kind of question has been answered many times before on 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.

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

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. Make sure answer_txt exists (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
}

Upvotes: 0

Atriace
Atriace

Reputation: 2558

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.

For example, let's assume you have a custom CHECK_ANSWER event that fires when the user enters an answer. Your gradeAnswer() function (that exists in your "application.swf") would need to register for that event after the child "trigonometry.swf" was loaded. 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).

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. 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. Subsequent child swfs also load this static class, and expect to find your grade method pre-populated.

Application.swf

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

Child.swf

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

Upvotes: 1

Related Questions