2011-08-22 113 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)与句子的边界。

相关问题