AS3 文本框更改事件未触发 [英] AS3 Textbox Change Event Not Firing

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

问题描述

我制作了一个带有卡通问题泡泡的问答游戏.气泡根据问题的长度重新调整大小.我想要动态文本框上的更改事件来调用更改问题气泡大小的函数.

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

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

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?

还有,有没有办法统计 question_txt 中的行数,以便更准确地设置 textBubble_mc 的高度?

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

推荐答案

是的,正如 David 评论的那样,TextField 仅在用户输入编辑时调度 CHANGE 事件 - 事实上,这通常是正确的大多数组件.原因当然是因为如果您在更改"处理程序中手动尝试更改字段的值,您将创建一个必须手动检测出路的递归事件循环.

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();
    }

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

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.

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

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天全站免登陆