简体   繁体   English

如何单击一个圆以显示围绕它的矩形? 使用Flash as3.0

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

I'm a newbie in flash. 我是新手。 I want 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. 在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. 当有人单击它时,请使用getBounds()方法获取一个Rectangle实例,该实例定义圆的边界矩形(以避免对其尺寸进行硬编码。)使用此Rectangle实例中的信息绘制一个矩形。

// 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. 我会把它留给您作为练习,以在用户在圆圈外单击时隐藏rect sprite。

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. 请注意,这只是这样做的一种方法,但是由于您对设置没有任何解释,也没有提供有关案例的详细信息,因此这是我想到的最简单的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM