我有一张美国地图,上面有许多城市的图标(大约12个)。当用户将鼠标悬停在其中一个图标上时,会在图标上弹出一个带有两个动态文本字段的动画片段。
每个城市的图标动画片段都以其家乡状态命名:state_(abbreviation)
即:state_TX
弹出文字气球的名称为:cityTag_mc
其中有两个动态文本字段:title_txt
& subTitle_txt
当用户将鼠标悬停在cityTag_mc
上并输入该州的标题和子标题时,逻辑是添加state_TX
。
我的主要问题是如何将文字输入字段(并为气球设置动画)。我不知道从哪里开始。我想在Action Script中为每个状态设置文本。我从哪里开始?什么是最佳做法?
答案 0 :(得分:0)
创建完所有城市图标并将其放置在舞台上并为其指定实例名称后,将它们放入数组中。这只是为了让事情变得更容易管理。
cityIcons.push(state_tx), cityIcons.push(state_ca)
等
现在我们需要添加代码以显示气球。你也提到了它的动画效果。将气球的动画放置在textfields下面的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%,因为这只是我的头脑。