PDA

View Full Version : Question About completeHandler Arguments


a_pedestrian
12-01-2007, 04:04 AM
Thanks for your time.

I'm working on setting up terse syntax for an external text loader, but I'm still not thoroughly understanding the following line:

loadit.addEventListener(Event.COMPLETE, completeHandler)

I've defined completeHandler below with an argument of event:Event, but I'm not sure how to get a second argument passed into that function while still maintaining the event. I tried this:

function completeHandler(event:Event, boxname:TextField)
...
loadit.addEventListener(Event.COMPLETE, completeHandler(Event.COMPLETE, boxname))

But I got the error "Implicit coercion of a value of type String to an unrelated type in flash.events:Event".

Does anyone know why this happens, or how to fix it?








PS: loadit is declared with "var loadit:URLLoader = new URLLoader();" and its dataFormat has been set as URLLoaderDataFormat.TEXT.

matbury
12-01-2007, 04:13 AM
Just do one thing at a time. The event listener is only listening for one thing, a static constant string object named 'COMPLETE' with the value 'complete'. It doesn't handle TextFields or anything else.

Just do what ever you want to do with the TextField in the handler function.

los_bordos
12-01-2007, 08:46 AM
Event.target and event.currentTarget will usually give you a reference to whetever object dispatched the event.

private function eventHandler(event:Event):void
{
var e:DisplayObject = event.currentTarget;
if(e is TextField)
{
trace(e.text)
}
}

edit:
Sorry for not reading your post before replying. But yes. an event listener listens for an event and responds to that event only, it doesn't take any arguments.

a_pedestrian
12-02-2007, 08:03 PM
That's really a shame. My goal with doing this stuff is basically to set up a one-line call for a loader, like:

myTextField = getTextFromFile( "filename.txt" );
// or
getTextFromFile( "filename.txt", myTextField );

I was told that this is possible, but I haven't figured out how to put it together with code yet. The problem I keep running into is that I don't know to get the argument (or variable) myTextField into a situation where I can assign it (by reference or otherwise) without having to explicitly type it a second time within the completeHandler or elsewhere.

This project itself is kind of a learning process for me, so any input is appreciated. Thanks for your time.