1

这是 actionScript 3.0 中的 textField.htmlText 和我为它制作的类的问题。

为了不发布我的整个课程,我设法将问题归结为一个看似愚蠢且几乎不可能克服的问题。似乎 TextFields(特别是使用 ActionScript 创建的)对于由

myTextField.htmlText =  myString.substr(0, length);

其中长度是一个不断增加的增量。然而,让我们举一个更具体的例子。首先,我们在 .fla 文件的第 1 帧上有一些随机字符串:

var Text0:String = 'It wasn\'t like we didn\'t have love. John and I shared much of our love, but for some reason something went terribly wrong one day...\nWe talked less, we saw each other less, we hardly even got to know each other anymore. The love was dried up, and if I stayed I would have withered, and choked on words I could never share...';

使用我的类,它需要这个字符串,对于 X 关键字,在这些词周围应用 Y HTML 标签:

var textBuildah = new textBuilder(); //Class name, for this example, is textBuilder
Text0 = textBuildah.htmlModify(Text0, ['love', 'red'], ['we', '~#FCFE12']);

这需要 Text0(我们仍处于第 1 帧的动作中)并对其执行以下操作:

trace(Text0) // Outputs: It wasn't like <FONT COLOR="#FCFE12">we</FONT> didn't have <FONT COLOR="#FF0000">love</FONT>. John and I shared much of our <FONT COLOR="#FF0000">love</FONT>, but for some reason something <FONT COLOR="#FCFE12">we</FONT>nt terribly wrong one day... We talked less, <FONT COLOR="#FCFE12">we</FONT> saw each other less, <FONT COLOR="#FCFE12">we</FONT> hardly even got to know each other anymore. The <FONT COLOR="#FF0000">love</FONT> was dried up, and if I stayed I would have withered, and choked on words I could never share...

我假设所有这些 HTML 标签都是正确的。然后,我们这样做:

textBox.htmlText = Text0 //textBox is a text field that was put on the stage and named "textBox" using the properties window

它完全按照我想要的方式显示(这张图片的右下角)[i49.tinypic.com/2upts1v.jpg]。添加 http:// 以查看它。

然后,我删除上面的代码行,然后试试这个:

var Length:Number = 0;
textBox.addEventListener(Event.ENTER_FRAME, writeText);

function writeText(e:Event):void {
    if (Length < Text0.length) {
        Length ++;
        e.currentTarget.htmlText = Text0.substring(0, Length);
        trace(e.currentTarget.htmlText+"\n"+e.currentTarget.name+"\n");
    } else {
        e.currentTarget.htmlText = Text0;
        e.currentTarget.removeEventListener(Event.ENTER_FRAME, writeText);
    }
}

而且,它就像做 textBox.htmlText = Text0;

然后,我们尝试一些新的东西。我们使用 ActionScript 创建一个 textField,并对其应用相同的事件侦听器:

var LengthTwo:Number = 0;
var myTextField = new TextField();
myTextField.x = 100
myTextField.y = 100;
myTextField.width = 200;
myTextField.height = 150;
myTextField.wordWrap = true;

addChild(myTextField);

mytextField.addEventListener(Event.ENTER_FRAME, writeText);

function writeTextTwo(e:Event):void {
if (LengthTwo < Text0.length) {
    LengthTwo++;
    e.currentTarget.htmlText = Text0.substring(0, Length);
    trace(e.currentTarget.htmlText+"\n"+e.currentTarget.name+"\n");
} else {
    e.currentTarget.htmlText = Text0;
    e.currentTarget.removeEventListener(Event.ENTER_FRAME, writeText);
}
}

它会产生您在这张图片的中心看到的东西。[ i49.tinypic.com/2upts1v.jpg ]。添加 http:// 以查看它。此外,当它键入时,它会频繁且迷失方向地改变颜色。我做了一些跟踪,我意识到 myTextField 的 htmlText 在它的开头有额外的标签。这是其中一条痕迹的快照:

<P ALIGN="LEFT"><FONT FACE="Times Roman" SIZE="12" COLOR="#FF0000" LETTERSPACING="0" KERNING="0">It wasn&apos;t like <FONT COLOR="#FCFE12">we</FONT> didn&apos;t have love. John and I shared much of our love, but for some reason something <FONT COLOR="#FCFE12">we</FONT>nt terribly wrong one day...</FONT></P><P ALIGN="LEFT"><FONT FACE="Times Roman" SIZE="12" COLOR="#FF0000" LETTERSPACING="0" KERNING="0">We talked less, <FONT COLOR="#FCFE12">we</FONT> saw each other less, <FONT COLOR="#FCFE12">we</FONT> hardly even got to know each other anymore. The love was dried up, and if I stayed I would have withered, and choked on words I could never share..</FONT></P>
instance5

<TEXTFORMAT LEADING="2"><P ALIGN="LEFT"><FONT FACE="Calibri" SIZE="10" COLOR="#FFFFFF" LETTERSPACING="0" KERNING="0">It wasn&apos;t like <FONT COLOR="#FCFE12">we</FONT> didn&apos;t have <FONT COLOR="#FF0000">love</FONT>. John and I shared much of our <FONT COLOR="#FF0000">love</FONT>, but for some reason something <FONT COLOR="#FCFE12">we</FONT>nt terribly wrong one day...</FONT></P></TEXTFORMAT><TEXTFORMAT LEADING="2"><P ALIGN="LEFT"><FONT FACE="Calibri" SIZE="10" COLOR="#FFFFFF" LETTERSPACING="0" KERNING="0">We talked less, <FONT COLOR="#FCFE12">we</FONT> saw each other less, <FONT COLOR="#FCFE12">we</FONT> hardly even got to know each other anymore. The <FONT COLOR="#FF0000">love</FONT> was dried up, and if I stayed I would have withered, and choked on words I could never share..</FONT></P></TEXTFORMAT>
textBox

