Ultrashock Forums > Flash > ActionScript
AS2 - Pass movieclip instance name ?

You are currently viewing our website as a guest which gives you limited access to forums, files and other resources.

Click here to join now for free, and start interacting with our members, download files and much more!

Click here if you are looking for our Flash files and other professional assets.
 
Post Reply | View first unread | Rate Thread Search this Thread | Thread Tools | Display Modes

#1
Bookmark and Share!
AS2 - Pass movieclip instance name ?
Old 2008-03-13

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:
Code:
	var X = _root.X.onPress = Delegate.create (this, buttonClick);
		X.button = "X";
Button function:
Code:
	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!
postbit arrow 6 comments | 4106 views postbit arrow Reply: with Quote   
Registered User
bob_300 is offline
seperator
Posts: 71
2008-01-20
seperator

Ultrashock Member Comments:
-Loren- -Loren- is offline -Loren- lives in United States 4 Creative Assets 2008-03-13 #2 Old  
You can try using
ActionScript Code:
  1. MovieClip._name

so,
ActionScript Code:
  1. function buttonClick ()
  2. {
  3.     textBox.text = arguments.caller._name;
  4. }

You might need to try this, not sure:

ActionScript Code:
  1. function buttonClick ()
  2. {
  3.     textBox.text = MovieClip(arguments.caller)._name;
  4. }

Let me know if this works for you
Reply With Quote  
bob_300 bob_300 is offline 2008-03-13 #3 Old  
Thanks, I tried that but no joy. It traces undefined;

If trace(this); within button click I get an response of [object Object]. Could the name be within this object?
Reply With Quote  
-Loren- -Loren- is offline -Loren- lives in United States 4 Creative Assets 2008-03-13 #4 Old  
You could definitely try that, but it shouldn't work since you're Delegating that function call. Try it I suppose
Reply With Quote  
Nutrox's Avatar Nutrox Nutrox is online now Super Moderator Nutrox lives in United Kingdom 17 Creative Assets 2008-03-13 #5 Old  
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:

ActionScript 2.0 Code:
  1. function a():Void
  2. {
  3.     b();
  4. }
  5.  
  6. function b():Void
  7. {
  8.     trace( arguments.caller == a ); // true
  9. }
  10.  
  11. a();
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:

ActionScript 2.0 Code:
  1. import neondust.utils.Delegate;
  2.  
  3. someButton.onRelease = Delegate.create( this, buttonClick, someButton );
  4.  
  5. function buttonClick( target:MovieClip ):Void
  6. {
  7.     trace( this ); // _level0
  8.     trace( target == someButton ); // true
  9.     trace( target._name ); // someButton
  10. }
Here is the class:

ActionScript 2.0 Code:
  1. class neondust.utils.Delegate
  2. {
  3.     public static function create( scope:Object, func:Function ):Function
  4.     {
  5.         var args:Array = arguments.splice( 2 );
  6.         return function()
  7.         {
  8.             return func.apply( scope, args.concat( arguments ) );
  9.         }
  10.     }
  11. }
Your custom arguments will precede any of the default arguments sent to the function by Flash, if any.

Reply With Quote  
bob_300 bob_300 is offline 2008-03-13 #6 Old  
Hey thanks, thats great it works a treat!

It is a lot easier this way, but is it also better performance wise?
Reply With Quote  
Nutrox's Avatar Nutrox Nutrox is online now Super Moderator Nutrox lives in United Kingdom 17 Creative Assets 2008-03-13 #7 Old  
Having lots of variables shouldn't slow down the movie really, the main cause of slowdown would be the graphics. If your movie is running slow then the graphics are the first thing you should try to optimise.
Reply With Quote  
Thread Tools
Display Modes Rate This Thread
Rate This Thread: