A Flash Developer Resource Site

Results 1 to 12 of 12

Thread: Text from file in multiple layers.

  1. #1
    Member
    Join Date
    Mar 2014
    Posts
    32

    Text from file in multiple layers.

    Hello, so I have a text file with a text "Text for layer 1", and layer 1 with one dynamic text field.
    But what I need, is that, if the text file consists of two lines, the second text line displays in the second layer's dynamic text field.

    Right now, its working only in the first layer and I also have the button to go to the second layer in which the second text line should be displayed.

    So my questions are :

    How the text file should be changed, so the flash reads the second line for the second layer?
    How to display the second line text in the second layer in flash?


    I hope You understand..I added the files also!

    example1.flaexample1.txt

  2. #2
    Designer, Programmer, Musician angelhdz's Avatar
    Join Date
    Mar 2010
    Posts
    971
    Actionscript in the first frame:
    stop();

    var sentences:Array = [];
    var Layer = 0;

    mainText.text = "Layer "+Layer;

    myData = new LoadVars();
    myData.onLoad = function(success)
    {
    if(success)
    {
    //Add the value of the variable "contents" from the textfile to
    //a new flash's side variable named "contents"
    var contents = this.contents;
    //Split the variable by its "|" character to separate
    //each text and push it into the sentences rray
    sentences = contents.split("|");

    //Display the corresponding text to the myText_txt textfield
    //from the sentences array
    myText_txt.text = sentences[Layer];
    }
    };

    myData.load("example1.txt");

    //Next button
    but_next.onPress = function()
    {
    //Increase the Layer number each time the Next button is pressed
    Layer++;
    //Go to the next frame
    nextFrame();
    //Display the Layer number in the mainText textfield
    mainText.text = "Layer "+Layer;
    //Display the corresponding text from the sentences array in the
    //myText_txt textfield
    myText_txt.text = sentences[Layer];
    }
    In the text file:
    contents=Text for layer 0|Another text for layer 1|Yeah, other text for layer 2|And text for layer 3|
    Already mastering AS3. It was so fun. Now into Javascript and PHP and all its libraries

  3. #3
    Member
    Join Date
    Mar 2014
    Posts
    32
    Thank You very much!

  4. #4
    Member
    Join Date
    Mar 2014
    Posts
    32

    Question One more important question....

    One thing I would like to add, that would be very very important detail for this topic..I would like to know, would this be somehow possible to make in Flash..

    So lets say, I have made 5 layers and I have 5 seperated sentences in my text file. I go throught all 5 layers, read my 5 written sentences in each layer and its fine. But what to do, if, for instance, I change the text file to only 4 seperated sentences? It means the fifth layer stays empty, right?

    Would it be somehow possible, that when the text file is all read, the layer 4(which would be the last layer if we would have 4 seperated sentences in our text file) doesnt have the button "next word", but has a button "back to layer 1"..

    I know its pretty tricky and hard to explain..I hope You understand what I mean to say.

  5. #5
    Designer, Programmer, Musician angelhdz's Avatar
    Join Date
    Mar 2010
    Posts
    971
    Yeah it can be done i will work on it and i will let you know how to make it. Basically in the script i made to you, each text from the text file is pushed or inserted into an array, so the array will have like a "place" for each sentence. The first place of the array is the index 0, next index 1, and so on. So we can tell something like "if ( Layer <= Array.length) { //keep showing the next button } else { nextButton._visible = false; previousButton._visible true; }

    Something like that ^_^
    Already mastering AS3. It was so fun. Now into Javascript and PHP and all its libraries

  6. #6
    Designer, Programmer, Musician angelhdz's Avatar
    Join Date
    Mar 2010
    Posts
    971
    Here you go.
    http://www.mediafire.com/download/dl...c/example1.zip

    If you can not open the file, just duplicate the next button in the library, change the textfield text to "previous button", place it to the of the stage, and the next button to the right, then replace the script:

    ///////////////////IMPORTANT///////////////////////////////////
    ///You have to insert the corresponding frame for each Layer///
    ///////////////////////////////////////////////////////////////

    //VARIABLES
    var Sentences:Array = [];
    var Layer = 0;
    var Quantity:Number = 6; //Number of sentences in the text file

    //INIT
    stop();
    mainText.text = "Layer "+Layer;


    //FUNCTIONS

    //Load sentences from text file
    myData = new LoadVars();
    myData.onLoad = function(Success:Boolean)
    {
    if(Success)
    {
    //Loop through all the sentences in the text file
    //and push/insert them into the Sentences array/list
    for(var i:Number = 0; i < Quantity; i++)
    {
    Sentences.push(this["content"+i]);
    }
    //Display the corresponding text in myText_txt textfield
    //from the sentences array
    myText_txt.text = Sentences[Layer];
    }
    };
    myData.load("example1.txt");


    //Check to hide or show the previous and next buttons depending on data
    function hideButtons()
    {
    //If the current Layer is not the first one, not the last one
    if(Layer > 0 && Layer < Sentences.length - 1)
    {
    //Then show both buttons
    but_prev._visible = true;
    but_next._visible = true;
    }
    //Or if the current Layer is the last one
    else if(Layer > 0 && Layer >= Sentences.length - 1)
    {
    //Hide the previous button, show the next button
    but_prev._visible = true;
    but_next._visible = false;
    }
    //Otherwise,
    else
    {
    //Show the next button and hide the previous button
    but_prev._visible = false;
    but_next._visible = true;
    }

    }
    hideButtons();


    //BUTTONS

    //Next button
    but_next.onPress = function()
    {
    //Increase the Layer number each time the Next button is pressed
    Layer++;

    //Call hideButtons
    hideButtons();

    //Go to the next frame
    //nextFrame();

    //Display the Layer number in the mainText textfield
    mainText.text = "Layer "+Layer;

    //Display the corresponding text from the sentences array in the
    //myText_txt textfield
    myText_txt.text = Sentences[Layer];
    }

    //Previous button
    but_prev.onPress = function()
    {
    //Increase the Layer number each time the Next button is pressed
    Layer--;

    //Call hideButtons
    hideButtons();

    //Go to the next frame
    //prevFrame();

    //Display the Layer number in the mainText textfield
    mainText.text = "Layer "+Layer;

    //Display the corresponding text from the sentences array in the
    //myText_txt textfield
    myText_txt.text = Sentences[Layer];
    }

    And the text file:
    content0=Text for layer 0
    &content1=Another text for layer 1
    &content2=Yeah, other text for layer 2
    &content3=And text for layer 3
    &content4=Last text for layer 4
    &content5=adksadjksajds
    Already mastering AS3. It was so fun. Now into Javascript and PHP and all its libraries

  7. #7
    Member
    Join Date
    Mar 2014
    Posts
    32
    Thank You so much! You are the best!

  8. #8
    Member
    Join Date
    Mar 2014
    Posts
    32
    Thank You so much, the code is fantastic and works just fine. But there is one small detail, that I wanted for this, and it maybe my fault for not explaining and formulating it well, but ill try again.

    So the idea is that the number of sentences in text file = the number of layers in swf file WITHOUT typing the exacty number in code of layers that will be needed to display the text from files. So far it is this var Quantity:Number = 6; - code line that is needed to be changed accordingly to the number of sentences in the text file, but, for example, if these swf and text files are used by non-programming skilled users, they would change the sentence quantity from 6 to 7, but only 6 will show.

    Im sorry if I have been not clear enough You have helped me a LOT and I really appreciate that.
    I hope im making a point of what I actually would like..and I hope it is possible at all. Thank You.

  9. #9
    Designer, Programmer, Musician angelhdz's Avatar
    Join Date
    Mar 2010
    Posts
    971
    Yeah. I imagined that you eventually would need and ask for that. Let me change it:

    ///////////////////IMPORTANT///////////////////////////////////
    ///You have to insert the corresponding frame for each Layer///
    ///////////////////////////////////////////////////////////////

    //VARIABLES
    var Sentences:Array = [];
    var Layer = 0;

    //INIT
    stop();
    mainText.text = "Layer "+Layer;


    //FUNCTIONS

    //Load sentences from text file
    myData = new LoadVars();
    myData.onLoad = function(Success:Boolean)
    {
    if(Success)
    {
    //Loop through all the sentences in the text file
    //and push/insert them into the Sentences array/list
    var i:Number = 0
    while(this["content"+i] != undefined)
    {
    Sentences.push(this["content"+i]);
    i++;
    }

    //Display the corresponding text in myText_txt textfield
    //from the sentences array
    myText_txt.text = Sentences[Layer];
    }
    };
    myData.load("example1.txt");


    //Check to hide or show the previous and next buttons depending on data
    function hideButtons()
    {
    //If the current Layer is not the first one, not the last one
    if(Layer > 0 && Layer < Sentences.length - 1)
    {
    //Then show both buttons
    but_prev._visible = true;
    but_next._visible = true;
    }
    //Or if the current Layer is the last one
    else if(Layer > 0 && Layer >= Sentences.length - 1)
    {
    //Hide the previous button, show the next button
    but_prev._visible = true;
    but_next._visible = false;
    }
    //Otherwise,
    else
    {
    //Show the next button and hide the previous button
    but_prev._visible = false;
    but_next._visible = true;
    }

    }
    hideButtons();


    //BUTTONS

    //Next button
    but_next.onPress = function()
    {
    //Increase the Layer number each time the Next button is pressed
    Layer++;

    //Call hideButtons
    hideButtons();

    //Go to the next frame
    nextFrame();

    //Display the Layer number in the mainText textfield
    mainText.text = "Layer "+Layer;

    //Display the corresponding text from the sentences array in the
    //myText_txt textfield
    myText_txt.text = Sentences[Layer];
    }

    //Previous button
    but_prev.onPress = function()
    {
    //Increase the Layer number each time the Next button is pressed
    Layer--;

    //Call hideButtons
    hideButtons();

    //Go to the next frame
    prevFrame();

    //Display the Layer number in the mainText textfield
    mainText.text = "Layer "+Layer;

    //Display the corresponding text from the sentences array in the
    //myText_txt textfield
    myText_txt.text = Sentences[Layer];
    }

    I removed var Quantity:Number = 6, and replaced the for loop with a while, so it will begins loading data from the textfile endlessly until the current index var value is undefined (until in the textfile doesn't exist a variable with the current index number) then it will stop and hide the next button. It work as you want now, only be sure to have the same amount of frames for each sentence. Try adding new sentences in the textfile and test the movie. íMagic!
    Already mastering AS3. It was so fun. Now into Javascript and PHP and all its libraries

  10. #10
    Member
    Join Date
    Mar 2014
    Posts
    32
    Great great code, works amazing! Thank You once again! I tried it and works great for one dynamic text field, but when I add another text file and another dynamic text field, the text from the second text file is not showing in the second dynamic text field. I assume, either im not placing the code in the right place (under the code for first dynamic field in the same actionscript frame), or there are problems with layers(?). I will add the files I came up with :

    example1.flaexample1.txtexample2.txt



  11. #11
    . fruitbeard's Avatar
    Join Date
    Oct 2011
    Posts
    1,785
    Hi,

    I was passing by and a little bit bored so I took a look,
    PHP Code:
    stop();

    var 
    Sentences1:Array = [];
    var 
    Sentences2:Array = [];
    var 
    Layer:Number 0;
    var 
    total:Number;

    mainText.text "Layer " Layer;

    myData1 = new LoadVars();
    myData1.onLoad = function(Success:Boolean)
    {
        if (
    Success)
        {
            var 
    i:Number 0;
            while (
    this["content" i] != undefined)
            {
                
    Sentences1.push(this["content" i]);
                
    i++;
            }
            
    myText_txt.text Sentences1[Layer];
            
    myData2.load("example2.txt");
            
    total Sentences1.length 1;
        }
    };
    myData1.load("example1.txt");

    myData2 = new LoadVars();
    myData2.onLoad = function(Success:Boolean)
    {
        if (
    Success)
        {
            var 
    i:Number 0;
            while (
    this["content" i] != undefined)
            {
                
    Sentences2.push(this["content" i]);
                
    i++;
            }
            
    SecondText_txt.text Sentences2[Layer];
        }
    };

    function 
    hideButtons()
    {
        if (
    Layer && Layer total)
        {
            
    but_prev._visible true;
            
    but_next._visible true;
        }
        else if (
    Layer && Layer >= total)
        {
            
    but_prev._visible true;
            
    but_next._visible false;
        }
        else
        {
            
    but_prev._visible false;
            
    but_next._visible true;
        }
    }

    hideButtons();

    but_next.onPress = function()
    {
        
    Layer++;
        
    hideButtons();
        
    nextFrame();
        
    mainText.text "Layer " Layer;
        
    myText_txt.text Sentences1[Layer];
        
    SecondText_txt.text Sentences2[Layer];
    };

    but_prev.onPress = function()
    {
        
    Layer--;
        
    hideButtons();
        
    prevFrame();
        
    mainText.text "Layer " Layer;
        
    myText_txt.text Sentences1[Layer];
        
    SecondText_txt.text Sentences2[Layer];
    }; 
    Last edited by fruitbeard; 03-26-2014 at 07:11 AM.

  12. #12
    Member
    Join Date
    Mar 2014
    Posts
    32
    Thank You, its perfect )

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center