var lib = fl.getDocumentDOM().library;
for( var i in lib )
{
for( var j in lib[ i ])
{ //遍历所有库元件
if( lib[ i ][ j ].itemType == "graphic" || lib[ i ][ j ].itemType == "movie clip" )
{ //判断,只有图形元件和影片元件里含有文本
for( var k in lib[ i ][ j ].timeline.layers )
{ //遍历图层
for( var n in lib[ i ][ j ].timeline.layers[ k ].frames )
{ //遍历所有帧
for( var m in lib[ i ][ j ].timeline.layers[ k ].frames[ n ].elements )
{ //遍历所有帧里的可视元素
if( lib[ i ][ j ].timeline.layers[ k ].frames[ n ].elements[ m ] == "[object Text]" )
{ //只有文本才执行下面的操作
if( lib[ i ][ j ].timeline.layers[ k ].frames[ n ].elements[ m ].getTextString() == "愚蠢的熊猫" )
{ //获取你需要操作的字符
lib[ i ][ j ].timeline.layers[ k ].frames[ n ].elements[ m ].setTextString( "聪明的兔子" ); //替换字符
}
}
}
}
}
}
}
}
2. 如上图所示,上面的文本框(实例名:ori_txt)我们可以输入查找的字符,
下面的文本框(实例名:new_txt)我们可以输入要替换成的字符,
最下面的按钮(实例名:btn)是用来执行JSFL的。
3. 接下来我们在AS层加入代码。首先为按钮编写侦听点击事件和执行函数:
btn.addEventListener( MouseEvent.CLICK, runScript );
function runScript( e:MouseEvent )
{
var otxt:String = ori_txt.text; //点击按钮后读入文本框的字符
var ntxt:String = new_txt.text; //同上
var jsfl:String;
MMExecute( jsfl );
}
var lib = fl.getDocumentDOM().library;for(var i in lib){for(var j in lib[i]){if(lib[i][j].itemType=='graphic'||lib[i][j].itemType=='movie clip'){for(var k in lib[i][j].timeline.layers){for(var n in lib[i][j].timeline.layers[k].frames){for(var m in lib[i][j].timeline.layers[k].frames[n].elements){if(lib[i][j].timeline.layers[k].frames[n].elements[m] == '[object Text]'){if(lib[i][j].timeline.layers[k].frames[n].elements[m].getTextString()=='愚蠢的熊猫'){lib[i][j].timeline.layers[k].frames[n].elements[m].setTextString('聪明的兔子');}}}}}}}}
var str1:String = "fl.outputPanel.clear();fl.outputPanel.trace('Start replacing text...');var lib = fl.getDocumentDOM().library;"
var str2:String = "for(var i in lib){for(var j in lib[i]){if (lib[i][j].itemType=='graphic'||lib[i][j].itemType=='movie clip'||lib[i][j].itemType=='button'){for(var k in lib[i][j].timeline.layers){for(var n in lib[i][j].timeline.layers[k].frames){for(var m in lib[i][j].timeline.layers[k].frames[n].elements){if(lib[i][j].timeline.layers[k].frames[n].elements[m]=='[object Text]'){if(lib[i][j].timeline.layers[k].frames[n].elements[m].getTextString()=='";
var str3:String = "'){lib[i][j].timeline.layers[k].frames[n].elements[m].setTextString('";
var str4:String = "');fl.outputPanel.trace('---');}}}}}}}}";
var str5:String = "fl.outputPanel.trace('All text replaced.');";
var jsfl:String = str1 + str2 + otxt + str3 + ntxt + str4 + str5;
MMExecute( jsfl );