Call a dynamic function from within a custom class

Hello All

i have created a class that so far works great.

what i need to do is get that class to accept a function as one of its parameters.

so for example:

example.getClass(function_name);

and when the class is finshed to use the passed function name to call a internal function in the flash movie

I don’t even know where to begin or what its called to even do a search on it.

any help or pointers would be greatly appreciated

functions are also a class in flash. so you should be able to pass in a function…

however do you want to pass in a function or nstring name of the function ??

[quote=bugboy;2332259]Hello All

i have created a class that so far works great.

what i need to do is get that class to accept a function as one of its parameters.

so for example:

example.getClass(function_name);

and when the class is finshed to use the passed function name to call a internal function in the flash movie

I don’t even know where to begin or what its called to even do a search on it.

any help or pointers would be greatly appreciated[/quote]

Are you passing a function reference to the class so that it can be used as a callback in that class? In other words, you are telling the class what it needs to trigger when something happens in the class?

If so, I’d really recommend an EventListener system… that way you can avoid passing a function, and just have the class broadcast to listeners. Its a bit tricky if you haven’t used them before - but its a great thing to learn/use to get classes to “communicate” with other objects.

i’ve sorted it now thanks. I pass the function name as a string to the class then use.

_myTimeline_myCallback;

to call the function on completion

i did try that even listener and it worked but wasn’t that great or neat where as a callback function does the trick. i now need to give that callback function some vars to use. I know i can use {one:“me”, two;“me”}
but can’t get the dynamic function call to use them.

Will keep searching

explain the problem in details please

cool - yeah, using the evaluation is fine too… however, event listeners will start getting you prepped for as3 - its breathes event-based communication.

but, to pass args, you can simply pass your function two arguments:


var obj:Object = {a:"asdfasf", b:"sadfsff"};
yourClassFunction("yourFunctionToFire",obj);

then, within your class:


_myTimeline[_myCallback](obj.a); //obj.a would be reference to wherever/however you save that object - ie someplace you're setting _myCallback="yourFunctionToFire" - you can have _myParam = obj;

yeah that works well.

The thing is now it will allow a loop but will only do it twice before stopping and not carrying on. i’ll post my class so you can see. and the flash code to call the class.

it manages to loop through to the second key in the array then just stalls. The flash file doesn’t fall over it just acts as if its finished.

Any ideas why. ps sorry for the long post. :slight_smile:

CollectData.as



import mx.utils.Delegate; 

class CollectData {
	
	//class members
	private var _tempXml:XML;
	private var _tempArray:Object;
	private var _sendDetails:LoadVars;
	private var _xmlUrl:String;
	private var _xmlLength:Number;
	private var _subLength:Number;
	private	var _valueName:String;
	private var _delete:Boolean = false;
	private var _myCallback:Object;
	private var _myTimeline:MovieClip;
	private var _myProps:Object;
		
	private function reset(){
		// clean up after ones self
		delete _tempXml;		
		delete _tempArray;
		delete _sendDetails;
		delete _xmlUrl;
		delete _xmlLength;
		delete _subLength;
		delete _valueName;
		delete _delete;
		delete _myCallback;
		delete _myTimeline;
		delete _myProps;
		trace("reset the collectData class")
	}
	
	
	
	private	function loadData() {
			trace('sort loadData')
		if (_tempXml.status == 0) {
			trace ("XML was loaded and parsed successfully");
			return true;
		} else {
			trace ("XML was loaded successfully, but was unable to be parsed.");
			return false;
		}
	}
	
	
	
	private	function sortBase(success) {
		trace('sort base')
		if (success) {
			if (loadData(_tempXml)) {
				XmlIntoArray(_tempXml)
			}
		}
	}
	
	
	
