Posted By

chrisaiv on 10/13/08


Tagged

TextField as3 SimpleButton


Versions (?)

Who likes this?

8 people have marked this snippet as a favorite

THEPWN3R
vip4lyf
akashaman
tspitzr
SteveRB511
mediapoint
elkraneo
Josueriv


AS3: Creating a SimpleButton with Dynamic Text


 / Published in: ActionScript 3
 

I wanted to include a dynamic TextField within SimpleButton and found that the process was different compared to how MovieClip works.

  1. var bgRed:Shape = new Shape()
  2. bgRed.graphics.beginFill( 0xFF0000 );
  3. bgRed.graphics.drawRect( 0, 0, 200, 30 );
  4. bgRed.graphics.endFill();
  5.  
  6. var bgBlack:Sprite = new Sprite();
  7. bgBlack.graphics.beginFill( 0x000000 );
  8. bgBlack.graphics.drawRect( 0, 0, 200, 30 );
  9. bgBlack.graphics.endFill();
  10.  
  11. var tf:TextFormat = new TextFormat();
  12. tf.color = 0xFFFFFF;
  13. tf.font = "Verdana";
  14. tf.size = 17;
  15. tf.align = "center";
  16.  
  17. var txt:TextField = new TextField();
  18. txt.text = "Snipplr Rocks!";
  19. txt.x = 0;
  20. txt.y = 0;
  21. txt.width = bgRed.width;
  22. txt.height = bgRed.height;
  23. txt.setTextFormat( tf );
  24.  
  25. var mc:MovieClip = new MovieClip();
  26. mc.addChild( bgRed );
  27. mc.addChild( txt );
  28.  
  29. var btn:SimpleButton = new SimpleButton();
  30. btn.upState = mc;
  31. btn.overState = bgBlack;
  32. btn.downState = btn.upState;
  33. btn.hitTestState = btn.upState;
  34. btn.x = stage.stageWidth / 2 - btn.width;
  35. btn.y = stage.stageHeight / 2 - btn.height;
  36.  
  37. addChild( btn );

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: vdubplate on February 16, 2011

When I attach an Event listener to this and a function it doesn't want to behave as a button and won't work. This is what I have tried testing with:

bgRed.addEventListener(MouseEvent.CLICK, onClicked1); function onClicked1(event):void {var url:String = "http://yahoo.com"; var request:URLRequest = new URLRequest(url); navigateToURL(request, '_blank');}

bgBlack.addEventListener(MouseEvent.CLICK, onClicked2); function onClicked3(event):void {var url:String = "http://yahoo.com"; var request:URLRequest = new URLRequest(url); navigateToURL(request, '_blank');}

You need to login to post a comment.