2011-08-22 51 views
1

我想將一些鼠標事件放在動態文本字段中滾動文本。如何添加事件偵聽器來滾動內容?

例如:我在幾個句子中輸入一個動態文本字段。對於每個句子,我想要有一個不同的鼠標事件。就像這樣:

sentenceA.addEventListener(MouseEvent.CLICK, functionA); 

function functionA(evt:MouseEvent):void 
{ 
trace("bla"); 
} 

我怎樣才能在滾動文本的事件監聽添加到每個句子?因爲當相對文本滾動時,鼠標事件的位置應該移動。

+0

不錯的問題! – Eugeny89

回答

0

在這裏你去...

_txt.htmlText = "<a href='event:sentence1'>This is one sentenc.</a> <a href='event:sentence2'>This is another sentence.</a>" 
_txt.addEventListener(TextEvent.LINK, clickCopyHandler); 

function clickCopyHandler(e:TextEvent):void { 
    trace(e.text); 
} 
+0

非常感謝你!會嘗試它! – nancy

0
 var sentences:Array = [ sentenceA, sentenceB, ...... ]; 

    function clickAllText(e:MouseEvent):void 
    { 
     var index:int; 
     for each(var s:String in sentences) 
     { 
      index = dynamicTextField.text.indexOf(s); 
      if(index >= 0 && index <= 3) 
      { 
       //if the sentence is in the front 
       this["sentence"+sentences.indexOf(s)](); 
       break;                               
      } 
     } 
    } 

    function sentence0():void 
    { 
    } 

    function sentence1():void 
    { 
    } 

如果不是的話,你就需要要麼使用不同的文本字段,或 獲取鼠標點附近來了一句X和y位置在 顯示。

0

仔細看看flash.event.TextEvent.LINK,當您點擊具有屬性href ='event:someText'的html文本時,它會被拋出。考慮下面的例子

var tf:TextField = new TextField(); 
tf.htmlText = "<a href='event:first'>sentence1.</a><a href='event:second'>sentence2.</a>"; 
tf.addEventListener("link", clickHandler); //link is value of flash.event.TextEvent.LINK 

function clickHandler(e:TextEvent):void { 
    //here should be something like the following 
    if (e.text == "first") sentence1(); 
    else if (e.text == "second") sentence2(); 
} 

好吧,我想你抓住了這個主意!問題在於句子將被放入標籤,但您可以使鏈接看起來像普通文本。如果該變體不符合您的需求,您可以嘗試在點擊後比較選擇邊界(tf.selectionBeginIndextf.selectionEndIndex)與句子的邊界。

相關問題