Actionscript 3 AS3中动态文本框的appendText和htmlText组合
您好,我浏览了网页和本网页,寻找答案,但似乎找不到解决问题的方法。我创造了打字机的效果。它通过动态文本框(tekst_txt)显示。我想实现的是能够使用html标记将特定单词的字体更改为粗体或斜体,只需包括和,但我似乎无法做到这一点。如果你能给我一些建议,我将不胜感激 这是显示在第一帧中的代码(该帧上不存在文本框): 导入flash.events.MouseEventActionscript 3 AS3中动态文本框的appendText和htmlText组合,actionscript-3,flash,Actionscript 3,Flash,您好,我浏览了网页和本网页,寻找答案,但似乎找不到解决问题的方法。我创造了打字机的效果。它通过动态文本框(tekst_txt)显示。我想实现的是能够使用html标记将特定单词的字体更改为粗体或斜体,只需包括和,但我似乎无法做到这一点。如果你能给我一些建议,我将不胜感激 这是显示在第一帧中的代码(该帧上不存在文本框): 导入flash.events.MouseEvent stop(); var tekst:String = ""; var i:uint = 0; var licznik:Ti
stop();
var tekst:String = "";
var i:uint = 0;
var licznik:Timer = new Timer(20);
tekst_txt.htmlText = tekst_txt.text;
stage.addEventListener(MouseEvent.CLICK, klikaj);
function klikaj(event:MouseEvent):void
{
if (licznik.running == true)
{
tekst_txt.htmlText = tekst;
licznik.stop();
}
else if (licznik.running == false || licznik == null)
{
nextFrame();
tekst_txt.text = "";
}
}
这是下一帧的代码(此帧中已存在文本框):
导入flash.events.MouseEvent;
导入flash.utils.Timer;
导入flash.events.TimerEvent;
停止();
tekst=“TEKST1TEKST1TEKST1TEKST1TEKST1TEKST1TEKST1TEKST1TEKST1”;
licznik.start();
licznik.addEventListener(TimerEvent.TIMER,odpalaj);
函数odpalaj(e:TimerEvent):无效
{
//tekst_txt.htmlText=tekst_txt.text;
tekst_txt.appendText(tekst.charAt(i));
//tekst_txt.htmlText=tekst_txt.text;
i++;
如果(i>=tekst.长度)
{
licznik.stop();
}
}
您所面临的问题是,任何形式的HTML格式都需要超过1个字符来描述,因此,当您尝试逐个字符执行此动画时,实际上只是将原始HTML标记设置到文本中
这可能看起来有点混乱,但这里有一些东西你可以试试
您将创建一个临时文本字段,并首先将整个html标记文本设置为其htmlText值,然后您可以getTextFormat在追加时复制每个字符的格式。。。这允许Flash为您处理html
import flash.events.MouseEvent;
import flash.utils.Timer;
import flash.events.TimerEvent;
stop();
tekst="Tekst1Tekst1<i>Tekst1</i>Tekst1Tekst1Tekst1Tekst1Tekst1Tekst1Tekst1";
// shove your html markup text into the htmlText of a textfield
// this allows Flash to deal with parsing the html
var myTextField:TextField = new TextField();
myTextField.htmlText = tekst;
licznik.start();
licznik.addEventListener(TimerEvent.TIMER, odpalaj);
function odpalaj(e:TimerEvent):void
{
// get the text directly from the temp textfield
// you want to do this because it will have already processed the html markup
// and will give you the correct indexes and length of your text
tekst_txt.appendText(myTextField.text.charAt(i));
// copy the textformat
var format:TextFormat = myTextField.getTextFormat(i, i+1);
tekst_txt.setTextFormat(format, i, i+1);
i++;
if (i >= tekst.length)
{
licznik.stop();
}
}
导入flash.events.MouseEvent;
导入flash.utils.Timer;
导入flash.events.TimerEvent;
停止();
tekst=“TEKST1TEKST1TEKST1TEKST1TEKST1TEKST1TEKST1TEKST1TEKST1”;
//将html标记文本放入文本字段的htmlText中
//这使得Flash能够处理对html的解析
var myTextField:TextField=newtextfield();
myTextField.htmlText=tekst;
licznik.start();
licznik.addEventListener(TimerEvent.TIMER,odpalaj);
函数odpalaj(e:TimerEvent):无效
{
//直接从临时文本字段获取文本
//您希望这样做,因为它已经处理了html标记
//并将为您提供正确的索引和文本长度
tekst_txt.appendText(myTextField.text.charAt(i));
//复制文本格式
var format:TextFormat=myTextField.getTextFormat(i,i+1);
tekst_txt.setTextFormat(格式,i,i+1);
i++;
如果(i>=tekst.长度)
{
licznik.stop();
}
}
听起来不错。我试试看。非常感谢你的建议,它很有效。好极了,考沙尔。在下一帧中,我得到了:'code'导入flash.events.MouseEvent;导入flash.utils.Timer;导入flash.events.TimerEvent;停止();tekst=“tekst2tekst2tekst2tekst2tekst2”;i=0;licznik.start();'代码“它仍然显示前一帧的文本。当你说下一帧时,我假设我们谈论的是第3帧。。。没有循环回到第1帧?因为你在追加文本,所以你永远不会清除tekst_txt.text的内容。。。您应该在stop()之后添加这一行<代码>tekst_txt.htmlText=“”代码>是,下一帧是3号。如果你看frame1的最后一行,它有“tekst_txt.text=”“;”我用它来结算。我把它改成了tekst_txt.htmlText=“”;”我也试着像你建议的那样把它放在停止后,但它不起作用。仍在第3帧上显示第2帧中的文本。Kaushal还有其他建议吗?
import flash.events.MouseEvent;
import flash.utils.Timer;
import flash.events.TimerEvent;
stop();
tekst="Tekst1Tekst1<i>Tekst1</i>Tekst1Tekst1Tekst1Tekst1Tekst1Tekst1Tekst1";
// shove your html markup text into the htmlText of a textfield
// this allows Flash to deal with parsing the html
var myTextField:TextField = new TextField();
myTextField.htmlText = tekst;
licznik.start();
licznik.addEventListener(TimerEvent.TIMER, odpalaj);
function odpalaj(e:TimerEvent):void
{
// get the text directly from the temp textfield
// you want to do this because it will have already processed the html markup
// and will give you the correct indexes and length of your text
tekst_txt.appendText(myTextField.text.charAt(i));
// copy the textformat
var format:TextFormat = myTextField.getTextFormat(i, i+1);
tekst_txt.setTextFormat(format, i, i+1);
i++;
if (i >= tekst.length)
{
licznik.stop();
}
}