简体   繁体   English

AS3中的TextField-对点击侦听器进行编程

[英]TextField in AS3 - programming a click listener

I want to add a simple piece of text to the stage and add a listener to do something when the user clicks it. 我想在舞台上添加一段简单的文本,并在用户单击它时添加一个侦听器以执行某些操作。

Here's my TextLink class: 这是我的TextLink类:

package some.package
{
    import flash.display.Sprite;
    import flash.external.ExternalInterface;
    import flash.text.TextField;
    import flash.text.TextFieldAutoSize;

    public class TextLink extends Sprite
    {
        public var tf:TextField = new TextField();
        public var bspr:Sprite = new Sprite();

        public function TextLink(tx:int, ty:int, tft:String):void
        {   
            tf.text = tft;
            tf.x = tx;
            tf.y = ty;
            tf.autoSize = TextFieldAutoSize.LEFT;
            bspr.addChild(tf);
            this.addChild(tf);
        }
    }
}

And here is the way I am calling it, along with the listener: 这就是我与侦听器一起调用的方式:

public function test_array_of_objects():void
{     
 var tmp:TextLink = new TextLink(30, 30, "some text");
 tmp.addEventListener(MouseEvent.CLICK, roverNotify); 
 addChild(tmp);       
}

protected function roverNotify(e:Event):void
{
    ExternalInterface.call("console.log", "got a click");
}

...But I don't get a message for some reason. ...但是由于某种原因,我没有收到任何消息。 I've imported everything successfully. 我已经成功导入了所有内容。 Any thoughts on what else I can try? 关于我还能尝试什么的任何想法?

函数TextLink在开始时是否需要这样的东西:var tf:Text = new Text();

Is your TextLink class an event dispatcher? 您的TextLink类是事件调度程序吗? You're trying to add a listener to the TextLink object, but the click listener needs to be attached to the text field that you're using inside TextLink. 您试图将侦听器添加到TextLink对象,但是单击侦听器需要附加到在TextLink内部使用的文本字段。 TextLink needs to be a DisplayObject of some kind to inherit the dispatching capabilities. TextLink必须是某种DisplayObject才能继承分派功能。

Also, constructors should not specify a return type (since they're just returning themselves) -- the :void should not be there where your TextLink constructor is. 另外,构造函数不应该指定返回类型(因为它们只是返回自己)-:void不应位于TextLink构造函数所在的位置。

Is the problem with clicking the Sprite or getting the event to fire? 单击Sprite或触发事件是否有问题? If it's the former you could try adding the code below. 如果是前者,则可以尝试添加以下代码。

tmp.mouseChildren = false;
tmp.buttonMode = true;
ExternalInterface.call("console.log", "got a click");

You have a JavaScript function defined like this??: 您具有这样定义的JavaScript函数??:

function console.log(inputString) {
    //do something
}

Edit: Nevermind the above, forgot about Firebug. 编辑:没关系以上,忘了Firebug。

Also, TextLink doesn't need to be an event dispatcher, though you may want to have TextLink set its mouseChildre n property to false (unless you need to be able to select that text), so that you don't inadvertently trigger events on the TextField , and buttonMode to true. 另外,尽管您可能希望TextLink将其mouseChildre n属性设置为false(除非您需要能够选择该文本),但TextLink不必是事件调度程序,这样您就不会在无意间触发事件。将TextFieldbuttonMode设置为true。

Edit: Also, what's the point of?: 编辑:另外,这有什么意义?:

var bspr:Sprite = new Sprite();
bspr.addChild(tf);

Final edit 最终编辑

How about this? 这个怎么样? http://code.google.com/p/fbug/issues/detail?id=1494 http://code.google.com/p/fbug/issues/detail?id=1494

Yes, you are correct, in FF3 the console is injected only when the page has javascript and uses window.console. 是的,您是正确的,在FF3中,仅当页面具有javascript并使用window.console时,才会注入控制台。

If you put any js that accesses the console before the Flash loads it should work, eg 如果您在Flash加载之前放置了任何可以访问控制台的js,那么它应该可以工作,例如

<script>
    var triggerFirebugConsole = window.console;
</script>

Let us know if this works. 让我们知道这是否有效。 It's unlikely that we can fix this soon. 我们不太可能很快解决此问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM