0

Below is my AS3 code:

    public function load_swfs(event:Event)
    {
        var loader:Loader = event.target.getChildAt(0) as Loader;
        var mc:MovieClip = loader.content as MovieClip;
        var mc_name = 'somethingGeneratedElsewhere';
        this.animations[mc_name] = mc;
    }

    // --------------------------------------------------------------------
    public function add(mc_name)
    {
        //this.animations is a object that holds a bunch of dynamically loaded SWF files.
        addChild(this.animations[mc_name]);
        this.animations[mc_name].addEventListener(Event.ENTER_FRAME, enter_frame_handler);
    }

    // --------------------------------------------------------------------
    public function enter_frame_handler(e:Event)
    {
       //how do I get mc_name in this function
       // Example, if I run add('movie123'). How do I get 'movie123' in this function?
    }

If I run add('movie123'). How do I get the string 'movie123' in the enter_frame_handler function?

UPDATE

This forum post (Click Here) describes the issues I am having but doesn't provide the solution I am looking for. Luckily @kapep provided a simple solution that solved the issue.

5 Answers 5

3

The other solutions are usually the better options. If you can't dynamically add properties, you still could map the objects to the names using a Dictionary.

private var animationNames:Dictionary = new Dictionary();

public function add(mc_name)
{
    // store animation object and the name
    animationNames[this.animations[mc_name]] = mc_name; 

    addChild(this.animations[mc_name]);
    this.animations[mc_name].addEventListener(Event.ENTER_FRAME, enter_frame_handler);
}

// --------------------------------------------------------------------
public function enter_frame_handler(e:Event)
{
    var name:String = animationNames[e.currentTarget];
    // ...
}

Anyway, keep in mind that this.animations[mc_name] isn't really using the movieclips name and that it isn't possible to get the variable name that way. If you would dothis.animations[some_other_name] = this.animations[mc_name]; you would have two variables referring to the same object. There would be no way to tell which variable got used to attach the listener to. So make sure you understand the difference between a movieclip name and it's variable's name.

5
  • Yes, I was unable to dynamically add the .name property so your solution worked great. You said the other solutions are better, are there any big disadvantage to doing it this way?
    – zechdc
    Dec 17, 2011 at 4:35
  • @zechdc In most cases it's better to work with the objects and not the names. Usually e.target or e.currentTarget is all you need. It's cleaner this way.
    – kapex
    Dec 17, 2011 at 4:40
  • 1
    Its roundabout. Its preferable (and faster, but not by much in this case) to access the properties of an object than to use a Dictionary. Since you can't edit the name property of your MovieClips, maybe the best way would be to wrap your timeline objects in another object with a property that you can edit.
    – user1080806
    Dec 17, 2011 at 4:42
  • @atonparker Would there be any performance advantage to wrapping it in another object as opposed to using a Dictionary? Or would the advantage just be organization / clarity?
    – zechdc
    Dec 17, 2011 at 4:49
  • Any performance advantage would be so small its not worth considering. It should always be faster to access a property of a class reference than query a collection, but the Dictionary is probably a hash map so the the access is probably in constant time. The biggest difference is definitely brevity/clarity.
    – user1080806
    Dec 17, 2011 at 7:08
2
public function eventListener(e:Event)
{
    var target:MovieClip = (e.target as MovieClip);
    // Target is the object that dispatched the event.
}
2
  • That returns a movieclip. I need the name in string format. If this.animations['awesomeMovie'] then I need to get 'awesomeMovie' in the enter_frame_handler. Is that possible? I tried e.target.name` and get instance5 or something similar.
    – zechdc
    Dec 17, 2011 at 4:03
  • Could you post the code where you add it to the this.animations collection? The e.target.name property is separate from the name you use in the code. Also, what are you going to do with the string? Remember that this.animations["awesomeMovie"] refers to the same object as e.target in this case.
    – user1080806
    Dec 17, 2011 at 4:25
2

You first need to name the movie clips, then you can retrieve their names using e.currentTarget.name:

// --------------------------------------------------------------------
public function add(mc_name)
{
    //this.animations is a object that holds a bunch of dynamically loaded SWF files.
    addChild(this.animations[mc_name]);
    this.animations[mc_name].name = mc_name; // IMPORTANT - name the MovieClip
    this.animations[mc_name].addEventListener(Event.ENTER_FRAME, enter_frame_handler);
}

// --------------------------------------------------------------------
public function enter_frame_handler(e:Event)
{
    trace(e.currentTarget.name);
}
3
  • I tried this and I get Error #2078: The name property of a Timeline-placed object cannot be modified.
    – zechdc
    Dec 17, 2011 at 4:20
  • 2
    If they are created directly on the time line, then you need to name them there. Just set the "Instance name" property in the Properties panel. If they already have a name, you should be able to get it just with trace(e.currentTarget.name);
    – laurent
    Dec 17, 2011 at 4:24
  • I would like to do it all in code. Plus, I'm loading external SWF files without any actionscript in them. I figured out a work around that I will post shortly. Thanks for your help.
    – zechdc
    Dec 17, 2011 at 4:25
2
public function eventListener(e:Event)
{
    var target:MovieClip = (e.target as MovieClip);
    trace(target.name); // should return the name
}
1
  • I tried using .name and it returned some system generated value like instance5. I think this is due to how I am loading these external SWF's. I added some of that code above. Also, @kapep has provided an answer that works for my situation. Thanks for your help.
    – zechdc
    Dec 17, 2011 at 4:38
0

Or you could use this line, if your looking for its index

trace(stage.getChildIndex(MovieClip(event.target)));

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.