Adding a Tooltip to the StackFlow Component
Actionscript 3, Flash Components, Tutorials December 23rd, 2008As promised in the previous tutorial, in this tutorial I will show you how to add a tooltip to one of the FlashOtaku components. I will use the StackFlow component as an example but it is applicable to the other flash components as well.
Here’s how the final result will look like:
First we need an XML file that will contain the source and the title of each item. To specify the title, I will use the ‘title’ attribute but this is not a keyword. You can use any name for the extra attributes you add. The flash component will store all the attributes and then, the API will give you access to the value of those attributes.
-
<?xml version="1.0" encoding="utf-8"?>
-
<images>
-
<image source="images/image1.jpg" title="Image 1 title"/>
-
<image source="images/image2.jpg" title="Image 2 title"/>
-
<image source="images/image3.jpg" title="Image 3 title"/>
-
<image source="images/image4.jpg" title="Image 4 title"/>
-
<image source="images/image5.jpg" title="Image 5 title"/>
-
<image source="images/image6.jpg" title="Image 6 title"/>
-
<image source="images/image7.jpg" title="Image 7 title"/>
-
<image source="images/image8.jpg" title="Image 8 title"/>
-
<image source="images/image9.jpg" title="Image 9 title"/>
-
<image source="images/image10.jpg" title="Image 10 title"/>
-
</images>
As I mentioned in the previous tutorial, I can also use Actionscript to specify the data for each item. The Actionscript code should look like this:
-
var myItems:Array = [{source:"images/image1.jpg", title:"Image 1 title"},
-
{source:"images/image2.jpg", title:"Image 2 title"},
-
{source:"images/image3.jpg", title:"Image 3 title"}];
-
-
myStackFlow.items = myItems;
or
-
myStackFlow.addItem({source:"images/image1.jpg", title:"Image 1 title"});
-
myStackFlow.addItem({source:"images/image2.jpg", title:"Image 2 title"});
-
myStackFlow.addItem({source:"images/image3.jpg", title:"Image 3 title"});
Now, create a new symbol and inside the symbol draw a simple tooltip and add a dynamic text field that will display the title. Give the text field an instance name of tooltipText and move the tooltip so that bottom edge of the tooltip’s arrow is positioned where the registration point of the symbol is.

When you’re done creating the tooltip, right click on it in the Library, chose Linkage, select Export for Actionscript and in the Class field write Tooltip. This step is necessary in order to be able to use the tooltip in the actionscript code.
The actionscript part is pretty easy, so I’m simply going to attach the code.
-
import com.flashotaku.events.StackFlowEvent;
-
import flash.events.MouseEvent;
-
-
// create an instance of the Tooltip
-
var tooltip:Tooltip = new Tooltip();
-
-
myStackFlow.addEventListener(StackFlowEvent.ITEM_MOUSE_OVER, tooltipHandler);
-
myStackFlow.addEventListener(StackFlowEvent.ITEM_MOUSE_OUT, tooltipHandler);
-
myStackFlow.addEventListener(StackFlowEvent.ITEM_CLICK, itemClickHandler);
-
-
function itemClickHandler(event:StackFlowEvent):void
-
{
-
// event.index is the index of the clicked item
-
// when an item is clicked, scroll to that item
-
myStackFlow.select(event.index);
-
}
-
-
function tooltipHandler(event:StackFlowEvent):void
-
{
-
if (event.type == StackFlowEvent.ITEM_MOUSE_OVER)
-
{
-
// event.data.title is the value of the title attribute added in the XML file
-
addTooltip(event.data.title);
-
}
-
else if (event.type == StackFlowEvent.ITEM_MOUSE_OUT)
-
{
-
removeTooltip();
-
}
-
}
-
-
function addTooltip(myText:String):void
-
{
-
// add the tooltip to the stage
-
addChild(tooltip);
-
// set the text property of the tooltipText TextField to the value of the myText parameter
-
tooltip.tooltipText.text = myText;
-
// when the mouse moves, move the tooltip
-
stage.addEventListener(MouseEvent.MOUSE_MOVE, mouseMoveHandler);
-
}
-
-
function removeTooltip():void
-
{
-
// remove the listener
-
stage.removeEventListener(MouseEvent.MOUSE_MOVE, mouseMoveHandler);
-
// remove the tooltip instance from stage
-
removeChild(tooltip);
-
}
-
-
function mouseMoveHandler(event:MouseEvent):void
-
{
-
tooltip.x = mouseX;
-
tooltip.y = mouseY;
-
}
As you can see in this tutorial and also in the previous one, it’s very easy to add extra features using the new flash components. If you have any questions feel free to ask.
Recent Comments