Re: [Flashcoders] Syntax for dynamically calling a function

2007-05-06 Thread Muzak
I mentioned this in another thread not to long ago.
http://www.mail-archive.com/flashcoders@chattyfig.figleaf.com/msg31071.html
http://www.mail-archive.com/flashcoders@chattyfig.figleaf.com/msg31112.html

regards,
Muzak

- Original Message - 
From: Jesse Graupmann [EMAIL PROTECTED]
To: [EMAIL PROTECTED]
Sent: Sunday, May 06, 2007 4:09 AM
Subject: RE: [Flashcoders] Syntax for dynamically calling a function


 Any details as to why?

 -Original Message-
 From: [EMAIL PROTECTED]
 [mailto:[EMAIL PROTECTED] On Behalf Of Muzak
 Sent: Friday, May 04, 2007 11:41 PM
 To: flashcoders@chattyfig.figleaf.com
 Subject: Re: [Flashcoders] Syntax for dynamically calling a function


 - Original Message - 
 From: Jesse Graupmann [EMAIL PROTECTED]
 To: flashcoders@chattyfig.figleaf.com
 Sent: Friday, May 04, 2007 10:05 PM
 Subject: RE: [Flashcoders] Syntax for dynamically calling a function



 My personal favorite is the use of the Delegate loaded with arguments.


 Happens to be the one I'd say you should avoid ;-)

 regards,
 Muzak



___
Flashcoders@chattyfig.figleaf.com
To change your subscription options or search the archive:
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders

Brought to you by Fig Leaf Software
Premier Authorized Adobe Consulting and Training
http://www.figleaf.com
http://training.figleaf.com


Re: [Flashcoders] Syntax for dynamically calling a function

2007-05-05 Thread Muzak

- Original Message - 
From: Jesse Graupmann [EMAIL PROTECTED]
To: flashcoders@chattyfig.figleaf.com
Sent: Friday, May 04, 2007 10:05 PM
Subject: RE: [Flashcoders] Syntax for dynamically calling a function



 My personal favorite is the use of the Delegate loaded with arguments.


Happens to be the one I'd say you should avoid ;-)

regards,
Muzak 


___
Flashcoders@chattyfig.figleaf.com
To change your subscription options or search the archive:
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders

Brought to you by Fig Leaf Software
Premier Authorized Adobe Consulting and Training
http://www.figleaf.com
http://training.figleaf.com


Re: [Flashcoders] Syntax for dynamically calling a function

2007-05-04 Thread Alistair Colling
Wow, thanks for all your suggestions guys, Lists, I really appreciate  
the time you took to write your detailed response, I like your third  
option the best. David thanks for your suggestion I am going to do  
some further experimentation. I'll also have a look into the Event  
Dispatcher class as this looks interesting but I'm not sure what  
specific benefits it brings me here, I will do more research to find  
out. For the moment I think I will stick with putting this code  
inside my movie clip, and the do_ functions on the main timeline:


this.onPress = function(){
this._parent[do_ + this._name].call();
}
//
Thanks again everyone :)


On 3 May 2007, at 17:59, Lists wrote:


The short answer is, you can invoke the function directly by writing:

this.onPress = function () {
_parent[this._name]();
}

or, if this looks clearer, you can use the call() method

this.onPress = function () {
_parent[this._name].call();
}

However, there are a few issues with this approach.

First, I don't like the fact that there are two things with the  
same name in
the same scope: a function and a movie clip. This might just be me  
being

picky, but I'll show you an example of where this breaks in a moment.

Second, it's not best practice to put scripts inside movie clips. It's
easier to assign these functions from the main timeline.

Third, I'm not sure what this gains you. It's easier to copy and  
paste but,
if you're already going into the movie clip manually, you might as  
well just
type the function name. Maybe if you reused the clip from movie to  
movie
this step would already be done and you'd only have to write the  
function...

Or maybe I'm missing another time saving purpose.

Combining all three of the above issues, you can automatically assign
functions to all your movie clips from the the main timeline by  
writing

this:

(assumes mcs on stage named pictures, help, and home, and  
gets around

same-name-in-same-scope by adding a do_ prefix to the function name)

//
for (var s:String in this) {
if (typeof(this[s]) == movieclip) {
this[x].onPress = function () {
this._parent[do_ + this._name].call();
}
}
}
function do_pictures() {
trace(pictures);
}
function do_help() {
trace(help);
}
function do_home() {
trace(home);
}
//

This will cause EVERY mc in the main timeline to behave this way,  
so this
approach may not be what you want, but it's a proof of concept. You  
could do
this through an array of all mcs you want the function assigned to,  
or even

