AS3文本框更改事件不启动 [英] AS3 Textbox Change Event Not Firing

查看:184
本文介绍了AS3文本框更改事件不启动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我建立了一个漫画问题泡泡的问答游戏。气泡的大小与问题的长度有关。我想在动态文本框上进行更改事件来调用更改问题气泡大小的函数。



但是,当我的文本框值从代码动态修改。

  question_txt.addEventListener(Event.CHANGE,setTextBubbleSize); 

函数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;
}

我想使用更改事件,因为我的代码中有几个地方可以设置question_txt。如何让文本框触发更改事件?



另外,有没有办法计算question_txt中的行数,以更准确地设置textBubble_mc的高度?是的,正如David所说, TextField 只会调用CHANGE事件它正在被用户输入编辑 - 实际上,大多数组件通常都是这样。原因当然是因为如果您手动尝试更改更改处理程序中的字段值,则会生成一个递归循环的事件,您必须手动检测出您的出路。



你想做的是做一个包装器功能,如:

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

对于您的另一个问题,没有理由估计文本框的高度行数或字符数 - 只需设置文本属性,您可以立即查询文本框的高度。它将准确反映该领域的新高度,如果更改字段的宽度,高度将立即更新等。



或者,如果您需要知道任何罚款有关文本字段大小的详细信息,您可以随时使用 TextExtent


I built a quiz game with a cartoon question bubble. The bubble is re sized to the length of the question. I want to a change event on the dynamic textbox to call a function that changes the size of the question bubble.

However, the change event is never called when my textbox value is modified dynamically 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 the change event because there are several places in my code that question_txt can be set. How can I get the textbox to fire the 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?

解决方案

Yes, as David comments, TextField only dispatches CHANGE events when it's being edited by user input - in fact, this is generally true of most components. The reason, of course, is because if you manually tried to change the field's value inside your "change" handler, you'd make a recursive cycle of events that you'd have to manually detect your way out of.

What you want to do is make a wrapper function, like:

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

For your other question, there is no reason to estimate the textfield's height based on the number of lines or characters - just set the text property, and you can immediately query the textfield's height. It will accurately reflect the field's new height, and if you change the field's width, the height will immediately be updated, etc.

Or, if you need to know any fine details about the size of the text field, you can always use TextExtent.

这篇关于AS3文本框更改事件不启动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