PDA

View Full Version : external class problems with event function



Didi32
August 29th, 2008, 07:53 PM
Hi:
First of all, I would like to let you know that I am totally new to creating packages and classes externally from the main document.

I am currently trying to create a class that connects to a php file, gets some information and returns it into a String in the main timeline. I believe my problem is related to the fact event functions cannot return any value; but then, how can I get my string?

Here is the class:



package {
import flash.display.*;
import flash.events.*;
import flash.net.*;

public class CommentMaker extends Sprite {

private var myLoader2:URLLoader = new URLLoader()

public function CommentDisplayer(idNum) {

myLoader2.dataFormat = URLLoaderDataFormat.VARIABLES

myLoader2.load(new URLRequest('flashFile.php?id='+idNum))

myLoader2.addEventListener(Event.COMPLETE, onDataLoad)

function onDataLoad(evt:Event):String{
var commentString:String = "";
for(var i:uint=0; i<evt.target.data.cant; i++){
var commentString = evt.target.data['Title'+i];

}
return commentString;

}

}

}


And here, in my main document:



var theComments:CommentMaker = new CommentMaker();
trace(theComments.CommentDisplayer(5));


What I want to get back is the commentString value I tried to returned. I think the problem is that I'm trying to return a value from an event function. I tried to return whatever variable from the main function CommentDisplayer and I got it back properly, but how - from loading the CommentDisplayer function, can I get the value of the string that is inside the event function (OnComplete).

Also, if you know where I can find good information on how to create good classes, I would be grateful.

Thanks a lot.
Didi

senocular
August 29th, 2008, 08:14 PM
you cant return from an event it gets called from an internal process - you dont control the listener call so you can't get a return value.

What you need to ro is have your CommentMaker dispatch its own event in the onDataLoad listener. Then you can add a listener to the theComments instance to find out when the data has loaded

Didi32
August 29th, 2008, 08:42 PM
Thanks for your answer. However, I am not sure what type of event listener I should put inside onDataLoad and how to add a listener to find out when the data has loaded. Is there any way I could see a concrete example?

Thanks.

Didi