views:

384

answers:

1

Because I want to avoid repetive 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  A: 

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.)

fenomas
Thanks a lot. This will get me much further.My problem is that I don't really know what the extend is for, I'm just using Sprite by default. I really need to find that one out...
Tom
Second comment: after changing Extends to TextFormat, the format doesn't seem to do anything - it's the same as when no format is assigned.
Tom
Update: I think I got it working now. So by extending TF, you don't have to call the object again. Makes sense.
Tom
There really isn't a need here to extend TextFormat. Instead just create an instance of TextFormat and give it's properties the values you need.
Branden Hall
Tom: I'm glad you got it working, but I also agree with Branden that there's no real need to extend TextFormat. Now would be a good time to do some reading about what extend does, because it's the heart and soul of object-oriented programming!
fenomas