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
_______________________________________________
[email protected]
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