2013-08-17 141 views
0

我会对我怎样才能使一个线的问题,以捕捉到物体的中心,在我来说,我得出了一些圆圈时按下按钮,然后另一个按钮添加行,但我想它们捕捉到中心,因此,当我移动至圆的线延伸/压缩其中i创建圆的代码:AS3快线对象

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

其中i画线从圆中的代码来圈

function click1(e:MouseEvent):void{ 
    e.currentTarget.removeEventListener(MouseEvent.CLICK, click1); 
    if (e.target.name!=null){ 
    cntr=cntr+1; 
    trs=e.target.name; 
    var trn = Number(trs); 
    du[cntr]=trn; 
    sx=e.target.x+400; 
    sy=e.target.y+300; 
    stage.addEventListener(MouseEvent.CLICK,click2); 
    } 
} 




function click2(e:MouseEvent):void{ 
    e.currentTarget.removeEventListener(MouseEvent.CLICK, click2); 
    fx=e.target.x+400; 
    fy=e.target.y+300; 
    var i:int; 
    i=2; 
    trs=e.target.name; 
    var trn = Number(trs); 
    u[cntr]=trn; 
    var line:Shape = new Shape(); 
    line.graphics.lineStyle(1,0xFF0000,2); 
    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; 
    inputField.width = 23; 
    inputField.height = 18; 
    inputField.x = (sx+fx)/2; 
    inputField.y = (sy+fy)/2; 
    inputField.multiline = false; 
    inputField.maxChars = 3; 
    inputField.restrict = "0-9"; 
    inputField.addEventListener(Event.CHANGE, checkInput); 
    addChild(inputField); 
} 

很抱歉的长期职位,但我真的需要一些帮助,我只是不t know how to do it and have no ideas, it is a big project i米正在为模拟MaxFlow 它的外观:http://gyazo.com/d0478d967b4cc34b59490245b3530e26 我只是想知道是否有可能使线捕捉到的圈子的中心......

回答

1

你的第二个点击后(在你绘制你的最后指向并完成该行),您需要有一个遍历所有想要可能捕捉到的对象的步骤。

在上述步骤中,您可以计算并查看第2个绘图点是否在对象的抓取距离内,并将该绘图点设置为等于对象的x和y(应该是中心点)。如果不是的话,它会直接继续循环,直到所有可捕捉对象已经被彻底测试。