-
Flash Incompetent
[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
-
-
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.
-
Flash Incompetent
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...
-
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.
-
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.
-
Flash Incompetent
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
-
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;
}
-
hi
your help is wonderfull!!
thanks a lot
-
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|