views:

139

answers:

4

Hi there,

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?

+1  A: 

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

WayneH
yea; i have that defined already outside of the main constructor.
jml
+1  A: 

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.

wmid
re: textlink: it's a sprite; won't this work?re: void: i took that out. doesn't seem to matter either way... hm.thanks for the thoughts; still not werkin.
jml
btw- i updated the code in my post to show you the fuller picture
jml
Yes, Sprites dispatch events.
aib
EXCELLENT. thanks for the help, everyone.you all are great.... i'm always impressed by the level of support here. my issue turned out to be multiple things, but the most imp. was that i wasn't dispatching an event and latching onto it from the main runner.
jml
+1  A: 

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;
knuckfubuck
I can click on it, and then i get an error saying that i've passed in too many args (1). i have my callback defined with a single arg of the event (e).
jml
A: 
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.

Tegeril