开发者

how to click a circle to show a rectangle surrounding it ?? using flash as3.0

I'm a newbie in flash. I wa开发者_运维技巧nt to click a circle to show a rectangle surrounding it,and click any other places in this stage to hide this rectangle,how to implement this event?

Thank you for your help!!!


Draw your circle in a Sprite. Programmatically this can be done in the following way:

var circle : Sprite = new Sprite();
circle.graphics.beginFill(0xffcc00);
circle.graphics.drawCircle(20, 20, 20); // center x, y and radius
addChild(circle);

When someone clicks it, use the getBounds() method to get a Rectangle instance that defines the bounding rectangle of the circle (to avoid hard-coding it's dimensions.) Draw a rectangle using the information from this Rectangle instance.

// Create an empty Sprite into which we will draw our rectangle
var rect : Sprite = new Sprite();

circle.addEventListener(MouseEvent.CLICK, handleCircleClick);
function handleCircleClick(ev : MouseEvent) : void {
    var bounds : Rectangle;

    // Draw rectangular graphics into the rect sprite
    bounds = circle.getBounds(circle.parent);
    rect.graphics.beginFill(0x00ff00);
    rect.graphics.drawRect(bounds.left, bounds.top, bounds.width, bounds.height);

    // Add rect to stage, below circle
    circle.parent.addChildAt(rect, 0);
}

I'll leave it to you as an exercise to hide the rect sprite when the user clicks outside the circle.

Note that this is just one way of doing it, but since you explain nothing about your set-up and give no details about your case this is the simplest method that comes to mind.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