How to return a variable from Public function

后端 未结 2 1374
暗喜
暗喜 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;
            }
        }
    }
    

提交回复
热议问题