do it manually like this:

pictures.onPress = function () {
this._parent[do_ + this._name].call();
}

But, again, without some sort of automation in applying the script,  
you lose

the benefit. You might as well just write:

pictures.onPress = function () {
do_pictures();
}

If you want to see an example of the same-name-in-same-scope  
problem, run
the first example above after removing do_ from everything. The  
script
will work but when it goes through and assigns the onPress event  
handler,
it will assign it to the function pictures rather than the mc  
pictures. The

function is seen before the mc in the execution order of the loop.

Finally, if the lowercase string movieclip for typeof is  
confusing, you

can substitute:

if (this[x] instanceof MovieClip) {


___
Flashcoders@chattyfig.figleaf.com
To change your subscription options or search the archive:
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders

Brought to you by Fig Leaf Software
Premier Authorized Adobe Consulting and Training
http://www.figleaf.com
http://training.figleaf.com




___
Flashcoders@chattyfig.figleaf.com
To change your subscription options or search the archive:
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders

Brought to you by Fig Leaf Software
Premier Authorized Adobe Consulting and Training
http://www.figleaf.com
http://training.figleaf.com


RE: [Flashcoders] Syntax for dynamically calling a function

2007-05-04 Thread Jesse Graupmann

My personal favorite is the use of the Delegate loaded with arguments.



//
//  INTERACTION
//


function button_onPress ( btn:MovieClip ) 
{
var data = dataArray [ btn.id ];
var func = this [ 'func_' + data.section ];

func ( data );
}


//
//  EVENTS
//


function func_work ( data:Object ) {trace( data.btn );  }
function func_portfolio ( data:Object ) {   trace( data.id );   }
function func_about ( data:Object ) {   trace( data.section );  }


//
//  UTILS
//


function proxyBEFORE (s:Object, func:Function):Function
{
// from jgDelegate.as
var a:Array = arguments.slice(2, arguments.length);
return function ():Void { func.apply(s, a.concat(arguments));   };
}


//
//  INIT
//


var dataArray = [ {section:'work'}, {section:'portfolio'}, {section:'about'}
];

var len = dataArray.length;
for ( var i = 0; ilen; i++) 
{
var btn = holder [ 'btn_' + dataArray [i].section ];

dataArray[i].id = btn.id = i;
dataArray[i].btn = btn;

btn.onPress = proxyBEFORE ( this, button_onPress, btn );
}



_

Jesse Graupmann
www.jessegraupmann.com   
www.justgooddesign.com/blog/
_



-Original Message-
From: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED] On Behalf Of Alistair
Colling
Sent: Friday, May 04, 2007 8:59 AM
To: flashcoders@chattyfig.figleaf.com
Subject: Re: [Flashcoders] Syntax for dynamically calling a function

Wow, thanks for all your suggestions guys, Lists, I really appreciate  
the time you took to write your detailed response, I like your third  
option the best. David thanks for your suggestion I am going to do  
some further experimentation. I'll also have a look into the Event  
Dispatcher class as this looks interesting but I'm not sure what  
specific benefits it brings me here, I will do more research to find  
out. For the moment I think I will stick with putting this code  
inside my movie clip, and the do_ functions on the main timeline:

this.onPress = function(){
this._parent[do_ + this._name].call();
}
//
Thanks again everyone :)


