Iura Rusu
Iura Rusu

Reputation: 95

as3 getting object coordinates

Hello everyone so i have a piece of code witch creates some circles, and after i move them with another function i want to get their center coordinates so i can draw lines from center to center of circles, but i don`t have any idea how to do it ... if you can suggest me 1 , here is the code witch creates the circle :

function new_sond(event:MouseEvent):void
{
    if (i<9)
    {
    i++;
    q=i;
    var btn:Sprite = new Sprite();  
    btn.graphics.beginFill(0x0099FF, 1);
    btn.graphics.drawCircle(400, 300, 15);
    btn.graphics.endFill();
    var s:String = String(q);
    btn.name=s; 
    var textField = new TextField();
    textField.mouseEnabled=false;
    textField.text = i;
    textField.width = 10; 
    textField.height = 17;
    textField.x = 395; // center it horizontally
    textField.y = 292; // center it vertically
    btn.addChild(textField);
    this.addChild(btn);
    }
}

the code with is mooving them is :

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();
}

And the code where i draw the lines between them :

function click1(e:MouseEvent):void{
    e.currentTarget.removeEventListener(MouseEvent.CLICK, click1);
    var i:int;
    i=1;
    if (e.target.name!=null){
    trace(e.target.name);
    sx=mouseX;
    sy=mouseY;
    stage.addEventListener(MouseEvent.CLICK,click2);
    }
}

function click2(e:MouseEvent):void{
    e.currentTarget.removeEventListener(MouseEvent.CLICK, click2);
    fx=mouseX;
    fy=mouseY;
    var i:int;
    i=2;
    trace(e.target.name);
    var  line:Shape = new Shape();
    line.graphics.lineStyle(1,0x0066FF,1);
    line.graphics.moveTo(sx,sy);
    line.graphics.lineTo(fx,fy);
    this.addChild(line);
    var inputField:TextField = new TextField();
    inputField.border = true;
    inputField.type = TextFieldType.INPUT;
    str=inputField.text;
    trace(str);
    inputField.width = 23;
    inputField.height = 18;
    inputField.x = (sx+fx)/2;
    inputField.y = (sy+fy)/2;
    addChild(inputField);
}

The thing is i want to draw the line from center to center, but i get the mouseX and mouseY coordinates to draw, because i don`t know how to take the center coordinates of an object.... what i get is : http://gyazo.com/6003630d549209ec5e16ccfffe0ee689 But i want the lines to be drawn from center, if someone has any suggestions please help

Sorry for the long post, i just don`t know where i need to put the piece with will center them so i wanted to give the hole code where it can be placed.... I will appreciate very much any idea .

Upvotes: 0

Views: 935

Answers (1)

Jeff Ward
Jeff Ward

Reputation: 19026

Well, if you drew the circle at 0,0 and moved the btn object .x and .y to 400,300 like this:

btn.graphics.drawCircle(0,0,15);
btn.x = 400;
btn.y = 300;

Then as you drag btn around the screen, btn.x , btn.x (or in the click handler, e.target.x and e.target.y) would always be the center of the circle.

Alternately, if you can't or don't want to do it that way, you can get the bounds of btn (with respect to this coordinate system, since that's where line is being drawn), and since it's a circle, then center of the bounds will be the center of the circle:

var btn:Sprite = e.target;
var bounds:Rectangle = btn.getBounds(this);
var center_x:Number = bounds.x + bounds.width/2;
var center_y:Number = bounds.y + bounds.height/2;

Upvotes: 1

Related Questions