How to include an actionscript function from an ex

2019-08-26 05:49发布

问题:

It turns out that’s it’s impossible to declare a class inside a embedded <fx:Script><![CDATA[ so it turns I need to put and include the actionscript code inside an external Sourcefile. The error is commented out

<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
               xmlns:s="library://ns.adobe.com/flex/spark" 
               xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="1955" minHeight="1600">
    <fx:Script source="URLRequest.as" />
    <s:layout>
        <s:BasicLayout />
    </s:layout>
    <s:Panel x="0" y="0" width="955" height="600" title="Bypass">
        <s:layout>
            <s:BasicLayout />
        </s:layout>
        <s:Label x="1" y="1" text="Please enter the ɢɪᴛ repository ʜᴛᴛᴘ ᴜʀʟ :"/>
        <s:TextInput x="224" y="1" width="726" id="txtName" text="http://ytrezq.sdfeu.org/flashredirect/?http"/>
        <s:Button x="1" y="12" label="ɢɪᴛ push !" click="send()"/> <!-- Undefined Method method error -->
    </s:Panel>
    <fx:Declarations>
    </fx:Declarations>
</s:Application>

and in URLRequest.as :

final public class MyClass {
    // some stuff
}
public function send():void {
    var request:Myclass=new Myclass(txtName.text);
    // Some stuff with 
}

So the question is simple but I couldn’t found the answer anywhere. At least not with for mxml with Spark.
send() doesn’t need to be in a class and as you can see is outside a class. But it needs to use a custom class.

So how can I call send() from URLRequest.as ?

回答1:

Now that I finally understand what you want to do, I've another idea - which is a little more complicated.

Create a file called Dummy.as and fill it with this:

package
{
    public class Dummy
    {
        public static function send(url:String):void
        {
            var request:Myclass=new Myclass(url);
        }
    }
}
class Myclass
{
    public function Myclass(inp:String)
    {
        trace(inp);
    }
}

Again, get rid of

<fx:Script source="URLRequest.as" />

and replace it by

<fx:Script>
    <![CDATA[
        import Dummy;
    ]]>
</fx:Script>

and finally replace

<s:Button x="1" y="12" label="ɢɪᴛ push !" click="send()"/>

with

<s:Button x="1" y="12" label="ɢɪᴛ push !" click="Dummy.send(txtName.text)"/>

The trick here is that we're importing the Dummy class that just has a static function which we can call without instantiating. Furthermore - as long as we define it outside of the package, we can add more class definitions, which are visible to the Dummy class.



回答2:

I'm also not too sure what you're trying to do but here's a possible way:

Create an empty file called Myclass.as in the same folder as your .mxml file and insert these lines

package
{
    public class Myclass
    {
        public function Myclass(inp:String)
        {
            trace(inp);
        }
    }
}

Now get rid of

<fx:Script source="URLRequest.as" />

and replace it by

<fx:Script>
    <![CDATA[
        public var request:Myclass;
    public function send():void {
        request=new Myclass(txtName.text);
    }
    ]]>
</fx:Script>