	private	function XmlIntoArray() {
		// converts ones xml into an array
		var rootNode:XMLNode = _tempXml.firstChild;
		_xmlLength = rootNode.childNodes.length;
		for (var i = 0; i<_xmlLength; ++i) {
			_tempArray* = new Object();
			_subLength = rootNode.childNodes*.childNodes.length;
			for (var a = 0; a<_subLength; ++a) {
				_valueName = rootNode.childNodes*.childNodes[a].nodeName;
				_tempArray*[_valueName] = rootNode.childNodes*.childNodes[a].firstChild.nodeValue;
			}
		}
		
		
		// confirm everything is done.
		trace("xml turned into array");
				
		
		if(_myCallback != undefined){
			trace('a function for callback has been given');
			callback_function();
		}
		
		if(_delete == true){
			trace('class has been reset');
			reset();
		}
		
			
	}
	
	
	
	public function CollectData(){
		// Constructor function
		_tempXml = new XML;
		_tempXml.ignoreWhite = true;
		_tempXml.contentType = "text/xml";
	}
	
	
	
	private	function callback_function()
	{
		// set up array to hold callback prams
		var propArray:Array = new Array();
		
		if(_myProps != undefined){
			trace('props have been set');
			for(var i in _myProps){
				propArray.push(_myProps*);
			};
		};
		
		_myTimeline[_myCallback].apply(null, propArray);
		
		
		_myCallback = '';
	}
	
	
	
	/** 
    * goData: loads data from requested URL 
    * 
    * @param url:String the url to use 
	• @param arrayReplace:Arry to place loaded data into
    * @paran timeline:MovieClip to set scope for the callback function 
    * @param callback:String the name of the function to call back
	* @param props:Object properties to copy to the LoadVars object
    **/
	public function goData(url:String, arrayReplace:Object, deleteAfter:Boolean, timeline:MovieClip, callback:String, props:Object){
		_xmlUrl = url
		_tempArray = arrayReplace;
		_myTimeline = timeline;
		_myCallback = callback;
		_myProps = props;
				
		// decides if one should tidy ones self up
		_delete = deleteAfter;
		if(deleteAfter == undefined ){
			_delete = false;
		}
		trace('delete data is set too '+ _delete);
		
		_tempXml.onLoad = Delegate.create(this, sortBase);
		_sendDetails.sendAndLoad(_xmlUrl, _tempXml, "POST");
		
	}
	
	
	
	public function set sendData(data:Object){
		// set up load vars if needed
		_sendDetails = new LoadVars;
		for(var i in data){
			_sendDetails* = data*;
		}
	}
	
	
	
	public function get sendData():LoadVars{
		// debug checks to see if load vars has loaded data
		return _sendDetails;
	}
	
}


flash code in movie



var receivingArray:Array = new Array();
receivingArray[0] = new Array();
receivingArray[1] = new Array();
receivingArray[2] = new Array();
receivingArray[3] = new Array();



var sendingArray:Array = new Array();
sendingArray[0] = 'http://www.somesite.co.uk/contact/xml';
sendingArray[1] = 'http://www.somesite.co.uk/news/xml';
sendingArray[2] = 'http://www.somesite.co.uk/blog/xml';
sendingArray[3] = 'http://www.somesite.co.uk/page/xml/';


var sendBase:Object = new Object();
sendBase['area'] = "base";


function loop_load_data(i:Number)
{

	if( i > sendingArray.length)
	{
		return;
	}
	

var nextCount:Number = i+1;
trace(nextCount);

var base:CollectData = new CollectData();
base.sendData = sendBase;
base.goData(sendingArray*, receivingArray*, false, this, 'loop_load_data', {i:nextCount});
}



loop_load_data(0);


sample of the xml file i’m trying to load



<?xml version="1.0" encoding="ISO-8859-1"?><cms_item>
 <item>
  <id>1</id>
  <name>site name</name>
  <email>site email</email>
  <tel>site telephone</tel>
  <mob>site mobile</mob>
  <fax>site fax</fax>
  <road>site road</road>
  <town>site road</town>
  <region>site region</region>
  <postcode>site postcode</postcode>
  <title>our-atelier</title>
  <keyword>test, test</keyword>
  <dscpn>test</dscpn>
 </item>
</cms_item>


Sorry tp push this but any help or ideas would be really appreciated.

thanks :slight_smile: