如何返回从公共函数的变量变量、函数

2023-09-08 13:04:02 作者:最燦爛的陽光

我试图使用code中的主时间轴上脱身,但我在努力了解。至于文件和.fla文件的文件交互。例如,我试图找出如何从主时间轴传递变量,以公共职能,做一些东西该变量,并将其传递回主时间轴。我有框上输入文本框和一个简单的按钮与一个监听器。我希望能够输入00000 00到文本框,并有0.00归还。下面是我的code:

I'm trying to get away from using code on the main timeline but I'm struggling to understand how .as files and .fla files interact. For example, I'm trying to figure out how to pass a variable from the main timeline, to a public function, do some stuff to that variable and pass it back to the main timeline. I have an input text box on the frame and a simple button with a listener. I want to be able to input 00000 00 into the text box, and have 0.00 returned. Below is my code:

import flash.events.MouseEvent;
import convertToDecimal;
var inputText:String;
var outputText:String;

submit_btn.addEventListener(MouseEvent.CLICK, submit);

function submit(e:MouseEvent):void
{
    inputText = input_txt.text;
    new convertToDecimal(inputText);
    trace();
}

这里是公共职能:

And here is the public function:

package
{
    import flash.sampler.StackFrame;
    import flash.events.MouseEvent;
    import fl.controls.Button;
    public class convertToDecimal
    {
        public function convertToDecimal(stringParmter:String)
        {
            var rex:RegExp = /[\s\r\n]+/gim;
            stringParmter = stringParmter.replace(/^\s+|\s+$/g, '');
            stringParmter = stringParmter.replace(rex,'.');
            stringParmter = stringParmter.replace(/^0+(?!\.|$)/, '');
            if ((stringParmter == "-----.--") || (stringParmter == "0"))
            {
                stringParmter = "      00";
            }
        }
    }
}

这可能是一个非常小白的问题,但任何帮助是AP preciated。

This is probably a really noob question but any help is appreciated.

推荐答案

如果你有课,为了使用它,你必须建立它的拷贝,并将其分配给变量。构建你的类是很容易:

If you have class, in order to use it, you must construct its "copy" and assign it to the variable. Constructing your class is really easy:

new convertToDecimal(inputText); // does the constructing job

但是,接下来发生?当程序进入到下一行,你构造类会松动!你必须把它分配给变量,以保持它在内存中:

But what happen next? When your program goes to the next line, you constructed class will be loosed! You must assign it to variable, in order to keep it in memory:

var yourVariableName:convertToDecimal = new convertToDecimal(inputText);

现在你有类的副本。 OOP范式是好事,因为你可以创建吨的副本的其实很容易,然后,每一个复制将生活由自己的生活。

Now you have your "copy" of class. OOP paradigm is good because you can create tons of "copies" really easily and then, each "copy" will live by its own live.

现在回到你的问题。这不是一个秘密,将您的code到时间线是坏的。而不是你的类附加到项目,并改变它是这样的:

Now back to your question. It's not a secret that adding your code to the timeline is bad. Instead attach your class to your project and change it this way:

    package
{
    import flash.sampler.StackFrame;
    import flash.events.MouseEvent;
    import fl.controls.Button;

    public class Main
    {
        public function Main()
        {
            submit_btn.addEventListener(MouseEvent.CLICK, submit);

        }

        private function submit(e:MouseEvent):void
        {
            var inputText:String = input_txt.text;
            inputText = convertToDecimal(inputText);
            trace(inputText);
        }

        private function convertToDecimal(stringParmter:String):String
        {
            var rex:RegExp = /[\s\r\n]+/gim;
            stringParmter = stringParmter.replace(/^\s+|\s+$/g, '');
            stringParmter = stringParmter.replace(rex, '.');
            stringParmter = stringParmter.replace(/^0+(?!\.|$)/, '');
            if ((stringParmter == "-----.--") || (stringParmter == "0"))
            {
                stringParmter = "      00";
            }
            return stringParmter;
        }
    }
}