Pregunta

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


    }


}
¿Fue útil?

Solución

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.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top