需要一些帮助理解在AS3中包的使用

时间:2021-10-27 02:01:16

I'm just beginning to explore the use of packages. I would like the result of a function (as a variable "myXML") to be available to other elements of the code. And I'm wondering if creating a new class file is the way to go. For example:

我才刚刚开始探索包装的使用。我希望函数(作为一个变量“myXML”)的结果对代码的其他元素可用。我想知道是否应该创建一个新的类文件。例如:

package  
{
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.net.URLLoader;
    import flash.net.URLRequest;    

    public class XMLLoad 
    {
        public var myXML:XML;
        public var myLoader:URLLoader = new URLLoader();
        myLoader.load(new URLRequest("http://myWebsite/myFile.xml"));
        myLoader.addEventListener(Event.COMPLETE, processXML);

        public function processXML(e:Event):void 
        {
            myXML = new XML(e.target.data);
            trace(myXML);
        }

    }

}

Is this a good way to create a global variable? If so, in the FLA file, how would I access/use the var "myXM" which holds the XML data?

这是创建全局变量的好方法吗?如果是,在FLA文件中,我将如何访问/使用包含XML数据的var“myXM”?

import XMLLoad;
XMLLoad();
?

Many thanks.

多谢。

3 个解决方案

#1


2  

What you've done in your class is almost there, but there are a few things that need to be rectified:

你在课堂上所做的已经差不多了,但是有一些事情需要纠正:

Firstly, within the top level of a class deceleration you are only allowed to define members (properties and methods). At the moment you're doing that which is great, but you're also attempting to run some actual code in this scope as well:

首先,在类减速的顶层,您只能定义成员(属性和方法)。目前您正在做的事情非常棒,但是您也在尝试在这个范围内运行一些实际的代码:

myLoader.load(new URLRequest("http://myWebsite/myFile.xml"));
myLoader.addEventListener(Event.COMPLETE, processXML);

This will throw and error and your class will not function. To run code immediately when an instance of a class is created, you will need to use a constructor, which is a function using the same name as the class it's declared within (this is case sensitive). Converting that into code will look like this, where you'll notice I've created the constructor and placed the culprit code inside of it:

这将抛出并出错,您的类将无法运行。要在创建类的实例时立即运行代码,您将需要使用构造函数,它是一个函数,使用与在其中声明的类相同的名称(这是区分大小写的)。把它转换成代码就像这样,你会注意到我已经创建了构造函数,并将罪魁祸首的代码放在其中:

public class XMLLoad 
{

    public var myXML:XML;
    public var myLoader:URLLoader = new URLLoader();


    // This is the constructor.
    public function XMLLoad()
    {
        myLoader.load(new URLRequest("http://myWebsite/myFile.xml"));
        myLoader.addEventListener(Event.COMPLETE, processXML);
    }


    public function processXML(e:Event):void 
    {
        myXML = new XML(e.target.data);
        trace(myXML);
    }

}

What you've done now is created a class XMLLoad. Using the new keyword, you are able to create instances of that class. In your case, you will only need a single instance to do what you want, like this:

您现在所做的是创建一个类XMLLoad。使用新的关键字,您可以创建该类的实例。在你的例子中,你只需要一个实例来做你想做的事情,比如:

var xmlLoad:XMLLoad = new XMLLoad();

This will created an instance of XMLLoad and assigned it to the variable xmlLoad, through which you will be able to access properties and methods that you defined in that class. The constructor we created above will also run automatically, meaning your class has already initiated a request for some XML.

这将创建一个XMLLoad实例并将其分配给变量XMLLoad,通过它,您将能够访问在该类中定义的属性和方法。上面创建的构造函数也将自动运行,这意味着您的类已经启动了对某些XML的请求。

Unfortunately because requests for external data are asynchronous (that is, they perform on a timeline which is different to the natural flow of your application), we are unable to tell when the XML has completely loaded and we are able to use it from outside the class. What we can do however is change the XMLLoad class a little bit to help us out:

不幸的是,由于对外部数据的请求是异步的(也就是说,它们在与应用程序的自然流不同的时间轴上执行),所以我们无法判断XML何时已被完全加载,并且我们能够在类之外使用它。但是我们可以做的是稍微改变XMLLoad类来帮助我们:

