Dynamically create the content of a function?

How do you dynamically create the content of a function?

Page 1 of 1

9 Replies - 1764 Views - Last Post: 28 April 2009 - 06:27 PM

#1 Maxx  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 16-September 06

Dynamically create the content of a function?

Posted 25 April 2009 - 10:21 AM

How do you dynamically create the content of a function?

For example we create 3 buttons in a loop with an "OnRelease" function.
Within the buttons onRelease function, MyFunction + i will be called...but it isnt because the variable is never passed within the function.
How would you do that then :blink:

for (i=1; i<=3; i++) {

	this["my_bnt"+i].onRelease = function() {

this["My_function"+i]();  //<<<<Dynamically  create function name

//Or to populate any variables inside a function while in a loop :(

	};
}


//Call on these after the buttons have been created
this.My_function1 = function() {
	trace("Function 1 hit");
};
this.My_function2 = function() {
	trace("Function 2 hit");
};
this.My_function3 = function() {
	trace("Function 3 hit");
};

This post has been edited by Maxx: 25 April 2009 - 10:25 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Dynamically create the content of a function?

#2 Aurel300  Icon User is offline

  • D.I.C Head

Reputation: 13
  • View blog
  • Posts: 230
  • Joined: 10-November 07

Re: Dynamically create the content of a function?

Posted 26 April 2009 - 04:36 AM

Well, I got similar problem when I was doing enemy scripts on a game.
And each enemy needed an unique script... So I did it like this:
(Oh and doing this["My_function" + i](); is ... stupid because - 1. this is now (in the function) referring to the button. You would have to use this.parent. 2. "i" is going to be 3 on the last call, so most likely, it would always call the 3rd function. 3. doing "blahblah"(); is... weird. Just pointing out your bugs so you know what to don´t do next time).

Now, you need to set a variable for the button so:
this["my_bnt"+i].callNumber = i;



Next, when you are calling it do:
this["my_bnt"+i].onRelease = function(){
eval("My_function" + this.callNumber);
};



Eval is very good thing - if you enter a correct script in form of a string, it will execute it. Very useful for dynamic scripts.

NOTE: If this scripts throws an error, first change the line with eval to:
eval("My_function" + this.callNumber + "()");


- not sure, as I didn´t tested this.
And if that doesn´t work too, post your whole code and the error output.

This post has been edited by Aurel300: 26 April 2009 - 04:37 AM

Was This Post Helpful? 0
  • +
  • -

#3 Maxx  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 16-September 06

Re: Dynamically create the content of a function?

Posted 26 April 2009 - 11:15 AM

HI Aurel300,
Thank you for the replay :)

I almost understand what you are telling me, but I cant get it to work.

I want to point to a function inside the "onrelease function" EG:"my_function" + i .

for (i=1; i<=3; i++) {

	this["my_bnt"+i].onRelease = function() {

		//>>>>>>POINT TO "myFunction" + i <<<<<
												 My_function +[ i ]()

	};

}


I tried this but I am not sure if I am doing it like you are telling me to:


for (i=1; i<=3; i++) {

	this["my_bnt"+i].callNumber = i;

	this["my_bnt"+i].onRelease = function() {

		eval("My_function"+this.callNumber+"()");

	};

}


//Call on these after the buttons have been created depending on the button number
this.My_function1 = function() {
	trace("Function 1 hit");
};
this.My_function2 = function() {
	trace("Function 2 hit");
};
this.My_function3 = function() {
	trace("Function 3 hit");
};

Was This Post Helpful? 0
  • +
  • -

#4 Aurel300  Icon User is offline

  • D.I.C Head

Reputation: 13
  • View blog
  • Posts: 230
  • Joined: 10-November 07

Re: Dynamically create the content of a function?

Posted 26 April 2009 - 11:40 AM

Yeah, that should do it (the second script).
Did you try it?
Or what is the error?
And try to delete the +"()" in the eval if it doesn´t work.

EDIT:
Oh, and if that doesn´t work too... You can do this:
for (i=1; i<=3; i++) {

	this["my_bnt"+i].callNumber = i;

	this["my_bnt"+i].onRelease = function() {

		eval("this.parent.My_function"+this.callNumber+"()");

	};

}


