A Flash Developer Resource Site

Page 1 of 2 12 LastLast
Results 1 to 20 of 25

Thread: evt.target.name only works once

  1. #1
    Senior Member
    Join Date
    Oct 2005
    Posts
    198

    evt.target.name only works once

    Hi,

    I have a simple event listener which calls a functions which first uses a switch case statement to determine what evt.target was pressed by name so I can assign info there--presently I just have trace statements--the problem is that it only works once and then I just get instance29 and the switch case evaluates to the default.

    Code:
    lifering1.addEventListener(MouseEvent.MOUSE_OVER, playSplash,false,0,true);
    		function playSplash(evt:MouseEvent):void
    		{
    			trace(evt.target.name);
    			switch (evt.target.name)
    			{
    
    				case "lifering1" :
    					trace("IBM");
    					break;
    				case "lifering2" :
    					trace("2");
    					break;
    				case "lifering3" :
    					trace("3");
    					break;
    				case "lifering4" :
    					trace("4");
    					break;
    
    				default :
    					trace("default");
    
    
    			}
    }
    Help??

    Thanks,
    ---Yvette

  2. #2
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    You are actually mousing over some other instance. Probably something contained within one of the things you're trying to detect.
    Use event.currentTarget rather than target.

    Why are you using names anyway? Why not just compare directly? For that matter, why have a single function with a switch statement when you want to handle each differently?

  3. #3
    Senior Member
    Join Date
    Oct 2005
    Posts
    198
    Thanks 5TonsOfFlax--
    dont understand this:
    Why not just compare directly? what do you mean?

    Im using names because there are 4 MCs which are all instances of the same MCs --I have given them instance names of lifering1,lifering2,lifering3,lifering4--
    They all will produce the same animation on MOUSE_OVER, MOUSE_OUT--the only thing that needs to change is the text that pops up--

    Thanks again and again,
    ---Yvette

  4. #4
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    By comparing directly, I mean:
    Code:
    function playSplash(evt:MouseEvent):void{
      trace(evt.target.name);
      switch (evt.currentTarget){
    	case lifering1 :
    		trace("IBM");
    		break;
    	case lifering2 :
    		trace("2");
    		break;
    	case lifering3 :
    		trace("3");
    		break;
    	case lifering4 :
    		trace("4");
    		break;
    	default :
    		trace("default");
      }
    }
    Though from your description it sounds like you'd be better off assigning the text to the instance and using that rather than switching.
    Code:
    lifering1.textToUse = "howdy";
    lifering2.textToUse = "hello";
    lifering3.textToUse = "hi";
    lifering4.textToUse = "'sup?";
    
    lifering1.addEventListener(MouseEvent.MOUSE_OVER, playSplash,false,0,true);
    lifering2.addEventListener(MouseEvent.MOUSE_OVER, playSplash,false,0,true);
    lifering3.addEventListener(MouseEvent.MOUSE_OVER, playSplash,false,0,true);
    lifering4.addEventListener(MouseEvent.MOUSE_OVER, playSplash,false,0,true);
    
    function playSplash(evt:MouseEvent):void{
      trace(MovieClip(evt.currentTarget).textToUse);
    }

  5. #5
    Senior Member
    Join Date
    Oct 2005
    Posts
    198
    Thanks for the clarification--Unfortunately, its not working--because either way the instance name is being interpreted as a string--how can I take that string and turn it back into a mc instance name?
    Here's the complete code minus the event listeners and the text Im trying to change:
    Code:
    		function playSplash(evt:MouseEvent):void
    		{
    			trace(evt.currentTarget.name);
    			switch (evt.currentTarget.name)
    			{
    
    				case lifering1 :
    					trace("IBM");
    					break;
    				case lifering2 :
    					trace("2");
    					break;
    				case lifering3 :
    					trace("3");
    					break;
    				case lifering4 :
    					trace("4");
    					break;
    
    				default :
    					trace("default");
    
    
    			}
    
    
    			evt.currentTarget.name.gotoAndStop(2);
    			/*trace(lifering1.InstanceName_0.y);*/
    			TweenMax.to(evt.currentTarget.name.stick, 1 ,{x:-121.9,y:-322,ease:Back.easeOut});
    			TweenMax.to(evt.currentTarget.name.cont, 1 ,{x:-227.7,y:-749.7,ease:Back.easeOut});
    			/*lifering1.InstanceName_0.menutitle.text="CONTACT";*/
    			evt.currentTarget.name.splash.gotoAndPlay(2);
    		}
    
    
    		function retractStick(evt:MouseEvent):void
    		{
    			evt.currentTarget.name.splash.gotoAndPlay(2);
    			TweenMax.to(evt.currentTarget.namecont, 1 ,{x:8.1,y:36.3,ease:Back.easeOut});
    			TweenMax.to(evt.currentTarget.namestick, 1 ,{x:120.45,y:464,ease:Back.easeOut,onComplete:endSplash});
    
    
    		}
    		function endSplash():void
    		{
    			evt.currentTarget.name.splash.gotoAndStop(1);
    			evt.currentTarget.name.gotoAndStop(1);
    
    
    		}
    ----Yvette

  6. #6
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    Your code is still switching on the name. Of course the name is a string and will never be equal to the instance. The code I posted switches on the instance.

    Remove ".name" everywhere in your code except for the trace which is the first line in playSplash.

  7. #7
    Senior Member
    Join Date
    Oct 2005
    Posts
    198
    God youre good--one more problem--the last function endSplash is being called by TweenMax and thus has no evt or rather its undefined---how can I fix the last function:
    Code:
    function endSplash():void
    		{
    			evt.currentTarget.splash.gotoAndStop(1);
    			evt.currentTarget.gotoAndStop(1);
    
    
    		}
    Thank you so much,
    ---Yvette
    Last edited by yvillicana; 07-08-2010 at 05:02 PM.

  8. #8
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    Have endSplash take an argument for the instance, and use onCompleteParams in the TweenMax line with onComplete.

    Code:
    //...
    TweenMax.to(evt.currentTarget.stick, 1 ,{x:120.45,y:464,ease:Back.easeOut,onComplete:endSplash, onCompleteParams:[evt.currentTarget]});
    //...
    Code:
    function endSplash(clip:MovieClip):void{
      clip.splash.gotoAndStop(1);
      clip.gotoAndStop(1);
    }

  9. #9
    Senior Member
    Join Date
    Oct 2005
    Posts
    198
    THANK YOU!!!

    I promise no more ?s today...

    ---Yvette

  10. #10
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    It's what we're here for. If you need any further clarification of the difference between a name and the thing being named, write "5TonsOfFlax" on a sheet of paper and ask it to explain.

  11. #11
    Senior Member
    Join Date
    Oct 2005
    Posts
    198
    I wrote your name on a piece of paper with my new ? and never got a reply??

    Just a general ?:
    Would going to frame 1 like I do in my endSplash function end the nested clip from playing if its only on frame 2 within the clip?--What I mean is its rather processor intensive--an animated flag for each menu item and it seems that things have slowed down--Would going back to frame 1 be equivalent to removeChild in this case?
    If not, I may have some ?s

    Thanks,
    ---Yvette

  12. #12
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    I really don't know. I don't do animations or deal with frames much, except to see the problems they cause in other people's code.

    If the player is optimized intelligently, it should at least skip the rendering of that clip, which should save some CPU. You could test by putting trace statements in the frames for the nested clip. If they still occur, then clearly it is still there and processing to some degree.

  13. #13
    Senior Member
    Join Date
    Oct 2005
    Posts
    198
    I added an enter frame listener and it keeps on tracing--
    I narrowed down the problem to the retractStick & endSplash function:
    Sometimes I get this error if I move back and forth over the menu items
    TypeError: Error #1009: Cannot access a property or method of a null object reference.
    at gs::TweenFilterLite()
    at gs::TweenMax()
    at gs::TweenMax$/to()
    at Main/retractStick()

    --Im thinking it could have to do with this onCompleteParams:[evt.currentTarget] as the target is changing--Can you suggest a fix? Also, I need to remove the clip with an instance name of cont but cant seem to do so in that endSplash function--Here's the code I tried and the error received:
    Code:
    		function retractStick(evt:MouseEvent):void
    		{
    			evt.currentTarget.splash.gotoAndPlay(2);
    			TweenMax.to(evt.currentTarget.cont, 1 ,{x:8.1,y:36.3,ease:Back.easeOut});
    			TweenMax.to(evt.currentTarget.stick, 1 ,{x:120.45,y:464,ease:Back.easeOut,onComplete:endSplash,onCompleteParams:[evt.currentTarget]});
    
    
    		}
    		function endSplash(clip:MovieClip):void
    		{
    			clip.splash.gotoAndStop(1);
    			clip.gotoAndStop(1);
    			clip.removeChild(cont);
    		}
    ReferenceError: Error #1065: Variable cont is not defined.
    at Main/endSplash()
    at Function/http://adobe.com/AS3/2006/builtin::apply()
    at gs::TweenLite/complete()
    at gs::TweenMax/complete()
    at gs::TweenFilterLite/render()
    at gs::TweenLite$/executeAll()
    what it should be removing is lifering1.cont, lifering2.cont,lifering3.cont,lifering4.cont--but I guess clip isnt referring to the currentTarget.....

    Help!

    Thanks,
    ---Yvette

  14. #14
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    Code:
    clip.removeChild(clip.cont);
    I'm not sure about the first one. Do they all have both cont and stick? the cont property may be set to null after it's removed through endSplash. That would cause the line where you try to tween it to throw the nullpointer error.

  15. #15
    Senior Member
    Join Date
    Oct 2005
    Posts
    198
    Hi and thanks again,

    Yes, they are all exactly the same--all have stick and cont--just different instances of the same clips...
    I put in a trace for that clip in the endSplash function and it traces out the correct names--trace(clip.name)--

    The new code you kindly provided produces this error:
    TypeError: Error #2007: Parameter child must be non-null.
    at flash.display:isplayObjectContainer/removeChild()
    at Main/endSplash()
    at Function/http://adobe.com/AS3/2006/builtin::apply()
    at gs::TweenLite/complete()
    at gs::TweenMax/complete()
    at gs::TweenFilterLite/render()
    at gs::TweenLite$/executeAll()
    It seems that everything is getting referred to the TweenMax.as files

    ---Yvette

  16. #16
    Senior Member
    Join Date
    Oct 2005
    Posts
    198
    actually this error too:
    ArgumentError: Error #2025: The supplied DisplayObject must be a child of the caller.
    at flash.display:isplayObjectContainer/removeChild()
    at Main/endSplash()
    at Function/http://adobe.com/AS3/2006/builtin::apply()
    at gs::TweenLite/complete()
    at gs::TweenMax/complete()
    at gs::TweenFilterLite/render()
    at gs::TweenLite$/executeAll()
    Code:
    		function retractStick(evt:MouseEvent):void
    		{
    			evt.currentTarget.splash.gotoAndPlay(2);
    			TweenMax.to(evt.currentTarget.cont, 1 ,{x:8.1,y:36.3,ease:Back.easeOut});
    			TweenMax.to(evt.currentTarget.stick, 1 ,{x:120.45,y:464,ease:Back.easeOut,onComplete:endSplash,onCompleteParams:[evt.currentTarget]});
    
    
    		}
    		function endSplash(clip:MovieClip):void
    		{
    			clip.removeChild(clip.cont);
    			clip.splash.gotoAndStop(1);
    			clip.gotoAndStop(1);
    			
    		}

  17. #17
    Senior Member
    Join Date
    Oct 2005
    Posts
    198
    changing clip.removeChild(clip.cont) to clip.cont=null; only produces this error once and only on the first MOUSE_OUT:
    TypeError: Error #1009: Cannot access a property or method of a null object reference.
    at gs::TweenFilterLite()[K:\americaCupSmitty\gs\TweenFilterLite.as:202]
    at gs::TweenMax()[K:\americaCupSmitty\gs\TweenMax.as:317]
    at gs::TweenMax$/to()[K:\americaCupSmitty\gs\TweenMax.as:707]
    at Main/retractStick()[K:\americaCupSmitty\Main.as:171]
    while the remove child method produces multiple error outputs--
    However, it still seems to be in memory--that is, processing seems to lag

    thanks,
    ---Yvette

  18. #18
    Senior Member
    Join Date
    Oct 2005
    Posts
    198
    fixed that with an if statement but still cant seem to actually remove the clips(ie, noticeable processor load):
    Code:
    		function endSplash(clip:MovieClip):void
    		{
    			if(clip.cont!=null){
    				clip.cont=null;
    				
    			};
    			clip.splash.gotoAndStop(1);
    			clip.gotoAndStop(1);
    			
    		}

  19. #19
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    Those errors are occurring because the cont clips are removed and the cont property is set to null, probably on different frames, resulting in the two different messages. TweenMax is in the stack trace just because it is what ends up calling endSplash. TweenMax is not the problem.

    Try this for endSplash
    Code:
    function endSplash(clip:MovieClip):void{
      clip.splash.gotoAndStop(1);
      clip.gotoAndStop(1);
      if (clip.cont && clip.contains(clip.cont)){
        clip.cont.stop();
        clip.removeChild(clip.cont);
      }
    }
    The if should safeguard against trying to access it when it's null or not a child, which should prevent those errors. By stopping the clip, it should cease taking so much processor. You could put a function in the conts that cleans up all their ongoing processes and call that just before removeChild.

  20. #20
    Senior Member
    Join Date
    Oct 2005
    Posts
    198
    Thanks for everything and for explaining about the error---However, either way, the way I had it or your way I still get 1009 errors(now)--
    An odd thing, which could be very helpful in finding out what is going on, is that once a few errors start being thrown--the restractStick animations no longer occur--the flags and sticks just disappear---mousing over another menu item will produce the emerging flag and stick animations in playSplash, but on mousing out the entire thing, sticks and flags disappear--Im lost....

    Thanks,
    ---Yvette

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