On 3 May 2007, at 17:59, Lists wrote:

 The short answer is, you can invoke the function directly by writing:

 this.onPress = function () {
 _parent[this._name]();
 }

 or, if this looks clearer, you can use the call() method

 this.onPress = function () {
 _parent[this._name].call();
 }

 However, there are a few issues with this approach.

 First, I don't like the fact that there are two things with the  
 same name in
 the same scope: a function and a movie clip. This might just be me  
 being
 picky, but I'll show you an example of where this breaks in a moment.

 Second, it's not best practice to put scripts inside movie clips. It's
 easier to assign these functions from the main timeline.

 Third, I'm not sure what this gains you. It's easier to copy and  
 paste but,
 if you're already going into the movie clip manually, you might as  
 well just
 type the function name. Maybe if you reused the clip from movie to  
 movie
 this step would already be done and you'd only have to write the  
 function...
 Or maybe I'm missing another time saving purpose.

 Combining all three of the above issues, you can automatically assign
 functions to all your movie clips from the the main timeline by  
 writing
 this:

 (assumes mcs on stage named pictures, help, and home, and  
 gets around
 same-name-in-same-scope by adding a do_ prefix to the function name)

 //
 for (var s:String in this) {
 if (typeof(this[s]) == movieclip) {
 this[x].onPress = function () {
 this._parent[do_ + this._name].call();
 }
 }
 }
 function do_pictures() {
 trace(pictures);
 }
 function do_help() {
 trace(help);
 }
 function do_home() {
 trace(home);
 }
 //

 This will cause EVERY mc in the main timeline to behave this way,  
 so this
 approach may not be what you want, but it's a proof of concept. You  
 could do
 this through an array of all mcs you want the function assigned to,  
 or even
 do it manually like this:

 pictures.onPress = function () {
 this._parent[do_ + this._name].call();
 }

 But, again, without some sort of automation in applying the script,  
 you lose
 the benefit. You might as well just write:

 pictures.onPress = function () {
 do_pictures();
 }

 If you want to see an example of the same-name-in-same-scope  
 problem, run
 the first example above after removing do_ from everything. The  
 script
 will work but when it goes through and assigns the onPress event  
 handler,
 it will assign it to the function pictures rather than the mc  
 pictures. The
 function is seen before the mc in the execution order of the loop.

 Finally, if the lowercase string movieclip for typeof is  
 confusing, you
 can substitute:

 if (this[x] instanceof MovieClip

Re: [Flashcoders] Syntax for dynamically calling a function

2007-05-03 Thread Robert Brisita

You could do something like this:

class Something
{
   method1();
   method2(a_number:Number);
}

Somewhere else:
var method_str:String = method1;
var sm:Something = new Something();

sm[method_str](); // Will call method1 class method
sm[method2](0x09F911029D74E35BD84156C5635688C0); // Also works ;-)

Ciao,
Rob.

Alistair Colling wrote:
Hi there, I want to call a function but want to able to call it 
dynamically so a string that is passed will determine which function 
is called.
My reason for this is I have an interface with a number of buttons 
that have different labels but that look the same and need to call 
different functions. I was going to make one button and duplicate it 
then name it so it will call a function depending on what it's name 
is. My code would go something like this:


///inside 'button' MC named 'pictures'
var label:String = this._name

this.onPress = function(){
//want to call function from here, dictated by the name of the MC, 
not sure of this sytanx

this._parent.label()
}
main timeline
function pictures(){
//to be called from pictures button onPress event
}
///
Any suggestions much appreciated!
Ali





___
Flashcoders@chattyfig.figleaf.com
To change your subscription options or search the archive:
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders

Brought to you by Fig Leaf Software
Premier Authorized Adobe Consulting and Training
http://www.figleaf.com
http://training.figleaf.com


--No virus found in this incoming message.
Checked by AVG Free Edition.
Version: 7.5.446 / Virus Database: 269.6.2/781 - Release Date: 
4/30/2007 9:14 AM





___
Flashcoders@chattyfig.figleaf.com
To change your subscription options or search the archive:
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders

Brought to you by Fig Leaf Software
Premier Authorized Adobe Consulting and Training
http://www.figleaf.com
http://training.figleaf.com


RE: [Flashcoders] Syntax for dynamically calling a function

2007-05-03 Thread Danny Kodicek
 

 -Original Message-
 From: [EMAIL PROTECTED] 
 [mailto:[EMAIL PROTECTED] On Behalf 
 Of Alistair Colling
 Sent: 03 May 2007 15:27
 To: flashcoders@chattyfig.figleaf.com
 Subject: [Flashcoders] Syntax for dynamically calling a function
 
 Hi there, I want to call a function but want to able to call 
 it dynamically so a string that is passed will determine 
 which function is called.
 My reason for this is I have an interface with a number of 
 buttons that have different labels but that look the same and 
 need to call different functions. I was going to make one 
 button and duplicate it then name it so it will call a 
 function depending on what it's name is. My code would go 
 something like this:
 
 ///inside 'button' MC named 'pictures'
 var label:String = this._name
 
 this.onPress = function(){
   //want to call function from here, dictated by the name 
 of the MC, not sure of this sytanx
   this._parent.label()

Nearly right: 

this._parent[label]()

Danny

___
Flashcoders@chattyfig.figleaf.com
To change your subscription options or search the archive:
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders

Brought to you by Fig Leaf Software
Premier Authorized Adobe Consulting and Training
http://www.figleaf.com
http://training.figleaf.com


Re: [Flashcoders] Syntax for dynamically calling a function

2007-05-03 Thread R�kos Attila

AC Hi there, I want to call a function but want to able to call it
AC dynamically so a string that is passed will determine which function  
AC is called.
AC My reason for this is I have an interface with a number of buttons  
AC that have different labels but that look the same and need to call  
AC different functions. I was going to make one button and duplicate it  
AC then name it so it will call a function depending on what it's name  
AC is. My code would go something like this:
AC 
AC ///inside 'button' MC named 'pictures'
AC var label:String = this._name
AC 
AC this.onPress = function(){
AC //want to call function from here, dictated by the name of the MC,  
AC not sure of this sytanx
AC this._parent.label()
AC }
AC main timeline
AC function pictures(){
AC //to be called from pictures button onPress event
AC }
AC ///
AC Any suggestions much appreciated!

this._parent[label]();

or

this._parent[label].call(_this.parent)

or

this._parent[label].apply(_this.parent)

But I have to mention that this is a really bad practice and I suggest
you to avoid it. It is far better to store a call-back reference in
your button instances or use a more sophisticated event dispatching
mechanism (mx.events.EventDispatcher, AsBroadcaster, etc.).


var onClick: Function;

this.onPress = function(){
  this.onClick();
}

and then:

function clickHandler1() {
  trace(button 1 clicked);
}

function clickHandler2() {
  trace(button 2 clicked);
}

my_button1.onClick = clickHandler1;
my_button2.onClick = clickHandler2;

and so on (but event dispatching is really more flexible)

  Attila

___
Flashcoders@chattyfig.figleaf.com
To change your subscription options or search the archive:
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders

Brought to you by Fig Leaf Software
Premier Authorized Adobe Consulting and Training
http://www.figleaf.com
http://training.figleaf.com


Re: [Flashcoders] Syntax for dynamically calling a function

2007-05-03 Thread Alistair Colling
Thanks Robert, I didn't know you could do that. It's a shame you  
can't use the same square bracket syntax  in the FLA without creating  
a custom class, still this is a neat solution :)

Cheers!
Ali


On 3 May 2007, at 16:04, Robert Brisita wrote:


You could do something like this:

class Something
{
   method1();
   method2(a_number:Number);
}

Somewhere else:
var method_str:String = method1;
var sm:Something = new Something();

sm[method_str](); // Will call method1 class method
sm[method2](0x09F911029D74E35BD84156C5635688C0); // Also works ;-)

Ciao,
Rob.

Alistair Colling wrote:
Hi there, I want to call a function but want to able to call it  
dynamically so a string that is passed will determine which  
function is called.
My reason for this is I have an interface with a number of buttons  
that have different labels but that look the same and need to call  
different functions. I was going to make one button and duplicate  
it then name it so it will call a function depending on what it's  
name is. My code would go something like this:


///inside 'button' MC named 'pictures'
var label:String = this._name

this.onPress = function(){
//want to call function from here, dictated by the name of the  
MC, not sure of this sytanx

this._parent.label()
}
main timeline
function pictures(){
//to be called from pictures button onPress event
}
///
Any suggestions much appreciated!
Ali





___
Flashcoders@chattyfig.figleaf.com
To change your subscription options or search the archive:
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders

Brought to you by Fig Leaf Software
Premier Authorized Adobe Consulting and Training
http://www.figleaf.com
http://training.figleaf.com


--No virus found in this incoming message.
Checked by AVG Free Edition.
Version: 7.5.446 / Virus Database: 269.6.2/781 - Release Date:  
4/30/2007 9:14 AM





___
Flashcoders@chattyfig.figleaf.com
To change your subscription options or search the archive:
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders

Brought to you by Fig Leaf Software
Premier Authorized Adobe Consulting and Training
http://www.figleaf.com
http://training.figleaf.com




___
Flashcoders@chattyfig.figleaf.com
To change your subscription options or search the archive:
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders

Brought to you by Fig Leaf Software
Premier Authorized Adobe Consulting and Training
http://www.figleaf.com
http://training.figleaf.com


RE: [Flashcoders] Syntax for dynamically calling a function

2007-05-03 Thread David Ngo
Couldn't you just use call() or apply()?

http://livedocs.adobe.com/flash/8/main/2234.html



-Original Message-
From: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED] On Behalf Of Robert
Brisita
Sent: Thursday, May 03, 2007 11:05 AM
To: flashcoders@chattyfig.figleaf.com
Subject: Re: [Flashcoders] Syntax for dynamically calling a function

You could do something like this:

class Something
{
method1();
method2(a_number:Number);
}

Somewhere else:
var method_str:String = method1;
var sm:Something = new Something();

sm[method_str](); // Will call method1 class method
sm[method2](0x09F911029D74E35BD84156C5635688C0); // Also works ;-)

Ciao,
Rob.

Alistair Colling wrote:
 Hi there, I want to call a function but want to able to call it 
 dynamically so a string that is passed will determine which function 
 is called.
 My reason for this is I have an interface with a number of buttons 
 that have different labels but that look the same and need to call 
 different functions. I was going to make one button and duplicate it 
 then name it so it will call a function depending on what it's name 
 is. My code would go something like this:

 ///inside 'button' MC named 'pictures'
 var label:String = this._name

 this.onPress = function(){
 //want to call function from here, dictated by the name of the MC, 
 not sure of this sytanx
 this._parent.label()
 }
 main timeline
 function pictures(){
 //to be called from pictures button onPress event
 }
 ///
 Any suggestions much appreciated!
 Ali





 ___
 Flashcoders@chattyfig.figleaf.com
 To change your subscription options or search the archive:
 http://chattyfig.figleaf.com/mailman/listinfo/flashcoders

 Brought to you by Fig Leaf Software
 Premier Authorized Adobe Consulting and Training
 http://www.figleaf.com
 http://training.figleaf.com


 --No virus found in this incoming message.
 Checked by AVG Free Edition.
 Version: 7.5.446 / Virus Database: 269.6.2/781 - Release Date: 
 4/30/2007 9:14 AM



___
Flashcoders@chattyfig.figleaf.com
To change your subscription options or search the archive:
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders

Brought to you by Fig Leaf Software
Premier Authorized Adobe Consulting and Training
http://www.figleaf.com
http://training.figleaf.com

___
Flashcoders@chattyfig.figleaf.com
To change your subscription options or search the archive:
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders

Brought to you by Fig Leaf Software
Premier Authorized Adobe Consulting and Training
http://www.figleaf.com
http://training.figleaf.com


Re: [Flashcoders] Syntax for dynamically calling a function

2007-05-03 Thread Lists
The short answer is, you can invoke the function directly by writing:

this.onPress = function () {
_parent[this._name]();
}

or, if this looks clearer, you can use the call() method

this.onPress = function () {
_parent[this._name].call();
}

However, there are a few issues with this approach.

First, I don't like the fact that there are two things with the same name in
the same scope: a function and a movie clip. This might just be me being
picky, but I'll show you an example of where this breaks in a moment.

Second, it's not best practice to put scripts inside movie clips. It's
easier to assign these functions from the main timeline.

Third, I'm not sure what this gains you. It's easier to copy and paste but,
if you're already going into the movie clip manually, you might as well just
type the function name. Maybe if you reused the clip from movie to movie
this step would already be done and you'd only have to write the function...
Or maybe I'm missing another time saving purpose.

Combining all three of the above issues, you can automatically assign
functions to all your movie clips from the the main timeline by writing
this:

(assumes mcs on stage named pictures, help, and home, and gets around
same-name-in-same-scope by adding a do_ prefix to the function name)

//
for (var s:String in this) {
if (typeof(this[s]) == movieclip) {
this[x].onPress = function () {
this._parent[do_ + this._name].call();
}
}
}
function do_pictures() {
trace(pictures);
}
function do_help() {
trace(help);
}
function do_home() {
trace(home);
}
//

This will cause EVERY mc in the main timeline to behave this way, so this
approach may not be what you want, but it's a proof of concept. You could do
this through an array of all mcs you want the function assigned to, or even
do it manually like this:

pictures.onPress = function () {
this._parent[do_ + this._name].call();
}

But, again, without some sort of automation in applying the script, you lose
the benefit. You might as well just write:

pictures.onPress = function () {
do_pictures();
}

If you want to see an example of the same-name-in-same-scope problem, run
the first example above after removing do_ from everything. The script
will work but when it goes through and assigns the onPress event handler,
it will assign it to the function pictures rather than the mc pictures. The
function is seen before the mc in the execution order of the loop.

Finally, if the lowercase string movieclip for typeof is confusing, you
can substitute:

if (this[x] instanceof MovieClip) {


___
Flashcoders@chattyfig.figleaf.com
To change your subscription options or search the archive:
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders

Brought to you by Fig Leaf Software
Premier Authorized Adobe Consulting and Training
http://www.figleaf.com
http://training.figleaf.com


Re: [Flashcoders] Syntax for dynamically calling a function

2007-05-03 Thread Lists
That¹s good to know. Thanks, Steven.

 IIRC, instanceof is faster than typeof.



___
Flashcoders@chattyfig.figleaf.com
To change your subscription options or search the archive:
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders

Brought to you by Fig Leaf Software
Premier Authorized Adobe Consulting and Training
http://www.figleaf.com
http://training.figleaf.com