我已经意识到,在整个过程中,放置在舞台上并设置为渲染 HTML 标签的文本字段将保留它的开始标签

<TEXTFORMAT LEADING="2"><P ALIGN="LEFT"><FONT FACE="Calibri" SIZE="10" COLOR="#FFFFFF" LETTERSPACING="0" KERNING="0">

在那里,只需对里面的指定部分进行更改。但是,对于添加了 VIA Actionscript 3.0 的文本框,它会随着 writeText 函数的继续而更改上述任何起始标记,并且 subString 开始包含越来越多的 HTML 标记。

有谁知道我错过了什么?我需要定义一个属性来消除这种奇怪的效果吗?

此外,如果解释太长,这是一个 .fla 会产生与我一直遇到的完全相同的问题。中间的文本字段是问题所在。右下角的 textField 没有问题。

如果您无法处理为 CS3/4 制作的 .fla,请执行以下操作:

var Text0:String = 'It wasn\'t like <font color = "#FCFE12">we</font> didn\'t have <font color = "#FF0000">love</font>. John and I shared much of our <font color = "#FF0000">love</font>, but for some reason something <font color = "#FCFE12">we</font>nt terribly wrong one day...\nWe talked less, <font color = "#FCFE12">we</font> saw each other less, <font color = "#FCFE12">we</font> hardly even got to know each other anymore. The <font color = "#FF0000">love</font> was dried up, and if I stayed I would have withered, and choked on words I could never share...';
var myTextField:TextField = new TextField();
var Length:Number = 0;
var LengthTwo:Number = 0;
myTextField.x = 75;
myTextField.y = 100;
myTextField.width = 400;
myTextField.height = 150;
myTextField.wordWrap = true;
myTextField.textColor = 0xFFFFFF;
addChild(myTextField);

textBox.addEventListener(Event.ENTER_FRAME, writeText);
myTextField.addEventListener(Event.ENTER_FRAME, writeTextTwo);

function writeText(e:Event):void {
    if (Length < Text0.length) {
        Length ++;
        e.currentTarget.htmlText = Text0.substring(0, Length);
        trace(e.currentTarget.htmlText+"\n"+e.currentTarget.name+"\n");
    } else {
        e.currentTarget.htmlText = Text0;
        e.currentTarget.removeEventListener(Event.ENTER_FRAME, writeText);
    }
}

function writeTextTwo(e:Event):void {
if (LengthTwo < Text0.length) {
    LengthTwo++;
    e.currentTarget.htmlText = Text0.substring(0, Length);
    trace(e.currentTarget.htmlText+"\n"+e.currentTarget.name+"\n");
} else {
    e.currentTarget.htmlText = Text0;
    e.currentTarget.removeEventListener(Event.ENTER_FRAME, writeText);
}
}

到目前为止,我已经尝试在 textField 定义的正下方为 myTextField 设置 TextFormat。然后我尝试在该字段之外设置一个特定的 TextFormat 。我尝试将 defaultTextFormat 设为一种新格式,我尝试在 writeText 函数中重置样式表……这对我来说有点像后勤方面的噩梦。我不知道如何阻止 .fla 中奇怪的颜色变化......

然而,问题不仅仅发生在颜色上。它以粗体、斜体等方式发生,您将其放入函数中并对其进行类型编写效果。但是,如果您只是设置 htmlText = x,那么它看起来还不错。

有任何想法吗?

编辑:使用粗体、斜体、下划线等实现相同的东西似乎有一个小问题。如果有人对如何使用上述文本执行此操作有任何想法,或者使用带有粗体、斜体的 htmltext 创建类似的效果等 - 在这里回答!

4

1 回答 1

1

您可以在 IDE 创建的TextField中看到,只要您更改重置字体、颜色、大小等的文本,就会出现默认的TextFormat 。

因此,对于您的动态 as3 TextField 创建一个新的 TextFormat ,其中包含您要保留的所有格式,然后在您的事件侦听器中,只需在写入之前重置 defaultTextFormat 属性:

var Text0:String = 'It wasn\'t like <font color = "#FCFE12">we</font> didn\'t have <font color = "#FF0000">love</font>. John and I shared much of our <font color = "#FF0000">love</font>, but for some reason something <font color = "#FCFE12">we</font>nt terribly wrong one day...\nWe talked less, <font color = "#FCFE12">we</font> saw each other less, <font color = "#FCFE12">we</font> hardly even got to know each other anymore. The <font color = "#FF0000">love</font> was dried up, and if I stayed I would have withered, and choked on words I could never share...';
var myTextField:TextField = new TextField();
var Length:Number = 0;
var LengthTwo:Number = 0;
myTextField.x = 75;
myTextField.y = 100;
myTextField.width = 400;
myTextField.height = 150;
myTextField.wordWrap = true;
addChild(myTextField);

// create a TextFormat
var tf:TextFormat = new TextFormat("Calibri", 10, 0xffffff);

myTextField.addEventListener(Event.ENTER_FRAME, writeTextTwo);

function writeTextTwo(e:Event):void {
  if (LengthTwo < Text0.length) {
    LengthTwo++;

    // reset the default TextFomat
    e.currentTarget.defaultTextFormat = tf;

    e.currentTarget.htmlText = Text0.substring(0, Length);
    trace(e.currentTarget.htmlText+"\n"+e.currentTarget.name+"\n");
  } else {
    e.currentTarget.htmlText = Text0;
    e.currentTarget.removeEventListener(Event.ENTER_FRAME, writeText);
  }
}
于 2010-03-02T09:03:09.720 回答