00:00
00:00
Newgrounds Background Image Theme

SpeakyDooman just joined the crew!

We need you on the team, too.

Support Newgrounds and get tons of perks for just $2.99!

Create a Free Account and then..

Become a Supporter!

Can't pass function as argument

734 Views | 9 Replies
New Topic Respond to this Topic

Can't pass function as argument 2012-02-08 19:21:19


For some reason this works:

function tick(e:Event) {
				testMC.tick();
		}

But when testMC is Badguys[0] this doesn't work:

function tick(e:Event) {
				arrayfunction(Badguys, tick);
		}
		function arrayfunction(a1:Array, f1:Function) {
				var arraylength1 = a1.length;
				for (var i = arraylength1 - 1; i >= 0; i--) {
					trace (i)
					var a1target = a1[i];
					a1target.f1();
				}
			
		}

Thankyou for your expert time. I'm sure this is some sort of stupid mistake.

Response to Can't pass function as argument 2012-02-08 19:24:15


I should say that I keep getting this in my output box:
ReferenceError: Error #1069: Property f1 not found on Enemies.treeElemental and there is no default value.
at Level/arrayfunction()
at Level/tick()

Response to Can't pass function as argument 2012-02-08 19:42:43


You are passing the function as a parameter with no problem.
Your problem is here:

var a1target = a1[i];
a1target.f1();

Just as the error says, there is no "f1" property in "a1target".
You passed "f1" in as a parameter, so you call it simply by:

f1();

However, I have to ask.
What are you trying to do here:

function tick(e:Event) {
	arrayfunction(Badguys, tick);
}

I think you are misunderstanding how function references work.

Are you intentionally ignoring the event object from your event listener?
Why are you passing the callback of an event listener as a parameter to another function?

Response to Can't pass function as argument 2012-02-08 19:52:54


Where is a1target defined? Also your function is also either trying to be recursive (why?), or your object (a1target) is trying to call a function that doesn't exist.

Also why are you looping backwards?

If you explain what you're trying to do you might get more help.


The water in Majorca don't taste like what it oughta.

| AS3: Main | AS2: Main | Flash Tutorials |

BBS Signature

Response to Can't pass function as argument 2012-02-08 20:08:57


At 10 minutes ago, Diki wrote: However, I have to ask.
What are you trying to do here:

function tick(e:Event) {
arrayfunction(Badguys, tick);
}

I think you are misunderstanding how function references work.

Are you intentionally ignoring the event object from your event listener?
Why are you passing the callback of an event listener as a parameter to another function?

Firstly: THANKYOU SO MUCH for the quick response!

It's sometimes hard for me to express these things accurately as I'm quite new to all this, but as best as I can describe it:
The code above is from a Level class. TestMC is a child of the Level class.
I'm trying to run a function within TestMC named "tick" Which in turn manages all of TestMC's behavior.

This is the actual function inside TestMC:

public function tick() {
			priority();
		}

(all priority currently does is moves TestMC to the left.)

Response to Can't pass function as argument 2012-02-08 21:24:34


If your tick() is a member function of each of your TestMC objects then why can't you just put all the code inside that function?
I don't see a need for you to be passing a function reference to another function.

Perhaps I'm misunderstanding what it is that you're trying to do.
Are you essentially just trying to create an update function for your TestMC class?

Response to Can't pass function as argument 2012-02-08 22:35:50


At 52 minutes ago, Diki wrote: If your tick() is a member function of each of your TestMC objects then why can't you just put all the code inside that function?
I don't see a need for you to be passing a function reference to another function.

Perhaps I'm misunderstanding what it is that you're trying to do.
Are you essentially just trying to create an update function for your TestMC class?

(thanks again)
Yes, exactly. also, I wanted to make it so that I can satisfy all of my basic for-loop needs with this one function. So that I don't have a million different for-loops in the level class. In a sense, this does work for that, but it only allows me to use functions within the Level class. I just cant seem to get it to do anything to any of the Level class's children.

I did come up with a solution of looking at f1 as a string and then doing a kind of
if(f1=="tick")a[i].tick();

but I can't help but feel like this is a lazy, inefficient solution, created entirely by my misunderstanding of exactly how this stuff works.

as for my reason for not just using an Event.ENTER_FRAME listener: I had read that having many Event.ENTER_FRAME listeners can bog down a game. Again, I am very new at this so I just take what I read as gospel.

Response to Can't pass function as argument 2012-02-09 00:25:32


Since I don't have or want your full code, I'll just give you this. It's everything you should ever need to do with using functions as variables and parameters. For the parameters, there is a default function, and it can pass an unlimited number of parameters along with it. You can adjust this in ways to suit your situation.

function showString(cString:String = "Default string"):void
{
	trace(cString);
}

function showSum(... nums):void
{
	var i:int;
	var cSum:Number = 0;
	
	if (String(Number(nums))=="NaN"
		&& nums.length==1)
	{
		/* nums is a 2D array, which occurs if the function was called by callFunctionWithParameters with 2 or more parameters
		   Would also occur if this happened to be called with an array as the parameter */
		for(i=0; i<nums[0].length; i++)
		{
			cSum += nums[0][i];
		}
	} else
	{
		for(i=0; i<nums.length; i++)
		{
			cSum += nums[i];
		}
	}
	
	trace(cSum);
}

function callFunctionWithParameters(cFunction:Function=null, ... args):void
{
	if (cFunction == null)
	{
		cFunction = showString;
	}
	
	if (args[0] == null)
	{
		cFunction();
	} else
	{
		cFunction(args);
	}
}

function callSimpleFunction(cFunction:Function=null):void
{
	if (cFunction == null)
	{
		cFunction = showString;
	}
	
	cFunction();
}

var tFunc:Function = showSum;

showSum(2);
showSum(2, 3);
tFunc();

trace("---")
callSimpleFunction();
callSimpleFunction(showString);
callFunctionWithParameters(showString, "nom nom");
trace("---");
callFunctionWithParameters();
callFunctionWithParameters(tFunc);
callFunctionWithParameters(tFunc, 19);
callFunctionWithParameters(tFunc, 19, 3);

That outputs the following:

2
5
0
---
Default string
Default string
nom nom
---
Default string
0
19
22

I trust you can figure it out. I can answer any specific questions about it.

Response to Can't pass function as argument 2012-02-09 02:05:05


At 3 hours ago, bonkanailios wrote: I did come up with a solution of looking at f1 as a string and then doing a kind of
if(f1=="tick")a[i].tick();

forget about it.

as for my reason for not just using an Event.ENTER_FRAME listener: I had read that having many Event.ENTER_FRAME listeners can bog down a game. Again, I am very new at this so I just take what I read as gospel.

Nobody said that you should add more enter frame handlers. (it would be indeed bad for the performance)
But you can run the for loop directly in your tick function.

Response to Can't pass function as argument 2012-02-09 03:55:06


At 3 hours ago, Kellexx wrote: Since I don't have or want your full code, I'll just give you this. It's everything you should ever need to do with using functions as variables and parameters. For the parameters, there is a default function, and it can pass an unlimited number of parameters along with it. You can adjust this in ways to suit your situation.
I trust you can figure it out. I can answer any specific questions about it.

HOLY RESPONSE! Thanks so much I'm going to comb through this and figure out if it's exactly what I needed.
It looks promising though.