public class XMLLoad 
{

    public var myXML:XML;
    public var myLoader:URLLoader = new URLLoader();
    private var _callback:Function;

    public function XMLLoad(callback:Function)
    {
        _callback = callback;
        myLoader.load(new URLRequest("http://myWebsite/myFile.xml"));
        myLoader.addEventListener(Event.COMPLETE, processXML);
    }

    public function processXML(e:Event):void 
    {
        myXML = new XML(e.target.data);
        _callback(myXML);
    }

}

What we've done here is modify the constructor to accept a reference to a function, which should accept an argument of the type XML. Then we've modified your processXML function to perform the callback and send the received XML through to it. This means that you can now do this:

我们在这里所做的是修改构造函数以接受对函数的引用,该函数应该接受XML类型的参数。然后我们修改了processXML函数来执行回调并将接收到的XML发送到它。这意味着你现在可以这样做:

var xmlLoad:XMLLoad = new XMLLoad(done);

function done(xml:XML):void
{
    trace(xml);
}

This means that you will be able to continue your application within the done function, which will have the fully loaded XML available to you.

这意味着您将能够在done函数中继续您的应用程序,该函数将向您提供已完全加载的XML。

#2


2  

Marty Wallace's answer covers everything you need to know to get your question answered and working. I would add, though, that since you are trying to make these variables Global, there is one dangerous pitfall.

马蒂·华莱士的回答涵盖了所有你需要知道的信息,让你的问题得到解答并发挥作用。不过,我想补充一点,既然你试图使这些变量具有全局性,那么就存在一个危险的陷阱。

The danger is that every time you call new, you get a new instance of your variables. It can be very easy to accidentally create two or more sets of Globals, each with different values.

危险在于,每次调用new时,都会得到一个新的变量实例。很容易意外地创建两个或多个全局变量集,每个全局变量具有不同的值。

There are two solutions: using only static variables, or writing the class as a singleton.

有两种解决方案:只使用静态变量,或者将类写成单例。

The first is much simpler, just add the static keyword to your variables and remember to reference them with XMLLoad.variable_name instead of creating a new instance.

第一个要简单得多,只需将静态关键字添加到变量中,并记住使用XMLLoad引用它们。variable_name,而不是创建一个新实例。

public class XMLLoad {
    public static var myXML:XML;
    public static var myLoader:URLLoader = new URLLoader();
    private static var _callback:Function;

    public static function loadXML(callback:Function):void
    {
        _callback = callback;
        myLoader.load(new URLRequest("http://myWebsite/myFile.xml"));
        myLoader.addEventListener(Event.COMPLETE, processXML);
    }

    public function processXML(e:Event):void 
    {
        myXML = new XML(e.target.data);
        _callback(myXML);
    }
}

Then, to once again borrow from Marty:

然后,再次向马蒂借用:

XMLLoad.loadXML(done);

function done(xml:XML):void {
    trace(xml);
}

This is nice, but in more complex situations get a little wordy and confusing. Singletons, on the other hand, work the same way, but behave just like a normal class. Though the inner workings can be a little more complex.

这很好,但是在更复杂的情况下会变得有点冗长和混乱。另一方面,单身人士的工作方式是一样的,但他们的行为举止却和正常的班级没什么两样。虽然内部工作可能会更复杂一些。

public class XMLLoad {
    /**
     *  A static variable that holds the actual instance of the class.
     **/
    private static var instance:XMLLoad;

    public var myXML:XML;

    /**
     *  An initialization method that replaces the constructor and creates
     *      (if it didn't already exists) and returns the instance for external
     *      use.
     **/
    public static function GetLoader():XMLLoad {
        if (!instance) {
            instance = new XMLLoad;
        }
        return instance;
    }

    public function loadXML(callback:Function):void {
        _callback = callback;
        myLoader.load(new URLRequest("http://myWebsite/myFile.xml"));
        myLoader.addEventListener(Event.COMPLETE, processXML);
    }

