Skip to main content
Inspiring
April 17, 2023
Answered

TextFormat parameters not applying until 2nd call

  • April 17, 2023
  • 1 reply
  • 857 views

I'm finding a sample label I add doesn't apply the assigned text formatting until the 2nd time it's called. The variables in this function aren't used anywhere else. I'm working on drawing a pie chart upon arrival at frame2. Before I get to the actual pie logic, I'm just testing the basic drawing parameters. The variables it uses are defined at the top of the AS3 for frame2:

 

var pieChart:Shape = new Shape;
var pieLabel_1:TextField = new TextField()
var tf:TextFormat = new TextFormat();

 

Then used in this stub function:

 

function draw_pie():void{
// Pie chart elements

trace("draw pie chart");
// Pie graphic logic TBD, place holder graphic
pieChart.graphics.beginFill(0xFFaaFF); // fill color
pieChart.alpha=.7; // temp alpha to see position relative to other graphics
pieChart.graphics.drawCircle(1660,545, 140); // (x upper left, y upper left, radius)
pieChart.graphics.endFill(); // end the fill

addChild(pieChart); // adds the circle to the stage


// label parameters

tf.font = "Arial";
tf.size = 56;
tf.color = 0x00FF00;
pieLabel_1.setTextFormat(tf);
pieLabel_1.text = "Label 1";
pieLabel_1.x = 1660;
pieLabel_1.y = 545;
addChild(pieLabel_1);
}

 

But the test label is intially drawn with the default format until an interaction on that same frame causes it to update, (at this point, just redraw). Is a flush or other refresh function needed before the setTextFormat function takes effect? The text format assignments in other components take effect without a refresh. Is an added action needed before a child can be added to the stage with the assigned format?

    This topic has been closed for replies.
    Correct answer JoãoCésar17023019

    defaultTextFormat is a property. So you should use it like this:

    pieLabel_1.defaultTextFormat = tf;

     

    1 reply

    JoãoCésar17023019
    Community Expert
    Community Expert
    April 18, 2023

    Hi.

     

    Use the defaultTextFormat property instead.

     

    Regards,

    JC

    hclmed0Author
    Inspiring
    April 18, 2023

    For a text object created with:

    var myText:TextField = new TextField()

    and a format created with:

    var tf:TextFormat = new TextFormat();

    While myText.setTextFormat(tf); compiles, when I try myText.defaultTextFormat(tf); I get the error:

    Attempted access of inaccessible method defaultTextFormat through a reference with static type flash.text:TextField.

    Is there some other way to assign this?

    JoãoCésar17023019
    Community Expert
    JoãoCésar17023019Community ExpertCorrect answer
    Community Expert
    April 18, 2023

    defaultTextFormat is a property. So you should use it like this:

    pieLabel_1.defaultTextFormat = tf;