2013-05-17 122 views
0

嗨我浏览了网页和此网页的答案,但似乎无法找到解决方案,我的问题。我创造了打字机效果。它通过动态文本框(tekst_txt)显示。我想达到的目的是能够使用html标签,通过包括即< b>和</b>将特定单词的字体更改为粗体或斜体,但我似乎无法解决这个问题。我真的很感激一些建议。在AS3中结合appendText和htmlText动态文本框

这是在第一帧中显示的代码(该框架上不存在文本框): import flash.events.MouseEvent;

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 = ""; 

} 
} 

这是从下一帧代码(文本框在这个框架已经存在):

import flash.events.MouseEvent; 
import flash.utils.Timer; 
import flash.events.TimerEvent; 
stop(); 
tekst="Tekst1Tekst1<i>Tekst1</i>Tekst1Tekst1Tekst1Tekst1Tekst1Tekst1Tekst1"; 
licznik.start(); 
licznik.addEventListener(TimerEvent.TIMER, odpalaj); 
function odpalaj(e:TimerEvent):void 
{ 
//tekst_txt.htmlText = tekst_txt.text; 
tekst_txt.appendText(tekst.charAt(i)); 
//tekst_txt.htmlText=tekst_txt.text; 
i++; 
if (i >= tekst.length) 
{ 
    licznik.stop(); 
} 
} 

回答

0

你所面对的问题是,任何形式的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(); 
    } 
} 
+0

听起来不错。我会尝试的。非常感谢您的建议。 – aya9

+0

它的工作原理。不错的@Kaushal。事情是在我已经得到的下一帧:'code'import flash.events.MouseEvent; import flash.utils.Timer; import flash.events.TimerEvent; stop(); tekst =“tekst2 tekst2tekst2tekst2”; i = 0; licznik.start();'code'它仍然显示前一帧的文本。 – aya9

+0

当你说下一帧时,我假设我们正在谈论帧3 ...没有循环回帧1? –