AS3 text box change event without fail

I built a quiz with a cartoony question. The length of the bubble depends on the length of the question. I want the change event in a dynamic text field to call a function that changes the size of the question bubble.

However, a change event is never raised when the value of my text field is dynamically changed from code.

 question_txt.addEventListener(Event.CHANGE, setTextBubbleSize);

 function setTextBubbleSize(event:Event):void
    {
        trace("QUESTION TEXT CHANGED");
        textBubble_mc.height = 30 + question_txt.text.length * 1.2;
        if (textBubble_mc.height > 170) textBubble_mc.height = 170;
        question_txt.y = textBubble_mc.y - textBubble_mc.height / 6 + 10;
    }

I want to use a change event because there are several places in my code that can be asked in question_txt. How can I make a text box fire a change event?

Also, is there a way to count the number of lines in question_txt to more accurately set the height of textBubble_mc?

+3
3

, , TextField CHANGE - . , , , , , , .

, -, :

function setQuestionText( s:String ):void
    {
        question_txt.text = s;
        setTextBubbleSize();
    }

- text, height. , , ​​ ..

, - , TextExtent.

+5

, CHANGE , as3. :

dispatchEvent(new Event(Event.CHANGE, true));

numLines TextField

http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/flash/text/TextField.html#numLines

+7

As I see the solutions, this is another class that extends TextField. Code example:

package {
import flash.events.Event;
import flash.text.TextField;

public class MyTf extends TextField {
    public function MyTf() {
        super();
    }

    override public function set text(value:String):void
    {
        super.text = value;
        dispatchEvent(new Event(Event.CHANGE));
    }

}
}
0
source

Source: https://habr.com/ru/post/1710199/


All Articles