as3 Main-Timline calling a function from a class l

2019-09-21 04:12发布

问题:

UPDATE: OK I am about ready to give up on Pacakages and classes. No answers coming. Not sure what to do. I tried to make the question easier and made a new post but I was down voted for it. as3 calling a function in another class


I am TOTALLY NEW to using PACKAGES and CLASSES. I am finally converting over from the timeline after having so many issues. Please be patient with my lack of knowledge. I need to know how to call a function in the child swf file I loaded from code in the maintime in the parent swf.

There are 2 swf files. Main.swf and pConent.swf 1. Main.swf has code in the timeline of the first frame. 2. pConent.swf is loading a PACKAGE CLASS as file.

QUESTIONS I am trying to call a function in it from its parent Main.swf. How do I do this? Here is sections of the code from both. Thanks

Main.swf CODE /// is an AIR for Andrid swf

function LoadContent()
{  
    TheContent.load(new URLRequest( "pContent.swf"));
    TheContent.contentLoaderInfo.addEventListener(Event.COMPLETE, LoadContentTWO);
    function LoadContentTWO(e:Event)
    {
        Content = TheContent.content as MovieClip;
        pContent = Content as Object;
        addChild(TheContent);

            var OSS:String = "device";
            trace(pContent); //// comes out as: [object pContent]
        pContent.GetOnlineStatus(OSS); ///// HOW DO I GET THIS TO CALL FUNCTION
    } 
}

A SECTION OF THE "CLASS" in pContent.swf I am trying to call

 public function GetOnlineStatus(OS:String)

{

    if(OS=="online")
        trace("inside ONLINE" );
        }
    if(OS=="device")
        {
        trace("inside DEVICE" );
        }
}

THE ERROR I AM GETTING

TypeError: Error #1006: GetOnlineStatus is not a function.

UPDATE: I decided to post the FULL PACKAGE ( my first) to see if I am doing it right.

package  
{

import flash.display.MovieClip;
import fl.transitions.Tween;
import fl.transitions.easing.*;
import fl.transitions.TweenEvent;
import flash.display.*;
import flash.media.Sound;
import flash.system.*;
import flash.media.SoundChannel;
import flash.display.MovieClip;
import flash.events.MouseEvent;




    public class pContent extends MovieClip 
    { 
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

var ScreenY = flash.system.Capabilities.screenResolutionY;
var ScreenX = flash.system.Capabilities.screenResolutionX;
var swf:String;
var daSounds:String;
var images:String;
var videos:String;
var OnlineStatus:Boolean;



//++++++++++++++++++++++++
//++++++++++++++++++++++++

public function pContent()
    {
    BG.addEventListener(MouseEvent.CLICK, mouseHandlerdown);        
    }

//++++++++++++++++++++++++
//++++++++++++++++++++++++





    //-------- * FUNCTIONS * --------
    //-------------------------------

    public function mouseHandlerdown(event:MouseEvent):void 

        {
        alpha = .3; // testing
        }

    public function GetOnlineStatus(OS:String)
        {

        if(OS=="online")
            {
            OnlineStatus = true;
            Security.allowDomain("*");
            trace("inside THE PATH " + ThePath.text);
            daSounds = "http://mycontactcorner.com/upload/files/";
            swf =    "http://mycontactcorner.com/upload/files/";
            trace("inside THE DEVICE ONLINE" );
            OnlineStatus = false;
            swf = "";
            daSounds = "content/sounds/";
            //LoadMenu();
            LoadStage();
            LoadBeau();
            }
        if(OS=="device")
            {
            trace("inside THE DEVICE ONLINE" );

            }
        }

    //------ * END FUNCTIONS * -----            
    //------------------------------ 



    }// END FUNCTION pContent
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%



}//// END PACKAGE

回答1:

Don't be disappointed, but I won't have a "real" answer to your question, and I am really not going to work through all of your code to solve it, either. It is your own task to learn how to do this, and unless people here are very, very hungry for reputation, no one will do it for you - we will only help you to find the right way.