//Call on these after the buttons have been created depending on the button number
this.My_function1 = function() {
	trace("Function 1 hit");
};
this.My_function2 = function() {
	trace("Function 2 hit");
};
this.My_function3 = function() {
	trace("Function 3 hit");
};


And, again, if THAT doesn´t work, delete the +"()" in the eval.

This post has been edited by Aurel300: 26 April 2009 - 11:42 AM

Was This Post Helpful? 0
  • +
  • -

#5 Maxx  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 16-September 06

Re: Dynamically create the content of a function?

Posted 26 April 2009 - 08:27 PM

Yeah no erros or anything but no it doesnt fire off the functions I tried a number of diffrent way with the above code.

I did a trace on the "callNumber" and it is working in the loop and and while on a button by tracing:

trace(my_bnt1.callNumber);


I did a trace to make sure the button was working and they are.

I have a trace on the functions but they are not getting fired off.

Is what your are suggesting is store the number in an object and use the buttons name to retrive the value?

Is there any way to directly write the actions of a functinon without storing them in an array of object?
Was This Post Helpful? 0
  • +
  • -

#6 Aurel300  Icon User is offline

  • D.I.C Head

Reputation: 13
  • View blog
  • Posts: 230
  • Joined: 10-November 07

Re: Dynamically create the content of a function?

Posted 27 April 2009 - 11:02 AM

Hm...
Try this:
for (i=1; i<=3; i++) {

	this["my_bnt"+i].callNumber = i;

	this["my_bnt"+i].onRelease = function() {

		this.parent.func[this.callNumber].func();

	};

this.func = new Array();
this.func[1].func = function(){
trace("Bllah. Button 1.");
};
this.func[2].func = function(){
trace("Bllah. Button 2");
};
this.func[3].func = function(){
trace("Bllah. Button 3");
};
}


- . - Could think that way sooner - . -
Yes, a little sloppy way, but whatever.
At least it works... I think.

BTW:
Maybe you will need to use this._parent, not just this.parent...

This post has been edited by Aurel300: 27 April 2009 - 11:03 AM

Was This Post Helpful? 0
  • +
  • -

#7 blake912  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 18
  • Joined: 15-April 09

Re: Dynamically create the content of a function?

Posted 27 April 2009 - 12:43 PM

declare the variable outside the function. then have the loop update the external variable each pass.

+=, -= ... etc
hope this helps
Was This Post Helpful? 0
  • +
  • -

#8 Maxx  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 16-September 06

Re: Dynamically create the content of a function?

Posted 27 April 2009 - 09:07 PM

I have to apologize for taking up each of your time, and thank you for taking the time to help me :)

Are you suggesting storing the name of the button and associating a variable to the buttons name in an object and/or an array?
I can see that would work…but there is no way to pass the value directly into the function while being created? That is what I am quessing you are telling me :(

Let me put it really simple because I am not getting it without storing the value outside of the function :blink:

If I simply want to tell the function call to gotoAndStop(i), I don’t understand the method in doing this to directly influence the function. :blink:

for (i=1; i<=3; i++) {

	this["my_bnt"+i].onRelease = function() {
		
		gotoAndStop(i);
		
	};
}

Was This Post Helpful? 0
  • +
  • -

#9 Aurel300  Icon User is offline

  • D.I.C Head

Reputation: 13
  • View blog
  • Posts: 230
  • Joined: 10-November 07

Re: Dynamically create the content of a function?

Posted 28 April 2009 - 10:49 AM

Well, when you use a for loop, it will make a temporary variable i, which will however be deleted at the end.
But the function just stays there and it can´t use that i, because it is deleted.
So, I think just copy my code and it should work...
Was This Post Helpful? 1
  • +
  • -

#10 Maxx  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 16-September 06

Re: Dynamically create the content of a function?

Posted 28 April 2009 - 06:27 PM

Ok thank you for you help this code below is working great...I still think its weird that I can not directly inject variables while the function is being create :blink:

for (i=1; i<=3; i++) {

	this["my_bnt"+i].callNumber = i;

	this["my_bnt"+i].onRelease = function() {
		trace("hit:"+this._name);
		this._parent["myfunction"+this.callNumber](this._name);

	};

	this["myfunction"+i] = function (button_name) {
		trace(this._name+" From: "+button_name);
	};

}

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1