View Full Version : passing a function around
GrndMasterFlash
February 24th, 2009, 05:09 PM
so, i think it's just cause it's the end of the day and my brain wants to quit, but i would like some input.
im trying to pass a function to a question box that when you click the YES button the function passed in will fire for some reason im having a hard time holding the function until the YES is clicked.
here is what i got
//the call that opens the choice box and sets the info the gui_obj part is cause the //funciton is in a separate class file
gui_obj.displayChoice("Add the cell phone to your inventory?", "YES", "NO", gui_obj.addToInventory(item))
//the function to set up everything
public function displayChoice(txt:String, yes:String, no:String, funk:Function)
{
panel_choice.txt.text = txt;
panel_choice.yes.text = yes;
panel_choice.no.text = no;
//dtrace(funk+" as "+choiceFunction);
choiceFunction = Function(funk);
panel_choice.visible = true;
}
any creative ideas on how to do this? :insomniac:
creatify
February 24th, 2009, 05:52 PM
I think your function is being executed at the point that you place it here:
gui_obj.displayChoice("Add the cell phone to your inventory?", "YES", "NO", gui_obj.addToInventory(item))
maybe try:
gui_obj.displayChoice("Add the cell phone to your inventory?", "YES", "NO", gui_obj.addToInventory, item);
so you pass item as a separate option, then within the method, you can use func(itemPassed);
I think that'll work?
GrndMasterFlash
February 25th, 2009, 09:41 AM
you rawk!
yep the function was firing off as soon as it got passed in, now i can save it and pass it along,
...
sooo...
and idea how to pass the params to the function in a way that is not specific to the function?
the code changed a little i have a 5th param that is an array of values that i want to pass to what ever function,
//
//the call
gui_obj.displayChoice("Add the cell phone to your inventory?", "YES", "NO", gui_obj.addToInventory, item)
//
//the function
public function displayChoice(txt:String, yes:String, no:String, funk:Function, ...fParamz)
{
panel_choice.txt.text = txt;
panel_choice.yes.text = yes;
panel_choice.no.text = no;
choiceFunction = funk;
panel_choice.visible = true;
}
//
//threw this one in just to give you the idea of where it's going
public function addToInventory(item:String)
{
dtrace(item+" added to inventory");
}
to note, i know i can make it work by storing the array and giving the function a default param value, however, i would like to make this note dependent on those things
creatify
February 25th, 2009, 09:50 AM
hmm, not sure I follow what you're asking. this whole passing functions thing... I think there are easier ways to achieve what you're trying to do using events maybe?
GrndMasterFlash
February 25th, 2009, 09:57 AM
ok, i was being dumb and should have play around with my code more before asking for more help, and thank you btw :)
anyways, so i can store the array, cause i just remembered that im using a buffer function, so here is the actual way it works, and it does work now :D
//
//the call
gui_obj.displayChoice("Add the cell phone to your inventory?", "YES", "NO", gui_obj.addToInventory, item)
//
//the function
public function displayChoice(txt:String, yes:String, no:String, funk:Function, ...fParamz)
{
panel_choice.txt.text = txt;
panel_choice.yes.text = yes;
panel_choice.no.text = no;
choiceFunction = funk;
choiceFarray = fParamz;
panel_choice.visible = true;
}
//
//listener attached to the no button
private function choiceNO(me:MouseEvent)
{
panel_choice.visible = false;
}
//
//listener attached to the yes button
private function choiceYES(me:MouseEvent)
{
choiceFunction(choiceFarray);
}
//
//the funciton+paramz im using in this case
public function addToInventory(item:String)
{
dtrace(item+" added to inventory");
}
IQAndreas
February 25th, 2009, 10:36 AM
Better yet, to have an unlimited amount of arguments be able to be passed in to the function, first use the change as creatify suggested:
gui_obj.displayChoice("Add the cell phone to your inventory?", "YES", "NO", gui_obj.addToInventory, item);
Then, since I don't have the rest of your code, I am not quite sure how to format this properly, but hopefully you get the point from this, but basically, use the rest parameter to allow all and any parameters:
//the function to set up everything
public function displayChoice(txt:String, yes:String, no:String, FunkObj:Object, FunkFunk:Function, ... FunkArgs)
{
panel_choice.txt.text = txt;
panel_choice.yes.text = yes;
panel_choice.no.text = no;
var choiceFunction:Function = FunkFunk;
var choiceFunctionArgs:Array = FunkArgs;
var choiceFunctionObject:Class = FunkObj;
panel_choice.visible = true;
}
Then when you want to execute the function:
choiceFunction.apply(choiceFunctionObject, choiceFunctionArgs);
//Or you might have to use choiceFunction.call() method, but I have used neither, so I am not 100% sure.
To make things easier, I have written up a new class that allows you to run complete functions by calling the execute() method.
/* ---------------------------------------------------------------
CompleteFunction - Version 0.01
Copyright Andreas J. Renberg - February 24, 2009
TODO: Add getter setter methods for each changeable value.
Add a clone() method.
/* --------------------------------------------------------------- */
public class CompleteFunction
{
private var funct:Function;
private var object:Object;
private var args:Array;
public function CompleteFunction(newFunct:Function, newObject:Object = null, ... newArgs)
{
funct = newFunct;
object = newObject;
args = newArgs;
}
public function execute():Object
{
return funct.apply(object, args);
}
}
An example of using the above code:
//the call that opens the choice box and sets the info the gui_obj part is cause the funciton is in a separate class file
gui_obj.displayChoice("Add the cell phone to your inventory?", "YES", "NO", new CompleteFunction(gui_obj.addToInventory, gui_obj, item);
//the function to set up everything
public function displayChoice(txt:String, yes:String, no:String, funk:ChoiceFunction)
{
panel_choice.txt.text = txt;
panel_choice.yes.text = yes;
panel_choice.no.text = no;
var choiceFunction:CompleteFunction = funk;
panel_choice.visible = true;
}
//And then to execute it:
choiceFunction.execute();
More on the rest parameter, taken directly from Adobe's Programming Action Script 3 documents (installed on all computers with Flash CS3, and available online for people with CS4)
The ... (rest) parameter
ActionScript 3.0 introduces a new parameter declaration called the ... (rest) parameter. This parameter allows you to specify an array parameter that accepts any number of comma- delimited arguments. The parameter can have any name that is not a reserved word. This parameter declaration must be the last parameter specified. Use of this parameter makes the arguments object unavailable. Although the ... (rest) parameter gives you the same functionality as the arguments array and arguments.length property, it does not provide functionality similar to that provided by arguments.callee. You should ensure that you do not need to use arguments.callee before using the ... (rest) parameter.
The following example rewrites the traceArgArray() function using the ... (rest) parameter instead of the arguments object:
function traceArgArray(... args):void
{
for (var i:uint = 0; i < args.length; i++)
{
trace(args[i]);
}
}
traceArgArray(1, 2, 3);
// output:
// 1
// 2
// 3
The ... (rest) parameter can also be used with other parameters, as long as it is the last parameter listed. The following example modifies the traceArgArray() function so that its first parameter, x, is of type int, and the second parameter uses the ... (rest) parameter. The output skips the first value, because the first parameter is no longer part of the array created by the ... (rest) parameter.
function traceArgArray(x: int, ... args)
{
for (var i:uint = 0; i < args.length; i++)
{
trace(args[i]);
}
}
traceArgArray(1, 2, 3);
// output:
// 2
// 3
NOTICE: TEXT IS COPYRIGHTED BY ADOBE 2007(?)
Powered by vBulletin® Version 4.1.10 Copyright © 2012 vBulletin Solutions, Inc. All rights reserved.