2013-05-17 33 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? –