Your problem is not "packages and classes", so please do not give up on them. They will help you a great deal, once you've started to understand them. Your problem is, that you are not facing a single problem, but actually at least two (and quite substantial ones, I might add):

  1. You need to go back to learn about the basics of object oriented programming in ActionScript. You won't have much luck getting answers to questions like this, otherwise. And believe me, I don't mean that in a patronizing way - it is simply a complicated matter, and it is hard to communicate complicated issues, both when you don't know the terms to express them, or when your counterpart doesn't understand them. Think of it like a high school math problem: You won't ever find a solution to your trigonometry question (or get a decent answer), unless you learn some basic algebra first.

  2. You also have a problem related to loading, application domains, and the Flash Player security model - which are all far more complicated than what you should aim at when trying out OOP stuff. This can be a major obstacle, and unless you want to frustrate yourself, you should try to avoid it, until your program actually runs.

So this here is my advice: Always try to solve one problem at a time. Do not work yourself into such complex scenarios as the one you are in right now, but take step by step, until you've reached a level where you are confident with what you are doing.

Your first issue should be to understand what's going on with classes and objects. Everything else will come later. You should try to isolate your problem in the pContent.swf and get that to work first - or better yet, put everything you need for your program into a single file. Convert to using classes. Then, once you know how to work with those, start learning about more advanced OO, decoupling your code using interfaces, type casting and loading binaries at runtime.



回答2:

//makes contact with classs but comes out as: [object pContent]

its because you said

pContent = Content as Object;

I am not sure why you are doing this extra step

Change it to this

Content = TheContent.content as MovieClip;
// pContent = Content as Object; //NO NEED OF THIS
addChild(Content); // just in case this gives error change it as addChild(Content as Object);

var OSS:String = "device";
trace(Content); //now see the difference
Content.GetOnlineStatus(OSS); // it calls now

Also, give the link where you posted that scary question :P if it has rest of the code



回答3:

Sorry if this does not sound like an answer, but I'm going to write a couple of doubts that I have reading your code that can possibly lead to the solution:

  • Why are you casting it to MovieClip? If you cast it as MovieClip, the compiler it is going to tell you that the method "GetOnlineStatus" doesn't exist, because MovieClip class doesn't have it! I think you have to cast it as pContent

  • Why are you trying to casting TheContent.content? What is "content"? I had a look to your previous post and I cannot see anything called "content"?

If I ignore my second doubt (TheContent.content issue), I would change the code like this:

 Content = TheContent.content as pContent;  // your class it's called pContent
 addChild(Content);
 Content.GetOnlineStatus(OSS); // it calls now

Also, keep in mind that generally it's a good pratice to capitalize name of classes and not variables. Let me know!



回答4:

private function GetOnlineStatus

Try making this a public function instead. When it's private it can't be accessed outside the scope of the class that owns it.



回答5:

I believe that in order to make this work, content property of the Loader. You have to create a reference to the loaded SWF as the class you are trying to call. This class has to be included in the main SWF's project. Then you can call the functions of that particular class in the child.

function LoadContent()
{  
    TheContent.load(new URLRequest( "pContent.swf"));
    TheContent.contentLoaderInfo.addEventListener(Event.COMPLETE,     LoadContentTWO);
}

function LoadContentTWO(e:Event)
    {
        var pContent:GetOnlineStatus = GetOnlineStatus(e.target.content);
        addChild(e.target.content); //Assuming that "TheContent was 
    //  declared as var TheContent:Loader , you'd be adding the loader to the stage when I think you actually wanted // to add the content. 

        var OSS:String = "device";
        trace(pContent); //// comes out as: [object pContent]
        pContent.GetOnlineStatus(OSS); ///// HOW DO I GET THIS TO CALL FUNCTION
    // This should work now. If not, try to loading a function that is not the class' main function. Because I think you might get an "unable to call static function error". I'm a begginner too though, so sorry if I'm wrong. Example: pContent.GetOnlineStatusFunction(OSS);
    } 

This answer assumes that the pContent.swf contains a class file that looks like this:

package {
    public class GetOnlineStatus {
        public function GetOnlineStatus (OSS:String) {
             //Do your GetOnlineStatus Logic. This is the main function.
        }
      /*public function GetOnlineStatusFunction (OSS:String) {
            //Example non-main function
        } */
    }
}  

Source: http://www.scottgmorgan.com/accessing-document-class-of-externally-loaded-swf-with-as3/