0

Everyone, I'm just a beginner and I have question. The idea of my program is that when the user clicks a button a circle is generated and it should be counting for each additional circle it should look something like this :

enter image description here

The number should be in center and I should be able to move the object with the number together this is my code:

add_s.addEventListener(MouseEvent.CLICK,new_sond);


function new_sond(event:MouseEvent):void
{
    var btn:Sprite = new Sprite();  
    btn.graphics.beginFill(0x00FF00, 1);
    btn.graphics.drawCircle(400, 300, 25);
    btn.graphics.endFill();
    this.addChild(btn);


}

//----------------------------Drag And Drop----------------------
this.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownH);
this.addEventListener(MouseEvent.MOUSE_UP, mouseUpH);

function mouseDownH(evt:MouseEvent):void {
    var object = evt.target;
    object.startDrag();
}

function mouseUpH(evt:MouseEvent):void {
    var obj = evt.target;
        obj.stopDrag();
} 

I will be very thankful if someone can help me. How do i make the text to apear on the center of the circles and i still will be able to move them with the text

4

3 回答 3

3

这就是 Vesper 应该发布的答案:)

TextField对象添加到您的Sprite. 由于TextField是 的子级Sprite,它将随 移动Sprite。您需要做的就是将文本放在圆圈的中间:

function new_sond(event:MouseEvent):void
{
    var btn:Sprite = new Sprite();  
    btn.graphics.beginFill(0x00FF00, 1);
    btn.graphics.drawCircle(400, 300, 25);
    btn.graphics.endFill();
    var textField = new TextField();
    textField.text = "1";
    textField.width = textField.textWidth; // default width is 100
    textField.height = textField.textHeight;
    textField.x = (25 - textField.textWidth)/2; // center it horizontally
    textField.y = (25 - textField.textHeight)/2; // center it vertically
    btn.addChild(textField);
    this.addChild(btn);
}

请注意,正如@Joeson Hwang 所建议的,您可以使用Shape对象而不是 aSprite来绘制圆圈。比Shapea 更轻量级Sprite。但是,由于Shape不扩展DisplayObjectContainer,您不能Shape像使用a 一样将子对象添加到 a 中Sprite

@Joeson Hwang 的回答建议将Shape和 文本添加到Sprite. 但这不会为您节省任何费用,因此只需Sprite像现在一样将图形直接绘制到 。

于 2013-07-30T16:27:05.780 回答
0

使用 btn:Shape(节省大量系统资源)。然后将形状和您的文本放入 Sprite。您还可以将 Sprite 添加到另一个 Sprite 中。

于 2013-07-30T16:05:19.583 回答
0

You can use also flash.text.engine package:

function new_sond(event:MouseEvent):void
{
    var btn:Sprite = new Sprite();  
    btn.graphics.beginFill(0x00FF00, 1);
    btn.graphics.drawCircle(cx, cy, cr);
    btn.graphics.endFill();

    // Font format
    var fontDescription:FontDescription = new FontDescription("Arial"); 
    var format:ElementFormat = new ElementFormat(fontDescription, 16, 0x000088);        

    // Display text
    var textElement:TextElement = new TextElement('1', format); 

    var textBlock:TextBlock = new TextBlock(); 
    textBlock.content = textElement; 

    // New display object containing text
    var textLine:TextLine = textBlock.createTextLine(null, 500); 

    // Centers the text inside the circle
    textLine.x = cx + (cr - textLine.width) / 2;
    textLine.y = cy + (cr - textLine.height) / 2;       

    // Add text into button
    btn.addChild(textLine);

    this.addChild(btn);
}

More info about creating text: Creating and displaying text in ActionScript 3.0 Developer’s Guide

于 2014-07-29T12:56:46.197 回答