A Flash Developer Resource Site

Results 1 to 10 of 10

Thread: [AS3] addEventListener for Mouse Over

  1. #1
    Flash Incompetent ChaseNYC's Avatar
    Join Date
    Jun 2002
    Location
    new york city
    Posts
    693

    [AS3] addEventListener for Mouse Over

    I have a text box that I want to display "tips" in, for example I have a text box with text in it and I added an event listener for mouse over but I realized i have no way to tell the function what text to display. When I try to do something like the following, I just get error messages:

    Code:
    txtpName.addEventListener(MouseEvent.MOUSE_OVER, tipText("This is your name");
    
    public function tipText (tip:String, e:Event):void {
    			txtTip.text = tip;
    		}
    Can anyone help me figure out how to make this work?

    Thanks in advance,
    ChaseNYC
    mmm signature

  2. #2
    Junior Member
    Join Date
    Dec 2006
    Posts
    8
    I have reconstructed your code and fixed the problems you had, and attached the FLA file for you to download.

    you had several problems:
    - you didn't define the variable that would hold your text string
    - the function event type was wrong

    here's the result of the fixed file:
    Code:
    var txtTip:String = "This is your name";
    txtpName.addEventListener(MouseEvent.MOUSE_OVER, tipText);
    
    function tipText (e:MouseEvent):void {
    	txtpName.text = txtTip;
    }
    I hope this helps
    Attached Files Attached Files

  3. #3
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    You can use a dictionary to map event.target to the text to use, or you can use my FunctionUtils class to create a closurized event handler which appears to take extra arguments. You can find it and example code on my site http://cosmodro.me and it's also linked in the resources thread at the top of the forum.

  4. #4
    Flash Incompetent ChaseNYC's Avatar
    Join Date
    Jun 2002
    Location
    new york city
    Posts
    693
    liquidquest, that would almost work but the problem with it is I want to attach listeners for many different things and have them all display different text to the txtTip.text...

    5TonsOfFlax, could you maybe explain a little bit more about the first option you listed of a dictionary to map event.target? I tried looking at your FunctionUtils but that was a little too complex for me to comprehend...
    mmm signature

  5. #5
    Junior Member
    Join Date
    Dec 2006
    Posts
    8
    thats easy... for any new object that you add to the stage, give it an instance name, then copy and paste it again like so:
    Code:
    // first object
    var txtTip01:String = "This is your name";
    ObjectName01.addEventListener(MouseEvent.MOUSE_OVER, tipText01);
    function tipText02 (e:MouseEvent):void {
    	txtpName.text = txtTip01;
    }
    
    // second object
    var txtTip02:String = "Yup, it is your name";
    ObjectName02.addEventListener(MouseEvent.MOUSE_OVER, tipText02);
    function tipText02 (e:MouseEvent):void {
    	txtpName.text = txtTip02;
    }
    
    // third object
    var txtTip03:String = "You iz ma son! hehe";
    ObjectName03.addEventListener(MouseEvent.MOUSE_OVER, tipText03);
    function tipText03 (e:MouseEvent):void {
    	txtpName.text = txtTip03;
    }
    ObjectNameXX is the instance name of the object on stage.
    Last edited by liquidquest; 10-04-2008 at 12:07 PM.

  6. #6
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    I'm posting from my phone, so I can't go into as much detail as I'd like.

    Code:
    var tipByTxt:Dictionary = new Dictionary();
    tipByTxt[txtpName] = "some text";
    
    txtpName.addEventListener(MouseEvent.MouseOver, showTip);
    
    function showTip(evt:MouseEvent):void{
      var tiptxt:String = tipByTxt[event.currentTarget];
    // do stuff
    }
    For each new thing with a tip, you define an entry in the dictionary and add showTip as a listener.

  7. #7
    Flash Incompetent ChaseNYC's Avatar
    Join Date
    Jun 2002
    Location
    new york city
    Posts
    693
    Well I'm 99% there now, that Dictionary thing is awesome... my only problem is something I'm not sure what its called but its causing me problems...

    Code:
    		public function tipText (e:MouseEvent):void {
    			var tipByTxt:Dictionary = new Dictionary();
    			tipByTxt[Game.txtpName] = "some text";			
    			Game.txtTip.text = tipByTxt[Game[e.currentTarget.name]];
    		}
    the last line of code is causing the problem, I'm not sure how to recreate Game.txtpName... I've checked and e.currentTarget.name = txtpName but Game[e.currentTarget.name] is not working I think.... Any clue why?

    Anyways,
    Thanks a ton for the help 5TonsOfFlax, this is exactly what I was looking for!

    ChaseNYC
    mmm signature

  8. #8
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    Your dictionary should be declared outside the event handler function, or else there's no use for it.

    You've fallen into the trap of trying to access things by instancename. AS2 forced you to do that in many cases, but you almost never have to do that in AS3.

    Code:
    var tipByTxt:Dictionary = new Dictionary();
    tipByTxt[Game.txtpName] = "some text";
    tipByTxt[Game.otherThingWithTip] = "some other text";
    
    Game.txtpName.addEventListener(MouseEvent.MOUSE_OVER, showTip);
    Game.otherThingWithTip.addEventListener(MouseEvent.MOUSE_OVER, showTip);
    
    function showTip(evt:MouseEvent):void{
      var tip:String = tipByTxt[evt.currentTarget];
      txtTip.text = tip;  
    }
    This assumes you want all tips to show in txtTip, but should show the general idea.

    By the way, to accomplish the same thing with FunctionUtils, it would be something like this:

    Code:
    import me.cosmodro.utils.FunctionUtils;
    
    txtpName.addEventListener(MouseEvent.MOUSE_OVER, FunctionUtils.closurizeEventHandler(tipText, "This is your name"));
    
    someOtherThingWithTip.addEventListener(MouseEvent.MOUSE_OVER, FunctionUtils.closurizeEventHandler(tipText, "Something else"));
    
    public function tipText (e:Event, tip:String):void {
    	txtTip.text = tip;
    }

  9. #9
    Junior Member
    Join Date
    Aug 2009
    Location
    tehran
    Posts
    2

    Talking

    hi
    your help is wonderfull!!
    thanks a lot

  10. #10
    Junior Member
    Join Date
    Aug 2009
    Location
    tehran
    Posts
    2
    if i want show an image insted of text in text box, what should i do?
    in the other words, I want to display a symbol (air plan picture) when my mouse is over another symbol (air port). can you help me?

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