Skip to main content
Inspiring
May 17, 2013
Question

textfield with padding

  • May 17, 2013
  • 1 reply
  • 2315 views

I want to create a textfield with a coloured background BUT which some space around the text inside it, so the text doesn't start against the edges of it's background color. I can only textformat the left and right margin, so I found this:

var myFormat:TextFormat = new TextFormat;

myFormat.leftMargin = 40;

myFormat.rightMargin = 40;

var myText:TextField = new TextField;

myText.defaultTextFormat = myFormat;

myText.autoSize = TextFieldAutoSize.CENTER;

myText.y=40

var mySpr:Sprite = new Sprite();

mySpr.graphics.beginFill(0xeeaaaa,1);

mySpr.graphics.drawRect(0,0, myText.width,myText.height+80);

mySpr.graphics.endFill();

addChild(mySpr);

mySpr.addChild(myText);

This uses a coloured Sprite and inside it a textfield which has a space around of of 40 pixels. This textfield is still empty. That's because I've got this complete movieclip inside the Library. I add it through:

var clippy:textfieldpadding = new textfieldpadding()

addChild (clippy)

And what I want to do is place it like this, but than add some text to it.

var clippy:textfieldpadding = new textfieldpadding()

addChild (clippy)

clippy.myText.text = "fake text"

This doesn't work (error #1010). What does?

This topic has been closed for replies.

1 reply

Inspiring
May 17, 2013

Did you name the textfield inside the clippy sprite "myText" ( not "mytext", not "MyText", not "Mytext")?

If yes, enable debugging to get the exact line where the error occurs

(btw: It s a useful convention to name classes with a leading Capital)

jiggy1965Author
Inspiring
May 17, 2013

The textfield is actionscript generated. So the library item doesn't have any object on stage, just the actionscript.

I placed that library movieclip on stage. So now the coloured sprite is visible with an empty textfield on top. Then I want to target that empty - actionscript generated - textfield which I named myText as you can see from the first code, and add text toe it. But now I get this error with the clippy.myText.text = 'fake text' line: TypeError: Error #1010: A term is undefined and has no properties.  I've also tried clippy.mySpr.myText.text, but that also gave an error.

How can I target a textfield?

Inspiring
May 17, 2013

So the library item doesn't have any object on stage, just the actionscript.

You can`t reference an object that is not even created in a class.

You have to wait for it to be created.

This can be done by adding a Event.ADDED_TO_STAGE listener to the textfield, and only calling

clippy.myText.text = "fake text" after that event fires

addChild() alone is not enough to make sure the object is available