I’m having a problem with a my TextFormat object in the Letter class I’ve created. Whenever I call a class function that alters the TextFormat object (formatText,sizeText,colorText), the next time that one of these functions is called on a different instance the common formatting is applied. I’ve read that you need to redefine objects or arrays within the class constructor in order to make them unique to instances. I think I’ve done here with this code but it is not working:
//Letter Constructor
function Letter() {
//This should redefine the text format object for this instance right????
var txt_fmt:TextFormat = new TextFormat();
}
Here is the entire class. It purpose is to create draggable text letters.
class Letter extends MovieClip {
//Library linkage definition
static var linkageClip:String = "Letter";
//Create text box for text
private var text_box:TextField;
//Create text format object
private var txt_fmt:TextFormat;
//Create variable to hold the letter text
private var letter_txt:String;
//Letter Constructor
function Letter() {
//This should redefine the text format object for this instance right????
var txt_fmt:TextFormat = new TextFormat();
//Create an initial text format
var initial_fmt:TextFormat = new TextFormat();
//Define initial format
initial_fmt.font = "Arial";
initial_fmt.align = "Center";
initial_fmt.color = 0x000000;
initial_fmt.bold = true;
initial_fmt.size = 18;
//Set txt_fmt equal to the initial text format
txt_fmt = initial_fmt;
}
//Creates a new instance of Letter
static function create(timeline:MovieClip, instance_name:String, x_pos:Number, y_pos:Number, depth:Number):Letter {
//Create the instance on the passed timeline
var instance = timeline.attachMovie(linkageClip, instance_name, depth);
//Position the clip on the stage
instance._x = x_pos;
instance._y = y_pos;
//Return reference to the Letter created
return instance;
}
//Function to add text to the instance
public function addText(txt:String, new_fmt:TextFormat):Void {
//Check for a passed TextFormat object
if(new_fmt != null){
txt_fmt = new_fmt;
}
//Set the letter_txt to txt
letter_txt = txt;
//Get the size of the passed text
var metrics = txt_fmt.getTextExtent(letter_txt);
//Create a text field inside the clip
this.createTextField("text_box",this.getNextHighestDepth(),0,0,metrics.textFieldWidth,metrics.textFieldHeight);
//Set the text
text_box.text = letter_txt;
text_box.setTextFormat(txt_fmt);
text_box.selectable = false;
text_box._highquality = 2;
}
//Function to change the text in the instance
public function changeText(new_txt:String):Void {
//Change letter_txt
letter_txt = new_txt;
//Get the size of the passed text
var metrics = txt_fmt.getTextExtent(letter_txt);
//Set the text
text_box.text = letter_txt;
text_box._width = metrics.textFieldWidth;
text_box._height = metrics.textFieldHeight;
text_box.setTextFormat(txt_fmt);
text_box.selectable = false;
text_box._highquality = 2;
}
public function formatText(new_fmt:TextFormat):Void {
//Change the text format
txt_fmt = new_fmt;
//Get the size of the new format
var metrics = txt_fmt.getTextExtent(letter_txt);
//Set the text
text_box.text = letter_txt;
text_box._width = metrics.textFieldWidth;
text_box._height = metrics.textFieldHeight;
text_box.setTextFormat(txt_fmt);
text_box.selectable = false;
text_box._highquality = 2;
}
//Function to re-color the text
public function colorText(new_color:Number):Void {
//Change the text format object
txt_fmt.color = new_color;
//Get the size of the new format
var metrics = txt_fmt.getTextExtent(letter_txt);
//Set the text
text_box.text = letter_txt;
text_box._width = metrics.textFieldWidth;
text_box._height = metrics.textFieldHeight;
text_box.setTextFormat(txt_fmt);
text_box.selectable = false;
text_box._highquality = 2;
//Set the text
text_box.setTextFormat(txt_fmt);
}
//Function to resize the text
public function sizeText(new_size:Number):Void {
//Change the text format object
txt_fmt.size = new_size;
var metrics = txt_fmt.getTextExtent(letter_txt);
//Set the text
text_box.text = letter_txt;
text_box._width = metrics.textFieldWidth;
text_box._height = metrics.textFieldHeight;
text_box.setTextFormat(txt_fmt);
text_box.selectable = false;
text_box._highquality = 2;
//Set the text
text_box.setTextFormat(txt_fmt);
}
//Function to return the current text size
public function getTextValue():String {
return text_box.text;
}
//Function to return the current text size
public function getSizeText():Number {
return txt_fmt.size;
}
//Function to get the whole text format
public function getTxtFormat():TextFormat {
return txt_fmt;
}
//Function to start dragging of object
public function drag(Left:Number,Top:Number,Right:Number,Bottom:Number):Void {
//Check to see if a bounding box was passed
if(Left != undefined && Top != undefined && Right != undefined && Bottom != undefined){
this.startDrag(false,Left,Top,Right,Bottom);
} else {
this.startDrag();
}
}
//Function to start dragging of object
public function drop():Void {
this.stopDrag();
}
}