一旦你创建了所有的城市图标并将它们放在舞台上并给它们实例名称,然后将它们放在一个数组中。这只是为了让事情更容易管理。
cityIcons.push(state_tx), cityIcons.push(state_ca)
ETC
现在我们需要添加代码来显示气球。您也提到了对其进行动画处理。将在 cityTag_mc 中生长的气球动画放在文本字段下方。给它一个实例名称,例如 balloon_mc。
现在我们需要添加监听器。我们将遍历我们的数组,所以我们只需要写一次。
//instead of manually adding to listeners to every city icon movielip we can now
//just loop over all the items in the array
for (var i:int = 0; i < cityIcons.length;i++)
{
var mcCity:MovieClip = cityIcons[i] as MovieClip;
myCity.addEventListener(MouseEvent.MOUSE_OVER, onMouseOver)
myCity.addEventListener(MouseEvent.MOUSE_OUT, onMouseOut)
}
function onMouseOut(e:MouseEvent):void
{
cityTag_mc.visible = false;
//TODO any animating of balloon, maybe you could have
//different labels so instead of changing visible change alpha when your tweening
}
function onMouseOver(e:MouseEvent):void
{
//move balloon to where the city icon is
//e.target refers to the object you have added the listener to
cityTag_mc.x = e.target.x; // move the balloons position to the city's position
cityTag_mc.x = e.target.y;
//you may want to add an offset so its not directly overthe top
cityTag_mc.visible = true;
switch(e.target)
{
//testing which city instance icon we rolled over
case:state_tx
cityTag_mc.title_txt.text ="Texas";
cityTag_mc.balloon_mc.play(); //don't worry about this for now
//do remaining stuff
break;
case:state_ca
//same as above
}
}
您可以在舞台上放置一个名为 cityTag_mc 的气球实例并将可见设置为 false,或者您可以根据需要创建和删除。这只是一个指南,不要把它当作 100%,因为这只是我的想法。