开发者

TextField in AS3 - programming a click listener

开发者 https://www.devze.com 2022-12-14 07:33 出处:网络
I want to add a simple piece of text to the stage and add a listener to do something when the user clicks it.

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?


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.


Does function TextLink require something like this at the beginning: var tf:Text = new Text();


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 mouseChildren 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消