How to convert different line of a dynamic text bo

2019-06-14 14:20发布

I have a question regarding to my project which is How to convert different line of a dynamic text box to Movie clip in AS3?

Actually, I have an text file named test.txt. For instance:

It consists of:

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

and then I want to put all of them into an array and then a string to show them in the dynamic text Box called text_txt.

I have different objects in my library. If we see "sun" in the first line, the first object (obj01) will be shown in a specific area which is inside a movie clip called mc.

The question is here:

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

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

Thanks for your time and help in advance.

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");
    }
  }
}

2条回答
爷的心禁止访问
2楼-- · 2019-06-14 15:04

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???

split whole of the dynamic-text value with its lines to an Array then check each array item if contains that key ("sun"). you have to use some thing like it 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???

i just give you an example to find the reason of difference in size of movieclip and text-display. this image comes from adobe-flash. a text field with its value "brown fox jumps" has the same size as the green rectangle. but the text-size shown by the red rectangle.

  • Green is textfield size, same as textField.width & textField.height
  • Red is text size, same as textField.textWidth & textField.textHeight

so you must resize your movieclip/textfield to second Values(Red);
my answer is not walkthrough but a guidance.

enter image description here

查看更多
冷血范
3楼-- · 2019-06-14 15:20

After too much research and try a lot of functions, I have find the answer for Second part of this question. I was sure that there must be a way to do it and that's why I made it as my Favorite question.

//
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);
}

And now you can use:

//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");
    }
}
查看更多
登录 后发表回答