A Flash Developer Resource Site

Page 2 of 2 FirstFirst 12
Results 21 to 30 of 30

Thread: currentTarget.name doesn't give instance name...

  1. #21
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    Quote Originally Posted by Big 'Un
    The part that I don't understand is this:
    if you physically place a button or mc on the stage and type in an instance name, you can addEventListener to it by name.
    But if you create it dynamically and assign it a name, you can't.
    Why is that?
    Because you have "Automatically declare stage instances" checked. "Behind the scenes" Flash declares variables with the same names as your instancenames. It is these variables that you can actually use to addEventListener, or other direct code manipulation.

    When you dynamically add instances as you do above, you do not get variables named "but1", etc. No worries, though. A variable is just one way of referencing an instance. You can also get a reference to an instance by using the array method swak alluded to:
    Code:
    buttonArray[1].addEventListener(...)
    Or even by getChildByName on the containing parent (in this case, "this").
    Code:
    getChildByName("but1").addEventListener(...)

  2. #22
    trace("AKA: Biro Barna");
    Join Date
    Oct 2007
    Location
    RO.Timişoara
    Posts
    1,403
    Yes, you're right with the if, when you have multiple objects then it can get messy. To achieve the same thing in a more advanced way, you'll need to define a variable inside each movie clip, and ID. And you can trace those objects by their ID ( that variable ). Regarding the event listener. Well, you can assign an event listener to an object by name with the help of getChildByName("theName").addEventListener...



    | Windows MSN: birobarna [at] hotmail [dot] com | Skype: barna.biro |
    WebLog: http://blog.wisebisoft.com/ |
    | Software Developer / Flash & Flex Developer | Student ( Computer Science ) | Interested in: Sharing Knowledge |
    |
    Romanian Adobe Flash, Flex, AIR Forum: http://www.flashforum.ro/
    | By perseverance the snail reached the ark. |


  3. #23
    KoolMoves Moderator blanius's Avatar
    Join Date
    Jul 2001
    Location
    Atlanta GA
    Posts
    5,244
    I'm dealing with this same issue.

    I tried using e.currentTarget.name and it has the name but since this is NOT the object itself how do I call methods in the object without resorting to a if conditional?
    Last edited by blanius; 02-22-2008 at 07:53 PM.

  4. #24
    trace("AKA: Biro Barna");
    Join Date
    Oct 2007
    Location
    RO.Timişoara
    Posts
    1,403
    What do you mean by "call methods in the object"... You said that your object has a name, you can add a listener to the object ( using it's name or whatever you'd like ) and call functions once that certain event triggers.

    Could you explain a bit more what exactly is the problem ? Your problem is really not clear to me nor is what you are trying to achieve.

    EDIT: hah, really bad typo... I'm tired, sorry... If you want to add a listener then you could assign it to it's name or to the object:

    var whatever:YourMovieClip = new YourMovieClip();

    addChild(whatever);

    whatever.name = "yourMc";

    // now, you could use 2 methods
    whatever.addEventListener(....);

    // or by assigning it to it's name
    getChildByName("yourMc").addEventListener(...)
    Last edited by fx.barrett; 02-22-2008 at 08:51 PM.



    | Windows MSN: birobarna [at] hotmail [dot] com | Skype: barna.biro |
    WebLog: http://blog.wisebisoft.com/ |
    | Software Developer / Flash & Flex Developer | Student ( Computer Science ) | Interested in: Sharing Knowledge |
    |
    Romanian Adobe Flash, Flex, AIR Forum: http://www.flashforum.ro/
    | By perseverance the snail reached the ark. |


  5. #25
    KoolMoves Moderator blanius's Avatar
    Join Date
    Jul 2001
    Location
    Atlanta GA
    Posts
    5,244
    I'll try adding the listener with childByName and see how that goes.

  6. #26
    Senior Member Big 'Un's Avatar
    Join Date
    Aug 2004
    Location
    NYC
    Posts
    231
    Hi. Yes. Thanks so much for your replies. I tried getChildByName and that worked.
    I also did a similar experiment and registered events using getChildAt which worked well since their level numbers matched their index created by a loop.

    Thanks

  7. #27
    Junior Member
    Join Date
    Feb 2008
    Posts
    2
    Quote Originally Posted by PlenaryCreation
    instance19 and the rest are the instance names of your dynamically created buttons, you're not doing anything wrong. Since you are not naming your buttons, flash will name them by default "instance" + a random number ( not really sure if it's 100% random, but sorta ).

    In order to get a more "normal", custom instance name, use the .name property to set a custom identifier for each instance:

    PHP Code:
    for (1i<=5i++) 
    {
        
    this["but"+i] = new clearBut();
        
    addChild(this["but"+i]);
        
    this["but"+i].name "but" i// name each instance differently
        
    this["but"+i].this["txt"+i].x;

        
    this["but"+i].addEventListener(MouseEvent.CLICKonMouseDownhandler);
    // end of for

    function onMouseDownhandler (event:MouseEvent):void 
    {
        
    trace("instance name: " event.currentTarget.name);

    // end of onMouseDownHandler 
    After 5 iterations, that should give you something like: bu1, but2... but5

    Good luck.
    Im not creating instances dynamically, they are defined on the MovieClip instance. The instance itself is added to the stage at runtime and the MovieClip's base class is a custom AS3 class. I too am experiencing instance + num when I try event.target.name...

    can anyone offer suggestions?

  8. #28
    trace("AKA: Biro Barna");
    Join Date
    Oct 2007
    Location
    RO.Timişoara
    Posts
    1,403
    Could you maybe post your code ? It would help a lot.



    | Windows MSN: birobarna [at] hotmail [dot] com | Skype: barna.biro |
    WebLog: http://blog.wisebisoft.com/ |
    | Software Developer / Flash & Flex Developer | Student ( Computer Science ) | Interested in: Sharing Knowledge |
    |
    Romanian Adobe Flash, Flex, AIR Forum: http://www.flashforum.ro/
    | By perseverance the snail reached the ark. |


  9. #29
    Junior Member
    Join Date
    Feb 2008
    Posts
    2
    Heres the important part:

    Code:
    function clickDispatch(ev:Event):void{			
    			
    	trace(ev.target.name);
    	var white:ColorTransform = new ColorTransform();
    	white.color = 0xCC00FF;
    	playPause.transform.colorTransform = white;
    
    			
    	switch(ev.target.name){
    		case "playPause":
    			//dispatchEvent(new Event(ScrubberEvent.PLAY_PAUSE));
    					break;
    		case "playhead":
    			//dispatchEvent(new Event(ScrubberEvent.SEEK));
    			break
    		case "volumeIcon":
    			//dispatchEvent(new Event(ScrubberEvent.MUTE));
    			break;
    		case "volumeLevel":
    			//dispatchEvent(new Event(ScrubberEvent.NEWVOLUME));
    			break;
    		case "volume":
    			//dispatchEvent(new Event(ScrubberEvent.NEWVOLUME));
    			break;
    	}
    }
    That trace statement spits out instance+num but the code to change the coloue of the MovieClip works just fine so Flash is able to find it.

    playPause is defined in the Library inside a container MovieClip whose base class is the one that contains the above code.

    Does that help?

  10. #30
    trace("AKA: Biro Barna");
    Join Date
    Oct 2007
    Location
    RO.Timişoara
    Posts
    1,403
    If you are saying that the trace spits out "instance + num" correctly then you're not doing the switch right... in you switch you have nothing making "instance+num"... let's say the trace spits "button1" then once it looks at the switch, it will not find a matching case since no such case as "button1" exists...

    Example of a working switch:

    PHP Code:
    this.button1.addEventListener(MouseEvent.CLICKclickDispatch);
    this.button2.addEventListener(MouseEvent.CLICKclickDispatch);

    function 
    clickDispatch(ev:Event):void{            
                
        switch(
    ev.target.name){
            case 
    "button1":    
                
    trace(ev.target.name " clicked");
                break;
            case 
    "button2":    
                
    trace(ev.target.name " clicked");
                break;
        }

    Good luck.
    Last edited by fx.barrett; 03-01-2008 at 02:05 PM.



    | Windows MSN: birobarna [at] hotmail [dot] com | Skype: barna.biro |
    WebLog: http://blog.wisebisoft.com/ |
    | Software Developer / Flash & Flex Developer | Student ( Computer Science ) | Interested in: Sharing Knowledge |
    |
    Romanian Adobe Flash, Flex, AIR Forum: http://www.flashforum.ro/
    | By perseverance the snail reached the ark. |


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center