The Ultrashock Ultra Bundle
  • Home
  • Community
  • Forum
  • Flash
  • ActionScript
  • Thread
  •  
  • Previous topic
  • Next topic
Sign up to post

Flash
 ActionScript

  • bob_300 Author 
    • 6044 
    • 0 
    • 6 
    Pass movieclip instance name ?
    Nutrox

    Last reply Mar 13 2008, 03:33 PM

    by Nutrox

    Posted: Mar 13 2008, 10:51 AM

    by bob_300

     

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! good

  6 REPLIES
 
-Loren-
1  
-Loren-

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 smilie

  • 13 March 2008 11:18 AM
  •  
bob_300 Author 
2  
bob_300

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?

  • 13 March 2008 12:28 PM
  •  
-Loren-
3  
-Loren-

You could definitely try that, but it shouldn’t work since you’re Delegating that function call.  Try it I suppose smilie

  • 13 March 2008 12:35 PM
  •  
Nutrox
4  
Nutrox

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.

smilie

  • 13 March 2008 02:02 PM
  •  
bob_300 Author 
5  
bob_300

Hey thanks, thats great it works a treat!

It is a lot easier this way, but is it also better performance wise?

  • 13 March 2008 03:19 PM
  •  
Nutrox
6  
Nutrox

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.

  • 13 March 2008 03:33 PM
  •  
  •   Log in or join for free to make a comment.
 
Topic actions
  •  Share on Facebook
  •  Share on Twitter
Topic Categories
  •  Show All Topics
  •  Development
    •  Server Side
    •  Client Side
  •  Creative Software
    •  Web
    •  Video
    •  3D
    •  Illustrator
    •  Photoshop Battles
    •  Photoshop
  •  Design
    •  Typography
    •  Resources & Insight
    •  Checkpoint
  •  Career
    •  Copyright Matters
    •  Advice & issues
    •  Job Seekers
    •  Job Offers
  •  Flash
    •  UltraMath
    •  OOP
    •  Third Party Tools
    •  Open Source alternatives
    •  Data Communication
    •  Components
    •  Flex
    •  AIR
    •  Flash Lite
    •  Flash Professional
    •  Flash Newbie
    •  ActionScript
    •  XML
  •  Lounge
    •  Polls
    •  Random Chat
    •  Showcase And Critique
    •  BombShock Award Nominations
  •  Community Essentials
    •  BombShock Award Winners
    •  Tutorials
    •  Interviews
    •  News
    •  Bitmap tutorials
Popular Topics
  • Sort by: 
  • Activity
  • Views
  • Comments
  • Likes
Advertise with us
  • Your advertisement here!
  • loading
Ultrashock
  • Creative Assets
  • Community
  • Blog
  1. Home
  2. Forum
+/-
Creative Assets
  • Categories
  • Contributors
  • How to buy
Make Money
  • Commission Rates
  • Referral Program
  • Contributor Program
Community
  • Activity Feed
  • Forum
  • Profiles
About
  • Quick Tour
  • Our History
  • Banners & Logos
Support
  • Contact Ultrashock
  • Advertise with us
  • Legal Information
  •  Keep up to date
  • Flash 775  Flash
  • Audio 6,481  Audio
  • Vector 2,130  Vectors
  • Image 12,338  Images
  • Creative Assets 21,724  Assets
  • Profiles 282,659  Members
  • Topics 93,762  Topics
  • Blog 4  Blog
  • Facebook 1,680  Facebook
  • Twitter 1,165  Twitter
  • Join our FREE monthly newsletter!
  • Archive
  • Invalid email address. Please try again.
Subscribe
  • ©2012 Ultrashock LLC - All rights reserved
  • Terms of Use
  • Privacy Policy
  • Switch to dark theme
  • RSS Feeds
  • Top

©2012 Ultrashock LLC - All rights reserved

Printed on Sat, February 04, 2012 - 18:57:10