    private function processXML(e:Event):void {
        myXML = new XML(e.target.data);
        _callback(myXML);
    }
}

The call to this class would look like this:

这个类的调用应该是这样的:

var xmlLoader:XMLLoad = XMLLoad.GetLoader();
xmlLoader.loadXML(done);

function done(xml:XML):void {
    trace(xml);
}

#3


2  

  1. Create a class called "Variables.as"

    创建一个名为“variabls .as”的类

  2. The code inside it is like so (note: no functions, just variables):

    里面的代码是这样的(注意:没有函数,只是变量):

    // Define these variables in the normal place where you would 
    // define them in an AS3 class/package
    
    public static var enabled:Boolean = false;
    public static var configXMLData:XML;
    
  3. Usage, import Variables.as and just call and set using Variables.enable = true or trace(Variables.enable) etc. Add to public static var list for the variables you want.

    使用、进口变量。就像调用和设置变量一样。enable = true或trace(Variables.enable)等。为您想要的变量添加到公共静态var列表。

  4. XML Loading can be done in an external class or in your main application. Sounds like you don't know much about AS3 (no offence) so I would just keep it simple for now and stick it in your main class. All code assists are in Adobe Live Docs (some keywords there for you when Googling) eg. AS3 LoadXML googles will give you lots of examples. Note setup, load, load complete, get data is the process flow you want. So you only proceed with your application once the onComplete function is called.

    XML加载可以在外部类或主应用程序中完成。听起来你不太了解AS3(无意冒犯),所以我现在就简单一点,把它放在你的主课上。所有的代码辅助都在adobelive文档中(在谷歌搜索时提供一些关键字)。AS3 LoadXML googles将给出很多示例。注意设置、加载、加载完成、获取数据是您需要的流程流。因此,您只能在调用onComplete函数之后继续应用程序。

  5. Once your XML is loaded Google up AS3 E4X parsing. This is a huge subject where you can retrieve the value of attributes and node content using XML E4X for AS3. It's not hard you will need examples to reference to. There is also the old way which is XML List, which you can find documented and exampled Adobe Live Docs xmlList. Once you know enough about this you will be able to set the Variables in the Class document Variables.as at will. And no matter where you need them you simply call Variables.enable for example in any class you want.

    一旦您的XML被加载到AS3 E4X解析中。这是一个很大的主题,您可以使用AS3的XML E4X检索属性和节点内容的值。这并不难,你需要一些例子来参考。还有一种老方法就是XML列表,您可以找到文档化和示例化的adobelive Docs xmlList。一旦您对此有了足够的了解,您就可以在类文档变量中设置变量。作为。无论你在什么地方需要它们,你都只需要调用变量。例如,在您想要的任何类中启用。

#1


2  

What you've done in your class is almost there, but there are a few things that need to be rectified:

你在课堂上所做的已经差不多了,但是有一些事情需要纠正:

Firstly, within the top level of a class deceleration you are only allowed to define members (properties and methods). At the moment you're doing that which is great, but you're also attempting to run some actual code in this scope as well:

首先,在类减速的顶层,您只能定义成员(属性和方法)。目前您正在做的事情非常棒,但是您也在尝试在这个范围内运行一些实际的代码:

myLoader.load(new URLRequest("http://myWebsite/myFile.xml"));
myLoader.addEventListener(Event.COMPLETE, processXML);

This will throw and error and your class will not function. To run code immediately when an instance of a class is created, you will need to use a constructor, which is a function using the same name as the class it's declared within (this is case sensitive). Converting that into code will look like this, where you'll notice I've created the constructor and placed the culprit code inside of it:

这将抛出并出错,您的类将无法运行。要在创建类的实例时立即运行代码,您将需要使用构造函数,它是一个函数,使用与在其中声明的类相同的名称(这是区分大小写的)。把它转换成代码就像这样,你会注意到我已经创建了构造函数,并将罪魁祸首的代码放在其中:

public class XMLLoad 
{

    public var myXML:XML;
    public var myLoader:URLLoader = new URLLoader();


    // This is the constructor.
    public function XMLLoad()
    {
        myLoader.load(new URLRequest("http://myWebsite/myFile.xml"));
        myLoader.addEventListener(Event.COMPLETE, processXML);
    }


    public function processXML(e:Event):void 
    {
        myXML = new XML(e.target.data);
        trace(myXML);
    }

}

What you've done now is created a class XMLLoad. Using the new keyword, you are able to create instances of that class. In your case, you will only need a single instance to do what you want, like this:

您现在所做的是创建一个类XMLLoad。使用新的关键字,您可以创建该类的实例。在你的例子中,你只需要一个实例来做你想做的事情,比如:

var xmlLoad:XMLLoad = new XMLLoad();

This will created an instance of XMLLoad and assigned it to the variable xmlLoad, through which you will be able to access properties and methods that you defined in that class. The constructor we created above will also run automatically, meaning your class has already initiated a request for some XML.

这将创建一个XMLLoad实例并将其分配给变量XMLLoad,通过它,您将能够访问在该类中定义的属性和方法。上面创建的构造函数也将自动运行,这意味着您的类已经启动了对某些XML的请求。

Unfortunately because requests for external data are asynchronous (that is, they perform on a timeline which is different to the natural flow of your application), we are unable to tell when the XML has completely loaded and we are able to use it from outside the class. What we can do however is change the XMLLoad class a little bit to help us out:

不幸的是,由于对外部数据的请求是异步的(也就是说,它们在与应用程序的自然流不同的时间轴上执行),所以我们无法判断XML何时已被完全加载,并且我们能够在类之外使用它。但是我们可以做的是稍微改变XMLLoad类来帮助我们:

public class XMLLoad 
{

    public var myXML:XML;
    public var myLoader:URLLoader = new URLLoader();
    private var _callback:Function;

    public function XMLLoad(callback:Function)
    {
        _callback = callback;
        myLoader.load(new URLRequest("http://myWebsite/myFile.xml"));
        myLoader.addEventListener(Event.COMPLETE, processXML);
    }

    public function processXML(e:Event):void 
    {
        myXML = new XML(e.target.data);
        _callback(myXML);
    }

}

What we've done here is modify the constructor to accept a reference to a function, which should accept an argument of the type XML. Then we've modified your processXML function to perform the callback and send the received XML through to it. This means that you can now do this:

我们在这里所做的是修改构造函数以接受对函数的引用,该函数应该接受XML类型的参数。然后我们修改了processXML函数来执行回调并将接收到的XML发送到它。这意味着你现在可以这样做:

var xmlLoad:XMLLoad = new XMLLoad(done);

function done(xml:XML):void
{
    trace(xml);
}

This means that you will be able to continue your application within the done function, which will have the fully loaded XML available to you.

这意味着您将能够在done函数中继续您的应用程序,该函数将向您提供已完全加载的XML。

#2


2  

Marty Wallace's answer covers everything you need to know to get your question answered and working. I would add, though, that since you are trying to make these variables Global, there is one dangerous pitfall.

马蒂·华莱士的回答涵盖了所有你需要知道的信息,让你的问题得到解答并发挥作用。不过,我想补充一点,既然你试图使这些变量具有全局性,那么就存在一个危险的陷阱。

The danger is that every time you call new, you get a new instance of your variables. It can be very easy to accidentally create two or more sets of Globals, each with different values.

危险在于,每次调用new时,都会得到一个新的变量实例。很容易意外地创建两个或多个全局变量集,每个全局变量具有不同的值。

There are two solutions: using only static variables, or writing the class as a singleton.

有两种解决方案:只使用静态变量,或者将类写成单例。

The first is much simpler, just add the static keyword to your variables and remember to reference them with XMLLoad.variable_name instead of creating a new instance.

第一个要简单得多,只需将静态关键字添加到变量中,并记住使用XMLLoad引用它们。variable_name,而不是创建一个新实例。

public class XMLLoad {
    public static var myXML:XML;
    public static var myLoader:URLLoader = new URLLoader();
    private static var _callback:Function;

    public static function loadXML(callback:Function):void
    {
        _callback = callback;
        myLoader.load(new URLRequest("http://myWebsite/myFile.xml"));
        myLoader.addEventListener(Event.COMPLETE, processXML);
    }

