如何将动态文本框的不同行转换为 AS3 中的影片剪辑

How to convert different line of a dynamic text box to Movie clip in AS3

我有一个关于我的项目的问题,即如何将动态文本框的不同行转换为 AS3 中的影片剪辑?

实际上,我有一个名为 test.txt 的文本文件。例如:

它包括:

today is Sun; today is Mon; today is Tue; today is Wed; today is Thu; today is Fri; today is Sat;

然后我想将它们全部放入一个数组中,然后将一个字符串显示在名为 text_txt 的动态文本框中。

我的图书馆里有不同的物品。如果我们在第一行看到 "sun",第一个对象 (obj01) 将显示在名为 mc.

的影片剪辑内的特定区域中

问题在这里:

拳头:如果我的第一行有不同的文本。例如 "today is sun;"。如何找到 "sun" 在这一行???

二:如何将动态文本框的不同行转换为影片剪辑。所以,如果用户点击 "obj01",动态文本框中的第一行会变大???

提前感谢您抽出宝贵时间提供帮助。

import flash.events.MouseEvent;

var flag:int = 0;


//load the txt file
var myTextLoader:URLLoader = new URLLoader();
myTextLoader.addEventListener(Event.COMPLETE, onLoaded);
myTextLoader.load(new URLRequest("test.txt"));

//when the scene loads, all the info from txt file shown into the dynamic text;
function onLoaded(e:Event):void
{
//put all the info into array
var days:Array = e.target.data.split(/\n/);

var str:String;
//show them in the dynamic text
for (var i=0; i<days.length; i++)
{
    str = days[i];
    text_txt.appendText(str + "\n");

    //if one text founded, do somethind and shoe an objectinthe output;
    switch (str)
    {
        case "sun;\r" :
            var obj01:Show = new Show();
            mc.addChild(obj01);
            obj01.x = -200;
            obj01.y = -15;
            break;

        default :
            trace("None of the above were met");
    }
}

obj01.buttonMode = true;


//if the object clicked then something must be happend to the first line in the dynamic text box
obj01.addEventListener(MouseEvent.CLICK, firstLine);
function firstLine(e:MouseEvent):void
{
    flag = 1;

    switch (flag)
    {
        case 1 :
            trace("Clicked!");
            //the first line in the text box should get a different background and become more bigger
            //Do I need to convert the first line to a movieclip? OR I need to do in another way?! 
            break;

        default :
            trace("None");
    }
  }
}

First: if I have different texts in my first line. for instance "today is sun;". how to find that the "sun" is in this line???

将整个动态文本值及其行拆分为一个数组 然后检查每个数组项是否包含该键 ("sun")。 你必须使用类似的东西 Array[x].indexOf(key) >= 0

Second: How to convert different line of a dynamic text box to Movie clip. So, is user click on the "mc.obj01", the first line in the dynamic textbox will become bigger???

我只是给你一个例子来找出动画片段和文本显示大小不同的原因。此图片来自 adobe-flash。值为 "brown fox jumps" 的文本字段与绿色矩形的大小相同。但是红色矩形显示的文本大小。

  • 绿色是文本框大小,与textField.width & textField.height
  • 相同
  • 红色为文字大小,与textField.textWidth & textField.textHeight
  • 相同

因此您必须将 movieclip/textfield 的大小调整为第二个值(红色);
我的回答不是演练而是指南。

经过大量的研究和尝试了很多功能,我找到了这个问题第二部分的答案。 我确信一定有办法做到这一点,这就是为什么我把它作为我最喜欢的问题。

//
var newMcTitle:Array = [];

//put all the info into array
var days:Array = e.target.data.split(/\n/);

for (var i=0; i<days.length; i++)
{
    str = days[i];

    //put the texts line by line into a textfield and put the textfield into a movie clip
    var newMc:MovieClip = new MovieClip();
    var newText:TextField = new TextField();
    newText.text = days[i];
    newMc.addChild(newText);
    newMc.x = 30;
    newMc.y = positionY;
    newMc.scaleX = 2;
    newMc.scaleY = 2;
    addChild(newMc);
    positionY +=  30;

    //unique instance names:
    newMc.name = days[i];// or more generally when no arrays used
    newMc.name = 'mc_' + i;
    //trace(newMc.name);

    //asssign unique property to be used to identify newMc
    newMc.ivar = i;

    //populate an array instantiated outside for-loop (eg, var newMcA:Array=[];)
    newMcTitle.push(newMc);
}

现在您可以使用:

//if the object clicked then something must be happend to the first line in the dynamic text box
obj01.addEventListener(MouseEvent.CLICK, firstLine);
function firstLine(e:MouseEvent):void
{
    flag01 +=  1;

    switch (flag01)
    {
        case 1 :
            //the first line in the text box should get a different background and become more bigger
            newMcTitle[0].scaleX *=  1.2;
            newMcTitle[0].scaleY *=  1.2;
            break;

        default :
            trace("None");
    }
}