A Flash Developer Resource Site

Page 1 of 2 12 LastLast
Results 1 to 20 of 30

Thread: [RESOLVED] applying a text format retrieved using getTextFormat

  1. #1
    Senior Member
    Join Date
    Jan 2007
    Location
    Barcelona
    Posts
    389

    resolved [RESOLVED] applying a text format retrieved using getTextFormat

    Hi,

    I'm working on a new project where I want to animate some text that makes up a whole word and I want to apply random values to each of these, so I do not have a very clear idea of how to do it so that each part of the word maintains the same format since it's going to be randomly generated.

    I haven't found an example using getTextFormat that suits my needs when I want to apply the retrieved format on the next part.

    Can that be done? And How? I have tried tracing out:

    Code:
    trace("Format "+= ind.getTextFormat());
    but since it's not a property I get an error.

    This is a general idea of what I mean to do:

    Code:
    var fontsArr:Array = ["Times", "Calibri", "Aparajita", "Tahoma", "Trebuchet MS", "Verdana"];
    
    var select:Number = Math.round(Math.random()*fontsArr.length-1);
    
    var my_format:TextFormat = new TextFormat();
    if (select == 1) {
    	my_format.font = "Times";
    } else if (select == 2) {
    	my_format.font = "Calibri";
    } else if (select == 3) {
    	my_format.font = "Aparajita";
    } else if (select == 4) {
    	my_format.font = "Tahoma";
    } else if (select == 5) {
    	my_format.font = "Trebuchet MS";
    } else if (select == 6) {
    	my_format.font = "Verdana";
    }
    my_format.color = 0xFF0000;
    my_format.size = 24;
    var ind:TextField = this.createTextField("ind", this.getNextHighestDepth(), 10, 10, 300, 30);
    ind.text = "In...";
    ind.antiAliasType = "advanced";
    ind.border = false;
    ind.embedFonts = true;
    ind._x = 200;
    ind._y = 250;
    ind._rotation = Math.round (Math.random ()*90)-45;
    ind._alpha = 100;
    ind.setTextFormat(my_format);
    
    ind.getTextFormat();
    and then apply this text format to the next syllable.

  2. #2
    . fruitbeard's Avatar
    Join Date
    Oct 2011
    Posts
    1,780
    HI Cap,

    You should get it to work first, then add rotation and embedding etc etc.

    Instead of using all the if else statements, try doing it in one
    PHP Code:
    var my_format:TextFormat = new TextFormat();
    my_format.font fontsArr[select];
    trace(fontsArr[select]); 
    I think it might be useful for you to start getting used to using zero as your start point with the arrays as it can get quite mixed up after a while, or when there is lots of code usage.

    you need to comment out the embed fonts line for a moment, to use the embed I'm pretty sure you will neeed to have fonts in the library with that array linkage names, maybe you have, but you didn't attach your *.fla, never mind.

    and comment out the rotation line

    at the bottom try using this too
    PHP Code:
    ind.setTextFormat(my_format);
    trace(my_format.font); 
    so at the moment we have
    PHP Code:
    var fontsArr:Array = ["Times""Calibri""Aparajita""Tahoma""Trebuchet MS""Verdana"];

    var 
    select:Number Math.round(Math.random() * fontsArr.length 1);

    var 
    my_format:TextFormat = new TextFormat();
    my_format.font fontsArr[select];
    trace(fontsArr[select]);

    my_format.color 0x000000;
    my_format.size 24;

    var 
    ind:TextField this.createTextField("ind"this.getNextHighestDepth(), 101030030);

    ind.text "Hello World";
    ind.antiAliasType "advanced";
    ind.border false;
    //ind.embedFonts = true;
    ind._x 200;
    ind._y 250;
    //ind._rotation = Math.round(Math.random() * 90) - 45;
    ind._alpha 100;

    ind.setTextFormat(my_format);
    trace(my_format.font); 

  3. #3
    Senior Member
    Join Date
    Jan 2007
    Location
    Barcelona
    Posts
    389
    Good morning, FruitBeard, thanks for the quick reply,

    I am attaching my fla this time. Please take a look at it.

    I do have fonts in the library linked for the embedding. I'm wondering how to apply this same code once I need to use it again for the next syllable.

  4. #4
    . fruitbeard's Avatar
    Join Date
    Oct 2011
    Posts
    1,780
    Hi Cap,

    Went to the shops, back now,

    Anyway, if I think this is what you want to achieve, there may be a better way to do it or at least tidy up the code, try this
    PHP Code:
    var aFontsArr:Array = ["Times""Calibri""Aparajita""Tahoma""Trebuchet MS""Verdana"];
    var 
    aFontSize:Array = ["14""26""38""47""59""72"];
    var 
    aColor:Array = ["0xFF0000""0x003300""0xFFFFFF""0x000033""0x99CC00""0x6666CC"];
    var 
    aBoldness:Array = ["true""false""true""true""false""false"];
    var 
    aItalic:Array = ["true""false""true""true""false""false"];

    function 
    textFormatter(ab)
    {
        var 
    select:Number Math.floor(Math.random() * aFontsArr.length 1);
        var 
    tFormatter:TextFormat = new TextFormat();
        
    tFormatter.font aFontsArr[select];
        
    tFormatter.size aFontSize[select];
        
    tFormatter.color aColor[select];
        
    tFormatter.bold aBoldness[select];
        
    tFormatter.italic aItalic[select];
        
    ind.setTextFormat(a,b,tFormatter);
        
    //////////////////////////////////////////
        
    var my_fmt:TextFormat ind.getTextFormat(ab);
        for (var 
    prop in my_fmt)
        {
            
    trace(prop ": " my_fmt[prop]);
        }
        
        
    trace("----------------------");
        
    //////////////////////////////////////////
    }

    var 
    ind:TextField this.createTextField("ind"this.getNextHighestDepth(), 1010500500);
    ind.text "Independencia";
    ind.antiAliasType "advanced";
    ind.border false;
    ind.embedFonts true;
    ind._x 200;
    //Math.floor(Math.random()*Stage.width-200);
    ind._y 200;
    //Math.floor(Math.random()*Stage.height);
    ind._rotation Math.floor(Math.random() * 90) - 45;
    ind._alpha 100;
    textFormatter(0,3);
    textFormatter(3,9);
    textFormatter(9,ind.text.length); 
    Last edited by fruitbeard; 04-04-2014 at 07:37 AM.

  5. #5
    Senior Member
    Join Date
    Jan 2007
    Location
    Barcelona
    Posts
    389
    Hi, Fruit,

    thanks for the example. I know you are guessing what I'm trying to do, but say I'm meaning to add "world" after "Hello" to this code (I can't understand the code you're posting - it's very complicated for me[I'll study it though]):

    Code:
    var aFontsArr:Array = ["Times", "Calibri", "Aparajita", "Tahoma", "Trebuchet MS", "Verdana"];
    var aFontSize:Array = ["14", "26", "38", "47", "59", "72"];
    var aColor:Array = ["0xFF0000", "0x003300", "0xFFFFFF", "0x000033", "0x99CC00", "0x6666CC"];
    var aBoldness:Array = ["true", "false", "true", "true", "false", "false"];
    var aItalic:Array = ["true", "false", "true", "true", "false", "false"];
    
    
    var select:Number = Math.round(Math.random() * aFontsArr.length); 
    
    var tFormatter:TextFormat = new TextFormat(); 
    tFormatter.font = aFontsArr[select]; 
    tFormatter.size = aFontSize[select];
    tFormatter.color = aColor[select];
    tFormatter.bold = aBoldness[select];
    tFormatter.italic = aItalic[select];
    
    trace(aFontsArr[select]);
    trace(aFontSize[select]);
    trace(aColor[select]);
    trace(aBoldness[select]);
    trace(aItalic[select]);
    var Hello:TextField = this.createTextField("Hello", this.getNextHighestDepth(), 10, 10, 500, 500);
    Hello.text = "Hello";
    Hello.antiAliasType = "advanced";
    Hello.embedFonts = true;
    Hello._x = 200;
    //Math.floor(Math.random()*Stage.width-200);
    Hello._y = 200;
    //Math.floor(Math.random()*Stage.height-250);
    Hello._rotation = Math.floor(Math.random()*90)-45;
    Hello._alpha = Math.floor(Math.random()*100)-10;
    Hello.setTextFormat(tFormatter);
    
    
    Hello.getTextFormat();
    I either want to use a setInterval function or practice with sound que points to trigger the text creation. I can't think of which until I get this straight. Before I get to that, I would like to know how to use the getTextFormat() method. Also; I can't place the text randomly inside a given dimension. You can see I've tried placing it 200 pixels less than the stage's width or 250 pixels less than the stage's height. Sometimes I never get to see the text. What am I doing wrong there?

  6. #6
    . fruitbeard's Avatar
    Join Date
    Oct 2011
    Posts
    1,780
    Hi Cap,

    You must use *var select:Number = Math.round(Math.random() * aFontsArr.length - 1);*
    and not *var select:Number = Math.round(Math.random() * aFontsArr.length );* or you will end up with a 6 sometimes which is not part of the array,
    the array consists of 0 - 5

    Perhaps you never saw my edit of the last post, but it uses the getTextFormat()
    Last edited by fruitbeard; 04-04-2014 at 08:30 AM.

  7. #7
    Senior Member
    Join Date
    Jan 2007
    Location
    Barcelona
    Posts
    389
    hi, thanks, yes, yes. I just realized a couple of new things for me in it. Thanks...

    I better write tomorrow again.

  8. #8
    . fruitbeard's Avatar
    Join Date
    Oct 2011
    Posts
    1,780
    Hi Cap,

    For the _x, _ y, you should put the conditions in brackets like so
    PHP Code:
    Hello._x Math.floor(Math.random() * (Stage.width 200));
    Hello._y Math.floor(Math.random() * (Stage.height 250));
    trace(Hello._x);
    trace(Hello._y); 
    instead of
    PHP Code:
    Hello._x Math.floor(Math.random() * Stage.width 200);
    Hello._y Math.floor(Math.random() * Stage.height 250); 
    sometimes you don't see the text as it's colour is too close to the background colour

  9. #9
    Senior Member
    Join Date
    Jan 2007
    Location
    Barcelona
    Posts
    389
    Hi, Fruit, thanks again,

    thank you so much for the previous code, I'm starting to understand it; except the a and b values in the textFormatter function()...

    I've changed somethings a bit trying to execute in on a serInterval and the italic doesn't show even if I set it to true. Can you see why not? I guess bold won't work either. I really can't tell...

    Code:
    function createText():Void{
    
    var aFontsArr:Array = ["Times", "Calibri", "Aparajita", "Tahoma", "Trebuchet MS", "Verdana"];
    var aFontSize:Array = ["14", "26", "38", "42", "48", "54"];
    var aColor:Array = ["0xFF0000", "0x003300", "0xFFFFFF", "0x000033", "0x99CC00", "0x6666CC"];
    var aBoldness:Array = ["true", "false", "true", "true", "false", "false"];
    var aItalic:Array = ["true", "true", "true", "true", "true", "true"];
    
    function textFormatter()
    {
        var select:Number = Math.floor(Math.random() * aFontsArr.length - 1);
    	var select1:Number = Math.floor(Math.random() * aFontSize.length - 1);
    	var select2:Number = Math.floor(Math.random() * aColor.length - 1);
    	var select3:Number = Math.floor(Math.random() * aBoldness.length - 1);
    	var select4:Number = Math.floor(Math.random() * aItalic.length - 1);
    	
        var tFormatter:TextFormat = new TextFormat();
        tFormatter.font = aFontsArr[select];
        tFormatter.size = aFontSize[select1];
        tFormatter.color = aColor[select2];
        tFormatter.bold = aBoldness[select3];
        tFormatter.italic = aItalic[select4];
        ind.setTextFormat(tFormatter);
    }
    
    var ind:TextField = this.createTextField("ind", this.getNextHighestDepth(), 100, 100, 800, 500);
    ind.text = "In... inde...independència";
    ind.antiAliasType = "advanced";
    ind.embedFonts = true;
    ind._x = Math.floor(Math.random() * Stage.width); 
    ind._y = Math.floor(Math.random() * Stage.height);
    
    trace("ind X = "+ind._x);
    trace("ind Y = "+ind._y);
    ind._rotation = Math.floor(Math.random() * 90) - 45;
    ind._alpha = Math.floor(Math.random() * 200) - 10;
    textFormatter(0,6);
    textFormatter(6, 13);
    textFormatter(13,ind.text.length); 
    }
    intervalId = setInterval(this, "createText", 1000);

  10. #10
    . fruitbeard's Avatar
    Join Date
    Oct 2011
    Posts
    1,780
    Hi Cap,

    well you didn't use the textFormatter() properly, well at least not the a,b parts

    The a and b is equal to the text part start and text part end length accordingly.
    so *function textFormatter(a,b)*

    a , b are being used as the coordinates you sent from calling the function

    so callling
    textFormatter(0,6) = ind.text characters from 0 - 6
    textFormatter(6,12) = ind.text characters from 6 -12

    a = 0, b = 6
    function textFormatter(a=0,b=6);

    it just makes it easier for you to type things inside the function code rather than remembering numbers all the time.

    You are also using nested functions which makes it impossible to call one function without calling the other first.

    You keep moving the goal posts Cap, I'm not sure what you want anymore, this time you are making endless amounts of the text, where as before you wanted one that added words to the original text field??

    You only need use 100 for the alpha as alpha only goes to 100
    Last edited by fruitbeard; 04-04-2014 at 11:33 AM.

  11. #11
    Senior Member
    Join Date
    Jan 2007
    Location
    Barcelona
    Posts
    389
    right, sorry. I'm rushing again...

    I pasted the alpha and didn't even look at it.

    I'll keep working... thanks, see you

  12. #12
    Senior Member
    Join Date
    Jan 2007
    Location
    Barcelona
    Posts
    389
    Hi,

    I see all I need to do now is simply pass no parameters if I want all the text keeping the same format... thanks.

    But, I've been starting over once and again, and I can't get the italic showing even if it traces true. I can't even get it to show if I simply assign the text format to true; I suppose, like I said in my other post if this happens as well with the boldness, it's hard to tell...

    I already deleted the file I attached before and have overwritten my file several times but I would swear the italics and the bold worked with the if statements...

    what could thee problem be?

  13. #13
    . fruitbeard's Avatar
    Join Date
    Oct 2011
    Posts
    1,780
    Hi

    So do you want multiple textfields or just the one?

    The italic and bold will not show unless you embed a separate font using italic and bold in the library.

    One way to deal with that is to put empty non selectable textfields off the screen, for each
    Last edited by fruitbeard; 04-04-2014 at 02:41 PM.

  14. #14
    . fruitbeard's Avatar
    Join Date
    Oct 2011
    Posts
    1,780
    You have to embed the bold and italic fonts too (and Bold Italic).
    According to my font list, Tahoma only has reg and bold as options, probably many other fonts have limited choices too
    Last edited by fruitbeard; 04-04-2014 at 03:16 PM.

  15. #15
    Senior Member
    Join Date
    Jan 2007
    Location
    Barcelona
    Posts
    389
    ashamed... thanks

  16. #16
    Senior Member
    Join Date
    Jan 2007
    Location
    Barcelona
    Posts
    389
    I really need to practice my English as well!

    you can achieve that, with the same font and the same text?

    I've tried after reading your second post and it's always italicized and bold. Do you mean adding the same font with the same font name but marking the italic and bold attributes? It will not let me because of the same font names.




    With all due respect, what do you mean by the first post?

    I have just tried putting empty non selectable fields with instance name "ind", non selectable, italic and bold + embedding it again but I can't make it happen.

    I deselected everything again so I could fit the file here. Can you please do one for me?

  17. #17
    . fruitbeard's Avatar
    Join Date
    Oct 2011
    Posts
    1,780
    Hi Cap,

    I see somebody else got the file to look at too, I bet I know who.

    Here is a file for you to look at, it has the embedded fonts it the library, sometimes a part of the text does not show as it is trying to display Tahoma in Bold Italic for example, which does not exist.

    However it does work if you don't embed the fonts using code, but looks a little edgy.

    Mess around with it, I think it is what you wanted???

    Maricon - the file you attached last post is what we started off with, nevermind.

  18. #18
    Senior Member
    Join Date
    Jan 2007
    Location
    Barcelona
    Posts
    389
    Grazie. Ma, perché mi dici maricon. Io non sono un maricon. Sono un conyo madre, un vero conyo madre.

    I'll get it to work back in Flash 8, thanks

  19. #19
    . fruitbeard's Avatar
    Join Date
    Oct 2011
    Posts
    1,780
    Hi Cap,

    I didn't even notice that there, was an error, apologies if offended.

  20. #20
    Designer, Programmer, Musician angelhdz's Avatar
    Join Date
    Mar 2010
    Posts
    971
    Hi Cap,

    I see somebody else got the file to look at too, I bet I know who.
    You are an idiot fruibeard. That comment was not neccesary AT ALL here. Everyone here has the right to download , read , reply, any thing posted here. And if you are guessing and betting about who downloaded the file, let me tell you i wasn't. I'm not even keeping and eye on this thread, i just open each new thread, I see if I can be useful, otherwise i close it and keep going. You are the one who are keeping and eye always on me, pretending that i'm here downloading all your files and focusing in you, and competing, and don't negate now that you were reffering to me. I'm not stupid.

    Capdetrons, i hope all your queries and doubts are cleared off , and i wish you success in your projects. Have a nice day
    Already mastering AS3. It was so fun. Now into Javascript and PHP and all its libraries

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