Simple AS3 ToolTip

Tagged as: ActionScript, Flash

I recently had the need to create a fairly stylish, somewhat configurable tooltip for an AS3 project, so I figured I'd share as I couldn't find many freebees out there.  I also needed the ability to provide both a title and a short description with different formats.  This one is pretty straight forward and has a few settable properties.  Here's a rundown of what's available:

  • tipWidth : Number : Set the width of the tooltip
  • tipHeight : Number : Set the height of the tooltip
  • titleFormat : TextFormat : Format for the title of the tooltip
  • contentFormat : TextFormat : Format for the bodycopy of the tooltip
  • align : String : left, right, center
  • delay : Number : Time in milliseconds to delay the display of the tooltip
  • hook : Boolean : Displays a hook on the bottom of the tooltip
  • hookSize : Number : Size of the hook
  • cornerRadius : Number : Corner radius of the tooltip, same for all 4 sides
  • colors : Array : Array of 2 color values ( [0xXXXXXX, 0xXXXXXX] );
  • autoSize : Boolean : Will autosize the fields and size of the tip with no wrapping or multi-line capabilities, helpful with 1 word items like "Play" or "Pause"
  • border  : Number : Color Value: 0xFFFFFF
  • borderSize : Number : Size Of Border

A simple example would look something like:

var tf:TextFormat = new TextFormat();
tf.bold = true;
tf.size = 12;
tf.color = 0xff0000;
 
var tt:ToolTip = new ToolTip();
tt.hook = true;
tt.hookSize = 20;
tt.cornerRadius = 20;
tt.align = "center";
tt.titleFormat = tf;
tt.show( DisplayObject, "Title Of This ToolTip", "Some Copy below the Title" );

A working example:

Things of note, I am using an EnterFrame for following the mouse, maybe I should switch to a Timer?  I really never use frame rates below 30, so for me, this isn't much of a factor. I'm handling the MouseOut within the ToolTip, seemed easier that way.

Anyway, without further ado, here are the ToolTip source files used above.

Posted by CristianDuro on November 20th, 2008 at 8:24 am

Wow! Terryfic class!! xD

I was looking for a decent as3 tooltip class and is true that there’s no many freebees out there…

I only have 1 problem…When I try to change the ‘Georgia’ font with other one…THE TOOLTIP SHOWS NOTHING…I mean, I think it’s only possible to embed system fonts…agree?

Well, thank u so much 4 the class, good job, man! LOL

Posted by Duncan on November 20th, 2008 at 11:46 am

Good catch Cristian. I always forget the new way on adding a font to a TextFormat. Modified the lower middle ToolTip to use the Impact font, added the Impact font to the library with ‘impact’ and adjusted the code around line 55:

var font:impact = new impact(); //font in library
tf.font = font.fontName;

Updated the files, can you give it another try when you get a chance and let me know if that works for you?

Posted by JG on February 5th, 2009 at 3:09 pm

I am trying out the attached code and I keep getting an error on the TweenEvent - Type not found or was not a compile time constant on line 278. I am new to Flash/Flex/AS3 so I am kind of stuck. I feel like I am missing the TweenEvent lib or something. Any thoughts?

Posted by JG on February 5th, 2009 at 3:24 pm

Ok, I figured out that I need to have Flash CS3 to use the Tween classes. I am using Flex Builder 3. I figured someone else might make the same mistake and this will save some them some time.

Posted by Jared on February 8th, 2009 at 12:47 am

This is awesome! Helps me out a lot. Thanks!

Posted by Garth on March 5th, 2009 at 1:19 pm

Nice job! Easy integration and customization

Posted by gzzle on March 26th, 2009 at 9:45 am

Hi! Nice job!!!!!! Looks great, but I’ve got a question….

I took your code just to try it out and it works perfect..
But I’ve tried to put a movieclip into the tooltip.. It doesn’t work.. at least not the way I tried… How whould you do it??

Posted by rika on May 15th, 2009 at 4:10 am

Hi Duncan, thanks so much for posting this, it’s great! I’d very much like to have an outline on the tooltip. How would I go around adding that to the script?
Thanks in advance

Posted by Duncan on May 15th, 2009 at 3:18 pm

Hi Rika,

I’ve added “border” and “borderSize” to the class for you. border is a number color value ( 0xFF0000 ) and borderSize is also just a number to set the line thickness.

Hope this helps some.

Posted by rika on May 16th, 2009 at 9:18 am

That is so cool! Thanks :) :)

Posted by Hernan on June 9th, 2009 at 3:04 pm

Hey Duncan very neat Class love it …
I got a question …. there is anyway that you could set up the height of the box ?
the same way that you can right now set up the width ?

Thanks

Posted by Duncan on June 9th, 2009 at 3:50 pm

Hi Hernan,

I added “tipHeight” to the class for you, typed as Number. Hope this helps some.

Posted by Hwang on June 9th, 2009 at 5:35 pm

Nice stuff, but any way to disable the title or content?
And be able to adjust the position of the hook rather than always at the bottom?

Posted by Hwang on June 9th, 2009 at 5:44 pm

and I’m not sure whether is it just me.
sometimes when i keep rolling over to show the tool tips, it keeps adding my content?

Posted by Hernan on June 9th, 2009 at 7:26 pm

Hey Duncan Thanks a lot, but I tried to put that property and didnt work out … I checked the class it self and I didnt see “tipHeight” defined anywhere … so when I tried to compile it gives me an error. typical one when you dont have something define …. btw I did download the latest version which say “tt-06-2009.zip” Thanks

Posted by Duncan on June 9th, 2009 at 11:13 pm

@Hwang: If you leave out the ‘content’ it will only use the title, there is an example in the .zip and in the swf above. Right now the hook is stationed at the bottom, if I find some time I will look into moving it around along with the Tooltip depending on where the mouse is in comparison to the object you are rolling over. I haven’t experienced the adding content bug your mentioning but if you can send me a sample i’d gladly take a look for you.

@Hernan: I think WordPress is caching files or something, please try to download it again, I just did and everything worked. If your still having difficulty please send me an email and i’ll send the files to you directly.

Posted by Hernan on June 10th, 2009 at 6:03 pm

Hey Duncan Thanks that worked …… I got question … check this out this is what Im working on …. http://www.elasticcreative.com/beta/#/work/

if you click one of the thumbs then if you go to the right of the video or left you will see a left and right arrow … as you can see your tooltip class works perfect …. what is happening is when you rollover you see what is coming next (next video)… if you click it takes you to the right video …. but the thumbs doesnt change (or update) I need to rollout and rollover again for the text to update …. there is anyway that the text could change on the fly? Im loading the text through XML btw …. let me know what you think …. thank you .

Posted by Duncan on June 11th, 2009 at 10:42 am

Hi Hernan,

Update: added “setContent” method, parameters: title, content… content is optional. Example in the source and in the swf above ( click large circle on the left ). Hopefully this will help some.

Posted by Jeff on June 13th, 2009 at 8:11 pm

Duncan, thank you! This really helped me out alot. Great job [high five].

Leave a comment