0

So I made a program that will load some text in a dynamic text box, called text_Box. Then I also made a button called reset_btn. I am not sure how to code this, but I want the user to be able to click the button, and the dynamic text_Box will reset, and run through their code again (to get their text). Basically I want to close the program, without actually closing it haha. So all the actions, and code re-run on a click of a button. Or what some people may call, to "Flush" an application. Can someone please help me on this? Thanks in advanced!

var ldr:URLLoader = new URLLoader();
ldr.addEventListener(HTTPStatusEvent.HTTP_STATUS, ldrStatus);

var url:String = "http://google.com";
var limit:int = 10;

var time_start:Number;
var time_stop:Number;
var times:int;

ping();

function ping():void
{
    trace("pinging", url);

    times = 0;
    doThePing();
}

function doThePing():void
{
    time_start = getTimer();
    ldr.load(new URLRequest(url));
}

function ldrStatus(evt:*):void
{
    if(evt.status == 200)
    {
        time_stop = getTimer();
        textBox.text = String(time_stop - time_start);


    }


}
sdksmkfnajnf
  • 76
  • 1
  • 1
  • 10
  • Maybe you could show us some of your code. Basically, to reset the value of a `TextField` you just assign a new value to it's `text` property: `myTextField.text = ''`. But it sounds like you may want to reset more than just the value of the text field, which is why I suggest you show the code you're using. – Sunil D. Aug 16 '13 at 15:25
  • Sure thing, will do :) – sdksmkfnajnf Aug 16 '13 at 15:26
  • Well, you have a sequence of code that's executed at program startup. Either emulate a startup via creation of `new Main()` and replace current `Main` with a new one, or develop a `reinit()` method that will clean up and re-run initialization of your main object along with text field. – Vesper Aug 16 '13 at 15:28
  • How would I do that? Could you please give me some example code?:) – sdksmkfnajnf Aug 16 '13 at 15:30
  • Code is added Sunil:) – sdksmkfnajnf Aug 16 '13 at 15:44

1 Answers1

1

You need to do correct re-initialization. First, you've messed up static code with functions, and class-wide variables with pre-init, so declare those variables without initialization, and make a newPing() function with the following code, and call it whenever you need to.

function newPing():void {
    ldr=new URLLoader();
    ldr.addEventListener(HTTPStatusEvent.HTTP_STATUS, ldrStatus);
    times++; // this will also track how many pings were there
    doThePing();
}

You have the rest of variable initialization already done, and they do not need to be re-initialized. Your ldr is the only thing that should be renewed. In more complicated situations you might need a lot of re-assignments, say if you make a game with lives and score, set lives to initial amount and score to zero. But you should always find what needs re-assignment by yourself. This is only the principle.

Vesper
  • 18,599
  • 6
  • 39
  • 61