Bir metin bloğundaki veya metin bloğunun bir kısmındaki olayları bir olay göndericisine de yansıtabilirsiniz. İlk olarak, bir EventDispatcher örneği oluşturun ve bunu bir TextElement örneğinin eventMirror nesnesine atayın. Metin bloğu tek bir metin öğesinden oluşuyorsa, metin motoru, olayları metin bloğunun tamamı için yansıtır. Metin bloğu birden çok metin öğesinden oluşuyorsa, metin motoru, olayları yalnızca eventMirror özelliği ayarlanmış olan TextElement örnekleri için yansıtır. Aşağıdaki örnekteki metin, üç öğeden oluşur: "Click" sözcüğü, "here" sözcüğü ve "to see me in italic" dizesi. Örnek, ikinci metin öğesi olan "here" sözcüğüne bir olay göndericisi atar ve bir olay dinleyicisi olan clickHandler() yöntemini ekler. clickHandler() yöntemi, metni italik olarak değiştirir. Ayrıca, üçüncü metin öğesinin içeriğini de "Click here to see me in normal font!" olarak değiştirir.
package
{
import flash.text.engine.*;
import flash.ui.Mouse;
import flash.display.Sprite;
import flash.events.MouseEvent;
import flash.events.EventDispatcher;
public class EventMirrorExample extends Sprite
{
var fontDescription:FontDescription = new FontDescription("Helvetica", "bold");
var format:ElementFormat = new ElementFormat(fontDescription, 18);
var textElement1 = new TextElement("Click ", format);
var textElement2 = new TextElement("here ", format);
var textElement3 = new TextElement("to see me in italic! ", format);
var textBlock:TextBlock = new TextBlock();
public function EventMirrorExample()
{
var myEvent:EventDispatcher = new EventDispatcher();
myEvent.addEventListener("click", clickHandler);
myEvent.addEventListener("mouseOut", mouseOutHandler);
myEvent.addEventListener("mouseOver", mouseOverHandler);
textElement2.eventMirror=myEvent;
var groupVector:Vector.<ContentElement> = new Vector.<ContentElement>;
groupVector.push(textElement1, textElement2, textElement3);
var groupElement:GroupElement = new GroupElement(groupVector);
textBlock.content = groupElement;
createLines(textBlock);
}
private function clickHandler(event:MouseEvent):void
{
var newFont:FontDescription = new FontDescription();
newFont.fontWeight = "bold";
var newFormat:ElementFormat = new ElementFormat();
newFormat.fontSize = 18;
if(textElement3.text == "to see me in italic! ") {
newFont.fontPosture = FontPosture.ITALIC;
textElement3.replaceText(0,21, "to see me in normal font! ");
}
else {
newFont.fontPosture = FontPosture.NORMAL;
textElement3.replaceText(0, 26, "to see me in italic! ");
}
newFormat.fontDescription = newFont;
textElement1.elementFormat = newFormat;
textElement2.elementFormat = newFormat;
textElement3.elementFormat = newFormat;
createLines(textBlock);
}
private function mouseOverHandler(event:MouseEvent):void
{
Mouse.cursor = "button";
}
private function mouseOutHandler(event:MouseEvent):void
{
Mouse.cursor = "arrow";
}
private function createLines(textBlock:TextBlock):void
{
if(textBlock.firstLine)
removeChild (textBlock.firstLine);
var textLine:TextLine = textBlock.createTextLine (null, 300);
textLine.x = 15;
textLine.y = 20;
addChild (textLine);
}
}
}
mouseOverHandler() ve mouseOutHandler() işlevleri, imleç "here" sözcüğünün üzerinde olduğunda imleci bir düğme imleci olarak ayarlar ve imleç sözcüğün üzerinde olmadığında imleci tekrar ok olarak ayarlar.