Question

I was wondering how to change text format to Arial or Futura in this code?

On the stage there is a dynamic text box named my_text but trying to change text from properties panel does not work.

Here is my code:

//SCROLLING SPEED
var scrolling_speed:int = 10;
//TEXT TO SCROLL
var text_to_scroll:String = "This is my text";


//establish the field
var my_text:TextField = new TextField();
//add the field to stage
addChild(my_text);
//set the text
my_text.text = text_to_scroll;
//set the x coord off right side of stage
my_text.x = stage.stageWidth;
//set y coord in middle of stage (about)
my_text.y = (stage.stageHeight/2)-(my_text.height/2);
//not selectable
my_text.selectable = false;
//no border
my_text.border = false;
//field scales with more text
my_text.autoSize = TextFieldAutoSize.LEFT;

//set a format
var my_text_format:TextFormat = new TextFormat();
//set the color to the hex
my_text_format.color = 0x000000;
//set the font size
my_text_format.size = 24;
//apply formatting
my_text.setTextFormat(my_text_format);

//add the listener to scroll
my_text.addEventListener(Event.ENTER_FRAME,move_text);

//scroll function
function move_text(myevent:Event):void {
    my_text.x-=scrolling_speed;
    if(my_text.x<(0-my_text.width)){
        my_text.x=stage.stageWidth;
    }
}
Was it helpful?

Solution

Talking about changing the font face, try using the following command:

my_text_format.font = "Arial";

or

my_text_format.font = "Futura";

Consider also using defaultTextFormat property of the TextField:

//apply formatting
my_text.defaultTextFormat = my_text_format;

When you are using the setTextFormat() method, you are only applying the changes to the text, which is already in the form.

Here is the full code for you (Arial example is taken):

//SCROLLING SPEED
var scrolling_speed:int = 10;
//TEXT TO SCROLL
var text_to_scroll:String = "This is my text";


//establish the field
var my_text:TextField = new TextField();
//add the field to stage
addChild(my_text);
//set the text
my_text.text = text_to_scroll;
//set the x coord off right side of stage
my_text.x = stage.stageWidth;
//set y coord in middle of stage (about)
my_text.y = (stage.stageHeight/2)-(my_text.height/2);
//not selectable
my_text.selectable = false;
//no border
my_text.border = false;
//field scales with more text
my_text.autoSize = TextFieldAutoSize.LEFT;

//set a format
var my_text_format:TextFormat = new TextFormat();
//set the color to the hex
my_text_format.color = 0x000000;
//set the font size
my_text_format.size = 24;
//set the font face
my_text_format.font = "Arial";
//apply formatting
my_text.defaultTextFormat = my_text_format;
my_text.setTextFormat(my_text_format);

//add the listener to scroll
my_text.addEventListener(Event.ENTER_FRAME,move_text);

//scroll function
function move_text(myevent:Event):void {
    my_text.x-=scrolling_speed;
    if(my_text.x<(0-my_text.width)){
        my_text.x=stage.stageWidth;
    }
}

More on setting fonts in ActionScript 3.0: ActionScript TextFormat.font values - Erik’s Brain

More on defaultTextFormat and setTextFormat(): DefaultTextFormat vs SetTextFormat

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top