I have a movieclip button on the main stage with an instance name of X. Essentially I just need to pass this instance name from the button to a function. At the moment I have:
Button code:
var X = _root.X.onPress = Delegate.create (this, buttonClick);
X.button = "X";
Button function:
function buttonClick ()
{
textBox.text = arguments.caller.button;
}
This works but as I have lots of buttons like this I have to create loads of vars, which is slowing down my movie. Does anyone know if there is another way?
Thanks!
You can try using
[as]
MovieClip._name
[/as]
so,
[as]
function buttonClick ()
{
textBox.text = arguments.caller._name;
}
[/as]
You might need to try this, not sure:
[as]
function buttonClick ()
{
textBox.text = MovieClip(arguments.caller)._name;
}
[/as]
Let me know if this works for you
- 13 March 2008 11:18 AM
-
arguments.caller is a reference to the function that called the executing function, it won’t be a reference to the object that called the executing function:
[highlight=ActionScript 2.0]function a():Void
{
b();
}
function b():Void
{
trace( arguments.caller == a ); // true
}
a();[/highlight]
I use a custom Delegate class because it allows you to pass custom arguments to a function without overwriting any of the default arguments that may be passed to the function by Flash. So, you could do something like this:
[highlight=ActionScript 2.0]import neondust.utils.Delegate;
someButton.onRelease = Delegate.create( this, buttonClick, someButton );
function buttonClick( target:MovieClip ):Void
{
trace( this ); // _level0
trace( target == someButton ); // true
trace( target._name ); // someButton
}[/highlight]
Here is the class:
[highlight=ActionScript 2.0]class neondust.utils.Delegate
{
public static function create( scope:Object, func:Function ):Function
{
var args:Array = arguments.splice( 2 );
return function()
{
return func.apply( scope, args.concat( arguments ) );
}
}
}[/highlight]
Your custom arguments will precede any of the default arguments sent to the function by Flash, if any.
- 13 March 2008 02:02 PM
-
- Log in or join for free to make a comment.


