3

I started one week ago to study ActionScript 3.0. I would like to do a simple game. It will start with a window displaying a welcome message( "press the button to start"), and a arrow that start from the text and point to the button. I want to create everything from code. I'm using a TextField for the welcome message but I have some trouble. I created a file .fla AIR for Destkop. Then I associated to that file a class called mainFunzioneModidificaTest.as . In this class I wrote a function to set the text of the first window. I use the TextFormat but when I run the .fla file I see the text but without any formatting.The color, the dimension and the font dosen't change Here is the code. Can someone help me? Thank you!

package {
import flash.display.MovieClip;
import flash.text.TextField;
import flash.text.TextFormat;

public class mainFunzioneModificaTest extends MovieClip {

    public function mainFunzioneModificaTest() {
        setText();
    }
    function setText(): void {
        var text: TextField = new TextField();
        var myFormat: TextFormat = new TextFormat("Arial", 39, 0xFF0000);
        text.setTextFormat(myFormat);
        text.text = "Hello";
        addChild(text);
    }

}

}

SpaghettiFunk
  • 159
  • 1
  • 2
  • 13

1 Answers1

3

You need to set the text first before calling setTextFormat(), or alternatively use text.defaultTextFormat = myFormat;

From the TextFormat documentation:

Use the TextField.defaultTextFormat property to apply formatting BEFORE you add text to the TextField, and the setTextFormat() method to add formatting AFTER you add text to the TextField

Set text before calling setTextFormat():

function setText(): void {
    var text: TextField = new TextField();
    var myFormat: TextFormat = new TextFormat("Arial", 39, 0xFF0000);
    text.text = "Hello";
    text.setTextFormat(myFormat);
    addChild(text);
}

or set defaultTextFormat:

function setText(): void {
    var text: TextField = new TextField();
    var myFormat: TextFormat = new TextFormat("Arial", 39, 0xFF0000);
    text.defaultTextFormat = myFormat;
    text.text = "Hello";
    addChild(text);
}
Sly_cardinal
  • 12,270
  • 5
  • 49
  • 50
  • Thank you, now it's working! So everytime I have to set the text before apllying the "style" to that text? Now I have got another question. Can I add an addEventListener to a triangle drawn with the function drawTriangles() ? – SpaghettiFunk Mar 07 '17 at 11:50
  • Use `defaultTextFormat` to set text styles that should apply to all text by default. If you draw the triangle on a separate `Sprite` instance then you can add an event listener to that sprite. All paths drawn on the same DisplayObject will trigger the same event listeners, they are not independent. Use different Sprite instances for different interactive elements. – Sly_cardinal Mar 07 '17 at 11:54
  • And mark this answer as correct by clicking the check mark. – Neal Davis Mar 07 '17 at 16:33
  • I have created the arrow using a Sprite that point to the button but I still have a question.Can I animate the Sprite? I mean, I want that the arrow move up and down three times and then it stops for three seconds and then again, again and again. I just want to know if it is possible or not. Thank you! – SpaghettiFunk Mar 08 '17 at 10:10
  • Yes you can do that through code, but then you might as well make it a MovieClip with a timeline and animate it that way. – Sly_cardinal Mar 08 '17 at 23:26