0
votes

Because I want to avoid repetitive code, and I'm using a lot of text formats, I created a CustomTextFormat class in Flex Builder.

Another class, called CustomInputBox.as is using this object to create a format:

package
{
    import flash.display.Sprite;
    import flash.text.TextField;
    import flash.text.TextFieldType;

    public class CustomInputBox extends Sprite
    {
        public function CustomInputBox(xLoc:int, yLoc:int, width:uint, height:uint, password:Boolean = false, text:String = "", font:String = "Arial", fontColor:uint = 0x000000, fontSize:uint = 18, fontBold:Boolean = false)
        {
            var inputBox:TextField = new TextField();
            inputBox.type = TextFieldType.INPUT;
            inputBox.mouseEnabled = true;
            inputBox.selectable = true;
            inputBox.multiline = false;
            inputBox.x = xLoc;
            inputBox.y = yLoc;
            inputBox.width = width;
            inputBox.height = height;
            inputBox.displayAsPassword = password;
            var format:CustomTextFormat = new CustomTextFormat();
            inputBox.defaultTextFormat = format;
            inputBox.text = text;
            addChild(inputBox);
        }
    }
}

The code of CustomTextFormat is as follows:

package
{
    import flash.display.Sprite;
    import flash.text.TextFormat;
    import flash.text.TextFormatAlign;

    public class CustomTextFormat extends Sprite
    {
        public function CustomTextFormat(font:String = "Arial", fontColor:uint = 0x000000, fontSize:uint = 18, fontBold:Boolean = false, fontAlign:String = TextFormatAlign.LEFT)
        {
            var format:TextFormat = new TextFormat();
            format.font = font;
            format.color = fontColor;
            format.size = fontSize;
            format.bold = fontBold;
            format.align = fontAlign;
        }
    }
}

Now, I'm getting error 1067 in the CustomInputBox.as file, it's a Dutch error unfortunately (any way to set flex errors to english?):

1067: Impliciete afgedwongen omzetting van een waarde van het type CustomTextFormat in een niet-gerelateerd type flash.text:TextFormat. CustomInputBox.as

It's difficult to translate, but hopefuly the error number and the code are enough to identify my problem. I'm new to Flash, and searched but couldn't find out what I am doing wrong.

Thanks in advance.

1

1 Answers

1
votes

Something's wacky here. If you want to assign your custom format like this:

var format:CustomTextFormat = new CustomTextFormat();
inputBox.defaultTextFormat = format;

Then CustomTextFormat needs to extend TextFormat, and the code in CustomTextFormat's constructor should be modifying the inherited TF properties. Alternately, if you want to leave CustomTextFormat extending Sprite, then you need to change CustomTextFormat's "format" property to be a public property, and change your assignment to something like:

var customFormat:CustomTextFormat = new CustomTextFormat();
inputBox.defaultTextFormat = customFormat.format;

Does that make sense? Right now you're trying to set the input's default text format to a class object that extends Sprite. And the inputBox doesn't know anything about CustomTextFormat's internal "format" property, which is both private and temporary.

(Incidentally none of this precisely explains the error message you're getting, but in my experience it's somewhat rare for Flash compiler errors to really tell you what's wrong... they tend to claim you're using classes illegally when all you did is leave out a semicolon. I tend not to trust the error messages too much.)