How to return a variable from Public function

后端 未结 2 1373
暗喜
暗喜 2021-01-28 19:10

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

相关标签:
2条回答
  • 2021-01-28 19:48

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

    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.


    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;
            }
        }
    }
    
    0 讨论(0)
  • 2021-01-28 19:51

    Parameters of simple types are passed by value, so in order to return a changed String, make your public function return String:

    public function convertToDecimal(stringParmter:String):String {...}
    

    Then, when you come up with a value that you want to be available outside, put a return <the value>; statement in your function. In order to capture the value returned from a function, assign it to a variable, the same one that's passed into it can be used.

    stringParameter=convertToDecimal(stringParmter);
    

    Also, if your AS3 file contains only the function, you can avoid wrapping it into a class, and declare directly "public function...".

    package
    {
        public function convertToDecimal(stringParmter:String):String
        {
                var rex:RegExp = /[\s\r\n]+/gim;
                var s:String;
                s = stringParmter.replace(/^\s+|\s+$/g, '');
                s = s.replace(rex,'.');
                s = s.replace(/^0+(?!\.|$)/, '');
                if ((s == "-----.--") || (s == "0"))
                {
                    s = "      00";
                }
                return s;
        }
    }
    
    0 讨论(0)
提交回复
热议问题