views:

25

answers:

1

I am not sure exactly is happening here but did figure out that URLLoader.close(); is the cause.

I have a simple application whereby a user enters info into a textbox, then I send that info to a PHP script and return output. After output is received, you can click a new button that resets the application. The error lies with the function that executes after that button is clicked.

If you click that button to reset the application, it resets all variables, etc, but then it seems my addEventListener method does not execute.

Here is the reset function:

//Reset function if the Reset button is pressed
function clearApplication(e:MouseEvent):void {
    receivedData="";
    data1TextBox.text="";
    data2TextBox.text="";
    resetButton.visible=false;
    resetButton.removeEventListener(MouseEvent.CLICK, clearApplication, false);
    goButton.visible=true;
    goButton.addEventListener(MouseEvent.CLICK, getData, false, 0, true);
    myLoader.close();
}

After this executes, my goButton no longer works.

If the goButton is clicked, it should execute the following:

function getData(e:MouseEvent):void {
    if (data1TextBox.text!=""&&data2TextBox.text!="") {
        goButton.removeEventListener(MouseEvent.CLICK, getData, false);
        goButton.visible=false;

        postVars = new URLVariables();
        postVars.data1=data1TextBox.text;
        postVars.data2=data2TextBox.text;

        myRequest=new URLRequest("URL");
        myRequest.method=URLRequestMethod.POST;
        myRequest.data=postVars;

        myLoader.addEventListener(Event.COMPLETE, onComplete, false, 0, true);
        myLoader.load(myRequest);
    }
}

And finally, the onComplete function:

function onComplete(e:Event):void {
    receivedData=e.target.data;

    outputTextBox.text=receivedData;

    resetButton.visible=true;
    resetButton.addEventListener(MouseEvent.CLICK, clearApplication, false, 0, true);
}

Note that any variables not declared within the functions are declared globally.

So, I stumbled upon the fact that if I delete myLoader.close(); from the clearApplication function then everything works smoothly. My goButton has no issues and executes as expected if all text fields are filled in.

I know it is not necessary if nothing is currently being loaded, but it is there since I never know if it is going to hang and needs to be canceled. Why would myLoader.close(); cause something like this to happen?

Edit: Sorry, made a few mistakes trying to simplify my code for the example.

+1  A: 

Does URLLoader.close() cause problems if nothing is being loaded?

Yes, it does. From livedocs page for URLLoader.close()

Any load operation in progress is immediately terminated. If no URL is currently being streamed, an invalid stream error is thrown.

Apparently you're not using the debug version of the Flash player and hence not seeing the error being thrown. The debug version of Flash player - is a must have for Flash/Flex developers.

If you put the call to close() in try block and catch the error, you can see what's happening.

try 
{
  myLoader.close();
}
catch(e:Error) 
{
  trace("An error occurred " + e.toString());
}
Amarghosh
Aw... I cannot believe that I missed that! That is exactly where I found the close() method in the first place. I need to be more careful. And thank you, I didn't realize there was a debug player but thought I was definitely missing something!
Structure