PDA

View Full Version : Returning from EventListener functions



sans_devin
December 22nd, 2008, 10:32 PM
Hey, I'm really new to AS3. This is a simple problem to solve but I just can't seem to figure it out or find the answer to it. I'm still trying to wrap my head around OOP.

Here's my code:


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

public class pixel extends Sprite
{
public function pixel()
{
myTextField.text = getEntry("pages.page[1].pageName");
}

private dynamic function getEntry(req:String):String
{
var request:URLRequest = new URLRequest("getXML.php?q=" + req); //queries my PHP file for data using the string (works)
var myEntry:URLLoader = new URLLoader();
myEntry.addEventListener(Event.COMPLETE, loadedMyEntry);
myEntry.load(request);

function loadedMyEntry(event:Event):String
{
var loader2:URLLoader = URLLoader(event.target);
return loader2.data;//how the heck do i get it out of here into myTextField or any other thing I want to use it for?
}
}
}
}Hopefully you can see that I'm trying to write a function that pulls data from a PHP file using a string for an argument, and I want that same function to return the results so that I can assign it to whatever I want. Can someone help me understand how to accomplish this with AC3/OOP? Thanks so much!

MannPohir
December 22nd, 2008, 10:47 PM
Hi,

Please check the return function of AS3 and there must be a variable to store a returned value.





return (loader2.data)

sans_devin
December 22nd, 2008, 11:01 PM
Ok, I've added the parenthesis and read about AS3's return function but I still don't see what I'm doing wrong. The variable I'm trying to assign it to is on line 11 (it's a text field on the stage). It tells me "Line 26: Function does not return a value." How do I get the function called from the Event Listener to return to it's parent which will return to the parent's original call? I know I'll need to re-write my code to accomplish this, I just don't know how to.

Here's a quick update to my code:


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

public class pixel extends Sprite
{
public function pixel()
{
myTextField.text = getEntry("pages.page[1].pageName");
}

private dynamic function getEntry(req:String):String
{
var request:URLRequest = new URLRequest("getXML.php?q=" + req);
var myEntry:URLLoader = new URLLoader();
myEntry.addEventListener(Event.COMPLETE, loadedMyEntry);
myEntry.load(request);

function loadedMyEntry(event:Event):String
{
var loader2:URLLoader = URLLoader(event.target);
return (loader2.data);
}
}
}
}

Any help's appreciated. Thanks!

scottc
December 22nd, 2008, 11:06 PM
don't nest your functions, i see a return for the nested function, but none for the getEntry function.

set it to :void if your not planning on returning anything.

sans_devin
December 22nd, 2008, 11:40 PM
Yeah, thanks for the points... I knew those would be problems but even if I corrected the 'void' and/or de-nested my function, it still doesn't solve the problem.

I've figured out something that works but I'm sure it isn't the best way to accomplish this kind of thing:


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

public class pixel extends Sprite
{
var myObj:Object;
public function pixel()
{
getEntry("pages.page[1].pageName", myTextField);
}

private function getEntry(req:String, theObj:Object):void
{
var request:URLRequest = new URLRequest("getXML.php?q=" + req);
var myEntry:URLLoader = new URLLoader();
myEntry.addEventListener(Event.COMPLETE, loadedMyEntry);
myEntry.load(request);
myObj = theObj;

function loadedMyEntry(event:Event):void
{
var loader2:URLLoader = URLLoader(event.target);
if(getQualifiedClassName(myObj).search('TextField' ) != -1)
{
myObj.text = loader2.data;
}
}
}
}
}

Now I just have to create if/case statements to account for all the possible object types that I'll want to use with this function but this is going to be a pain. There's got to be a way that on line 13 I can simply say...


myTextField.text = getEntry("pages.page[1].pageName");

Any ideas?