Skip to main content
June 1, 2011
Answered

Simple function for tooltip creation in Actionscript 3

  • June 1, 2011
  • 1 reply
  • 5142 views

Hi,

I'm new to Actionscript 3 and I am currently creating an application in which I need to use tooltips.  I thought it would be straightforward because in VBA and other object-oriented languages, you  have a tooltip property of a button and all you need is enter the text there (or else program it, which is very straightforward).  However, I cannot manage to create tooltips in Flash using AS3, I have tried 6 different (quite complex) methods from code which I found online but could get none of them to work.  Can someone please give me a simple function which takes as parameters the tooltip text and the original button and then the function just creates the tooltip automatically once it's called? Any help would be greatly appreciated. Thanks.

This topic has been closed for replies.
Correct answer kglad

it can but using simple buttons adds complexity to the coding:

// create one buttonObj for all your buttons

var buttonObj:Object={};

//////////////////////////////////////change nothing above ////////////////////////////////////

//the next 3 lines need to be done for all your buttons

buttonObj[yourbutton] = ["hello",2,2];

yourbutton.addEventListener(MouseEvent.MOUSE_OVER,addToolTipF) ;

yourbutton.addEventListener(MouseEvent.MOUSE_OUT, removeToolTipF);

////////////////////////////////////change nothing below/////////////////////////

function addToolTipF(e:MouseEvent):void{

var a:Array = buttonObj[e.currentTarget];

var tf:TextField=new TextField();

buttonObj[e.currentTarget].push(tf);

tf.text=a[0];

tf.multiline=false;

tf.autoSize="left";

tf.border=true;

addChild(tf);

tf.x=a[1]+e.currentTarget.x;

tf.y=a[2]+e.currentTarget.y;

}

function removeToolTipF(e:MouseEvent):void{

removeChild(buttonObj[e.currentTarget][3]);

buttonObj[e.currentTarget].split(3,1);

}


if both the add and remove functions are on the same timeline you won't see that error message.  but i see a typo:


// create one buttonObj for all your buttons

var buttonObj:Object={};

//////////////////////////////////////change nothing above ////////////////////////////////////

//the next 3 lines need to be done for all your buttons

buttonObj[yourbutton] = ["hello",2,2];

yourbutton.addEventListener(MouseEvent.MOUSE_OVER,addToolTipF) ;

yourbutton.addEventListener(MouseEvent.MOUSE_OUT, removeToolTipF);

////////////////////////////////////change nothing below/////////////////////////

function addToolTipF(e:MouseEvent):void{

var a:Array = buttonObj[e.currentTarget];

var tf:TextField=new TextField();

buttonObj[e.currentTarget].push(tf);

tf.text=a[0];

tf.multiline=false;

tf.autoSize="left";

tf.border=true;

addChild(tf);

tf.x=a[1]+e.currentTarget.x;

tf.y=a[2]+e.currentTarget.y;

}

function removeToolTipF(e:MouseEvent):void{

removeChild(buttonObj[e.currentTarget][3]);

buttonObj[e.currentTarget].splice(3,1);

}

1 reply

kglad
Community Expert
Community Expert
June 1, 2011

:

function addToolTipF(dobj:DisplayObjectContainer,x:int,y:int,s:String):TextField{

var tf:TextField=new TextField();

tf.text=s;

tf.multiline=false;

tf.autoSize="left";

tf.border=true;

dobj.addChild(tf);

tf.x=x;

tf.y=y;

return tf;

}

function removeToolTipF(tf:TextField):void{

tf.parent.removeChild(tf);

}

June 1, 2011

That's very helpful kglad, at least it didn't give me any errors.  However it did give me errors when I tried to call the functions, like this:

mc.addEventListener(MouseEvent.MOUSE_OVER,addToolTipF(mc,2,2,"hello"));
mc.addEventListener(MouseEvent.MOUSE_OUT, removeToolTipF(mc,2,2,"hello"));

Note: mc is the instance name of a particular button.  Any suggestions? Thanks a lot for your help.

kglad
Community Expert
Community Expert
June 1, 2011

you can't add parameters using addEventListener().  at least, you can't do it like that.

assuming you're using movieclip buttons, use:

mc.ttText="hello";

mc.ttX=2;

mc.ttY=2;

mc.addEventListener(MouseEvent.MOUSE_OVER,addToolTipF) ;

mc.addEventListener(MouseEvent.MOUSE_OUT, removeToolTipF);

////////////////////////////////////change nothing below/////////////////////////

function addToolTipF(e:MouseEvent):void{

var mc:MovieClip=MovieClip(e.currentTarget);

var tf:TextField=new TextField();

mc.tf=tf;

tf.text=mc.ttText;

tf.multiline=false;

tf.autoSize="left";

tf.border=true;

mc.addChild(tf);

tf.x=mc.ttX;

tf.y=mc.ttY;

}

function removeToolTipF(e:MouseEvent):void{

var mc:MovieClip=MovieClip(e.currentTarget);

mc.removeChild(mc.tf);

mc.tf=null;

}