    public function processXML(e:Event):void 
    {
        myXML = new XML(e.target.data);
        _callback(myXML);
    }
}

Then, to once again borrow from Marty:

然后,再次向马蒂借用:

XMLLoad.loadXML(done);

function done(xml:XML):void {
    trace(xml);
}

This is nice, but in more complex situations get a little wordy and confusing. Singletons, on the other hand, work the same way, but behave just like a normal class. Though the inner workings can be a little more complex.

这很好,但是在更复杂的情况下会变得有点冗长和混乱。另一方面,单身人士的工作方式是一样的,但他们的行为举止却和正常的班级没什么两样。虽然内部工作可能会更复杂一些。

public class XMLLoad {
    /**
     *  A static variable that holds the actual instance of the class.
     **/
    private static var instance:XMLLoad;

    public var myXML:XML;

    /**
     *  An initialization method that replaces the constructor and creates
     *      (if it didn't already exists) and returns the instance for external
     *      use.
     **/
    public static function GetLoader():XMLLoad {
        if (!instance) {
            instance = new XMLLoad;
        }
        return instance;
    }

    public function loadXML(callback:Function):void {
        _callback = callback;
        myLoader.load(new URLRequest("http://myWebsite/myFile.xml"));
        myLoader.addEventListener(Event.COMPLETE, processXML);
    }

    private function processXML(e:Event):void {
        myXML = new XML(e.target.data);
        _callback(myXML);
    }
}

The call to this class would look like this:

这个类的调用应该是这样的:

var xmlLoader:XMLLoad = XMLLoad.GetLoader();
xmlLoader.loadXML(done);

function done(xml:XML):void {
    trace(xml);
}

#3


2  

  1. Create a class called "Variables.as"

    创建一个名为“variabls .as”的类

  2. The code inside it is like so (note: no functions, just variables):

    里面的代码是这样的(注意:没有函数,只是变量):

    // Define these variables in the normal place where you would 
    // define them in an AS3 class/package
    
    public static var enabled:Boolean = false;
    public static var configXMLData:XML;
    
  3. Usage, import Variables.as and just call and set using Variables.enable = true or trace(Variables.enable) etc. Add to public static var list for the variables you want.

    使用、进口变量。就像调用和设置变量一样。enable = true或trace(Variables.enable)等。为您想要的变量添加到公共静态var列表。

  4. XML Loading can be done in an external class or in your main application. Sounds like you don't know much about AS3 (no offence) so I would just keep it simple for now and stick it in your main class. All code assists are in Adobe Live Docs (some keywords there for you when Googling) eg. AS3 LoadXML googles will give you lots of examples. Note setup, load, load complete, get data is the process flow you want. So you only proceed with your application once the onComplete function is called.

    XML加载可以在外部类或主应用程序中完成。听起来你不太了解AS3(无意冒犯),所以我现在就简单一点,把它放在你的主课上。所有的代码辅助都在adobelive文档中(在谷歌搜索时提供一些关键字)。AS3 LoadXML googles将给出很多示例。注意设置、加载、加载完成、获取数据是您需要的流程流。因此,您只能在调用onComplete函数之后继续应用程序。

  5. Once your XML is loaded Google up AS3 E4X parsing. This is a huge subject where you can retrieve the value of attributes and node content using XML E4X for AS3. It's not hard you will need examples to reference to. There is also the old way which is XML List, which you can find documented and exampled Adobe Live Docs xmlList. Once you know enough about this you will be able to set the Variables in the Class document Variables.as at will. And no matter where you need them you simply call Variables.enable for example in any class you want.

    一旦您的XML被加载到AS3 E4X解析中。这是一个很大的主题,您可以使用AS3的XML E4X检索属性和节点内容的值。这并不难,你需要一些例子来参考。还有一种老方法就是XML列表,您可以找到文档化和示例化的adobelive Docs xmlList。一旦您对此有了足够的了解,您就可以在类文档变量中设置变量。作为。无论你在什么地方需要它们,你都只需要调用变量。例如,在您想要的任何类中启用。