Displaying text in the scene

Text 3D objects are represented by the TextField3D class, from the away3d.primitives package. Creating a text 3D object requires two steps:

  1. Extracting the fonts that were embedded inside a separate SWF file.
  2. Creating a new TextField3D object.

Let's create an application called FontDemo that creates a 3D textfield and adds it to the scene.

package
{

We import the TextField3D class, making it available within our application.

  import away3d.primitives.TextField3D;

The VectorText class will be used to extract the fonts from the embedded SWF file.

  import wumedia.vector.VectorText;

  public class FontDemo extends Away3DTemplate
  {

The Fonts.SWF file was created by compiling the Fonts class above. We want to embed this SWF file as raw data, so we specify the MIME type to be application/octet-stream.

  [Embed(source="Fonts.swf", mimeType="application/octet-stream")]
    protected var Fonts:Class;

    public function FontDemo()
    {
      super();
    }

    protected override function initEngine():void
    {
      super.initEngine();

Before any TextField3D objects can be created we need to extract the fonts from the embedded SWF file. This is done by calling the static extractFonts() function in the VectorText class, and passing a new instance of the embedded SWF file. Because we specified the MIME type of the embedded file to be application/octet-stream, a new instance of the class is created as a ByteArray.

      VectorText.extractFont(new Fonts());
    }

    protected override function initScene():void
    {
      super.initScene();
      this.camera.z = 0;

Here we create the new instance of the TextField3D class. The first parameter is the font name, which corresponds to the font name included in the embedded SWF file. The TextField3D constructor also takes an init object, whose parameters are listed in the next table.

      var text:TextField3D = new TextField3D("Vera Sans",
        {
          text: "Away3D Essentials",
          align: VectorText.CENTER,
          z: 300
        }
      );
      scene.addChild(text);
    }
  }
}

The following table shows you the init object parameters accepted by the TextField3D constructor.

Parameter

Type

Default Value

Description

size

int

20

The font size in pixels.

leading

int

20

Determines the amount of space between lines in a paragraph.

letterSpacing

int

0

Determines the amount of space between each character.

text

String

""

The text to display.

width

int

500

The width of the drawing area. If the text is greater than this number then we start wrapping to the next line. To disable wrapping set the textWidth property to Number.POSITIVE_INFINITY.

align

String

"TL" or VectorText.TOP_LEFT

Defines the alignment of the text. The VectorText class defines a number of constants that can be assigned to the align property. These are TOP_LEFT_CENTER, TOP_LEFT, TOP_RIGHT, BOTTOM_LEFT, BOTTOM_LEFT_CENTER, BOTTOM_RIGHT, LEFT, LEFT_CENTER, RIGHT, TOP, BOTTOM, and CENTER.

When the application is run, the scene will contain a single 3D object that has been created to spell out the words "Away3D Essentials" and formatted using the supplied font. At this point, the text 3D object can be transformed and interacted with, just like other 3D object.

Displaying text in the scene
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.15.190