AS3 Dynamic Speech Bubble Snippet

Actionscript

The static method drawSpeechBubble in the file below uses the drawing API to draw a rounded rectangle based speech bubble with a dynamic point. The side the point is on moves depending on the relative position to the bubble. It looks best if you set the point to be fairly close to the actual rectangle.

Usage:

AS:


SpeechBubble.drawSpeechBubble(target:Sprite, rect:Rectangle, cornerRadius:Number, point:Point)

// Example:
var g:Graphics = graphics;

var m:Matrix = new Matrix();
m.createGradientBox(200,100,90*Math.PI/180,80,80);
g.clear();
g.lineStyle(2,0x888888,1,true);
g.beginGradientFill(GradientType.LINEAR, [0xe0e0e0, 0xffffff], [1,1], [1,0xff],m);
SpeechBubble.drawSpeechBubble(this, new Rectangle(80,80,200,100), 20, new Point(120,300));
g.endFill();

Sample (move mouse to change point position):

Download:
SpeechBubble.as

Apologies for any code messiness! I'm aware that you can create a dent in the corner.. it's probably up to you to position the point sensibly so it doesn't screw up :)

This entry was posted on Monday, January 28th, 2008 at 11:25 pm and is filed under Actionscript. You can follow any responses to this entry through the RSS 2.0 feed. You can skip to the end and leave a response. Pinging is currently not allowed.

17 Responses to “AS3 Dynamic Speech Bubble Snippet”

  1. Angel Romero Says:

    Nice. Did you create this class yourself?

  2. Ash Says:

    Yes

  3. paranoio Says:

    :shock: just awesome

  4. Arran Maclean Says:

    :grin: Sweet effect.. Love simple ideas, that where genius starts.

  5. George Bones Says:

    Nice! And eerily similar to what something we're using on our site (soon to launch) :smile:

    I had a question about your as3 color component, it'd be great to email if you have a chance,

  6. Drewb Says:

    Is there an AS2 version of this class? D

  7. Ash Says:

    Drewb: Theres really nothing as3 specific in the code, it should be a snap to convert.

    Change the first line of the function to var g:MovieClip = target;
    Then change the function header (so target is a MovieClip), and package stuff.

  8. Dubai Web Design, Development Says:

    great example. I will use this one in future

  9. Stringycustard Says:

    I did something similar, but I tweened all the corners and the tag into new positions (it's "wobbly" in effect). I don't have the source anymore unfortunately, as I moved companies but I can redo it if anybody is interested. It was done in AS3 but I can code either...

  10. sid Says:

    Ash can you share the as2 code...

  11. BeechyBoy Says:

    When I try this out in Flash, it compiles with the following errors:
    5000: The class 'SpeechBubble' must subclass 'flash.display.MovieClip' since it is linked to a library symbol of that type.

  12. Ash Says:

    BeechyBoy: This class isn't designed to be an extension of MovieClip. You need to create a MovieClip on stage, and then pass it in to the static draw method, as in the example.

  13. BeechyBoy Says:

    Thank you for the swift reply Ash, I'm just having a mental block it seems :s I have a MovieClip on the stage and I want to put the SpeechBubble in it, or use the MovieClip to attach the pointers to... I'd really appreciate a final tip to get this clear in my head. Many thanks Ash. SpeechBubble.drawSpeechBubble(holder_mc, bubbleBody, 20, pointers);

  14. BeechyBoy Says:

    Got it to do what I needed it to do now, cheers :)

  15. mikeyq6 Says:

    Nice one. Was looking for a custom renderer for my Flex chart data tips, and this works perfectly. Had more trouble with Flex Docs posting incorrect code!

  16. RhythmOne Says:

    I am new to ActionScript (but not new to coding/programming). Can someone please enlighten me as to how you would use text with this bubble? Is there an element to put text into? or would you create a text obj in a layer that resides on top of the bubble.

  17. Ash Says:

    RhythmOne: This snippet draws the bubble shape only. You would have to create a component to create the text field, etc.

Leave a Reply