如何实现无法触摸文字的效果?
解决思路
这个效果又一个特别之处在于使用了动态文本,利用AS判断文本变量,通过数组的运算赋值数标语文字的距离,从而达到无法触摸的文字效果。
具体步骤:
1、 运行Flash MX 2004,设置文档背景为黑色。
2、 新建一个影片剪辑,在影片剪辑编辑区内键入一个动态文本。在属性面板中设置文本变量为“tt”。如图3.3.89所示。

图3.3.89 设置文本变量
3、 在该影片剪辑时间轴上新建一个图层2,单击矩形工具绘制一个无边框,填充颜色为透明的矩形。按F8转换为影片剪辑2,并在下面的属性面板中命名为“ff”。如图3.3.90所示。

图3.3.90 设置影片剪辑属性
4、返回到场景1,单击第1帧,打开as面板,添加如下语句:
_root.onLoad = function() {
t = " this is flash8";
myt = new Array();
l = t.length;
for (i=0; i<=l-1; i++) {
myt[i] = substring(t, i+1, 1);
}
_root.j = 0;
};
function out() {
_root.attachMovie("tt", "tt"+_root.j, _root.j);
if (_root.j<=(l-1)) {
_root["tt"+_root.j].tt = myt[_root.j];
_root["tt"+0]._x = 100;
_root["tt"+_root.j]._x = _root["tt"+(_root.j-1)]._x+20;
_root["tt"+_root.j]._y = 150;
_root.j = _root.j+1;
} else {
_root.effect();
}
}
function effect() {
for (n=0; n<=l-1; n++) {
if (_root._ymouse-_root["tt"+_root.n]._y<=100 && _root._xmouse-_root["tt"+_root.n]._x<=10 && _root._xmouse-_root["tt"+_root.n]._x>=-10) {
_root["tt"+_root.n].dy += (50-_root["tt"+_root.n]._y)*.3;
} else {
_root["tt"+_root.n].dy += (150-_root["tt"+_root.n]._y)*.3;
}
_root["tt"+_root.n].dy *= .8;
_root["tt"+_root.n]._y += _root["tt"+_root.n].dy;
}
}
_root.onEnterFrame = out;
5、上面的AS解释为:
_root.onLoad = function() {//一开始加载前定义变量
t = " this is flash8";
myt = new Array();//myt为数组变量
l = t.length;
for (i=0; i<=l-1; i++) {//将每个字,付给到数组
myt[i] = substring(t, i+1, 1);
}
_root.j = 0;//全局变量
};
function out() {//定义函数out
_root.attachMovie("tt", "tt"+_root.j, _root.j);//加载MC tt,重命名为"tt"+_root.j
if (_root.j<=(l-1)) {//判断t文本的字节数
_root["tt"+_root.j].tt = myt[_root.j];//将刚才付给数组的文字逐个付给新mc"tt"+_root.j
_root["tt"+0]._x = 100;
_root["tt"+_root.j]._x = _root["tt"+(_root.j-1)]._x+20;
_root["tt"+_root.j]._y = 150;
//以上是控制每个字符的位置
_root.j = _root.j+1;//变量递加
} else {
_root.effect();//调用effect函数
}
}
function effect() {
for (n=0; n<=l-1; n++) {
//判断鼠标和字符间的位置
if (_root._ymouse-_root["tt"+_root.n]._y<=100 && _root._xmouse-_root["tt"+_root.n]._x<=10 && _root._xmouse-_root["tt"+_root.n]._x>=-10) {
_root["tt"+_root.n].dy += (50-_root["tt"+_root.n]._y)*.3;
} else {
_root["tt"+_root.n].dy += (150-_root["tt"+_root.n]._y)*.3;
}
_root["tt"+_root.n].dy *= .8;
_root["tt"+_root.n]._y += _root["tt"+_root.n].dy;
//以上是弹性效果的算法。
}
}
_root.onEnterFrame = out;//调用out函数
6、请参考源文件“跳动的文字”。
特别说明
在as上加上:
System.UseCodePage = true;可以支持中文字符。
