Skip to content

June 28, 2010

1

AS3 _targetInstanceName equivalent is scrollTargetName

Back in the MX2004 day we could use the _xch clip in the Flash IDE to help us determine the name of any asset we dropped our component onto. Excessive Googling hasn’t turned up anything useful so, dear reader, this is for you.

In the CS4 library, select your component clip, right-click and choose ‘Component Definition‘. Add a parameter called scrollTargetName, give it a variable name of ‘scrollTargetName‘. In your component class, ensure you have getters and setters for ‘scrollTargetName‘ and for ‘scrollTarget‘, like this:

public class MyComponent extends DisplayObject{
        private var _scrollTarget : DisplayObject;
        public function RevealComponent() {
                init();
        }

        private function init():void {

        }

        public function set scrollTarget( value : DisplayObject ) : void {
                _scrollTarget = value;
        }

        public function get scrollTarget( ) : DisplayObject {
                return _scrollTarget;
        }

        [Inspectable()]
        public function get scrollTargetName():String {
                return _scrollTarget.name;
        }

        public function set scrollTargetName(target:String):void {
                try {
                        scrollTarget = parent.getChildByName(target);
                } catch (error:Error) {
                        throw new Error("ScrollTarget not found");
                }
        }

}

Now update your component by selecting ‘Component Definition’ again, but this time just clik ‘OK‘ to force the update.

Now place a TextField or MovieClip onstage and give it an instance name, then drag your updated component from the library onto the test clip. If you check the Component Parameters panel you should see that it’s automagically populated with the instance name of your example clip, just like _targetInstanceName used to. Dragging it onto other named instances should also update the parameter in the panel. Dragging your component onto an unnamed instance forces a default instance name, couldn’t be sweeter!

I also implemented some functionality that (via the SWFPanel of my component) auto-positions the dropped component instance at the top-left of its’ target instance. Let me know in the comments if you want to see how this is done.

1 Comment Post a comment
  1. Sep 30 2011

    Awesome to find this – I’ve been searching for this functionality for some time.

    It would be phenomenal to see an implementation that auto-positions itself to the dropped component.

    Thanks so much for this article!

    Jason Sturges

    Reply

Share your thoughts, post a comment.

(required)
(required)

Note: HTML is allowed. Your email address will never be published.

Subscribe to comments