简体   繁体   中英

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:

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? 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 needs to be a DisplayObject of some kind to inherit the dispatching capabilities.

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.

Is the problem with clicking the Sprite or getting the event to fire? 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??:

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

Edit: Nevermind the above, forgot about 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.

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

Yes, you are correct, in FF3 the console is injected only when the page has javascript and uses window.console.

If you put any js that accesses the console before the Flash loads it should work, eg

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

Let us know if this works. It's unlikely that we can fix this soon.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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