在闪光灯中拖动一个点,并有线,连接其他点移动

时间:2009-12-26 00:52:24

标签: flash actionscript-3 flash-cs4

我在画布上显示了一组点(代码的关键位被拉出):

// Drop points based on x y coords
for (var i = 0; i < 50; i++) {
    itemPoint[i] = new mcDot();
    itemPoint[i].x = 500*Math.random();
    itemPoint[i].y = 500*Math.random();

    // Set up drag & drop
    initDragger(itemPoint[i]);
    itemPoint[i].buttonMode = true;

    addChild(itemPoint[i]);
}

然后我连接点 - 一个点可以有50个连接

// Draw connections
for (i = 0; i < 50; i++) {
         for (j = 0; j < 50; j++) {
        // Is there a connection in the matrix? 
        if (connectMatrix[i][j] > 0) {
            itemConnect[k] = new Shape();

            itemConnect[k].graphics.lineStyle(1, 0x000000);

           // Connect the line to the dots
            itemConnect[k].graphics.moveTo(itemPoint[i].x, itemPoint[i].y);
            itemConnect[k].graphics.lineTo(itemPoint[j].x, itemPoint[j].y);
            addChild(itemConnect[k++]);
        }
    }
}

我有点拖放工作:

/** Drag and drop functions */
function initDragger(mc:MovieClip):void {
    mc.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
    mc.addEventListener(MouseEvent.MOUSE_UP, mouseUpHandler);
}

function mouseDownHandler(e:MouseEvent):void {
    e.currentTarget.startDrag();
}
function mouseUpHandler(e:MouseEvent):void {
    e.currentTarget.stopDrag();
}

然而,当我移动一个点时,我真的被困在如何重绘线条上。也可能有许多线连接到任何单个点。我是否需要以某种方式注册哪些线连接到哪个点?如何基于此重新绘制线条?

由于

1 个答案:

答案 0 :(得分:2)

我尝试复制相同的功能。这就是我得到的:这是用于绘制点的Dot类。

package {
  import flash.display.Sprite;
  public class Dot extends Sprite {
    public static var counter:Number = 0;
    public var id:Number;
    public function Dot():void {
      this.graphics.beginFill(0);
      this.graphics.drawCircle(5,5,5);
      this.graphics.endFill();
      id = counter++;
    }
  }
}
这是Test class。
package {
  import flash.display.Sprite;
  import flash.events.Event;
  import flash.events.MouseEvent;
  import flash.display.Shape;

public class Test extends Sprite {

var num:Number = 5; 
var item:Array = new Array(num);
var connection:Array = new Array(num);

public function Test():void{
  initItem();
  initConnection();
}

private function initItem():void {
  for (var i:Number = 0; i<num; i++) {
    item[i]= new Dot();
    item[i].x = 500*Math.random();
    item[i].y = 350*Math.random();
    addChild(item[i]);
    initDragger(item[i]);
  }
}

private function initDragger(dot:Dot):void {
  dot.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
  dot.addEventListener(MouseEvent.MOUSE_UP, mouseUpHandler);
}

function mouseDownHandler(e:MouseEvent):void {
  e.currentTarget.startDrag();
  e.target.addEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);
}
function mouseUpHandler(e:MouseEvent):void {
  e.currentTarget.stopDrag();
  e.target.removeEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);
  redrawLines(e.target.id);
}
function redrawLines(i:Number):void {
  for (var j:Number = 0; j< num; j++) {
    if(connection[i][j] != null) {
      removeChild(connection[i][j]);
      connection[i][j] = new Shape();
      connection[i][j].graphics.lineStyle(1, 0x000000);
      connection[i][j].graphics.moveTo(item[i].x, item[i].y);
      connection[i][j].graphics.lineTo(item[j].x, item[j].y);
      addChild(connection[i][j]);
      connection[j][i] = connection[i][j];
    }
  }
}
function onMouseMove(e:MouseEvent):void {
  redrawLines(e.target.id);
}

private function initConnection():void {
  for (var i:Number = 0; i<num; i++) {
    connection[i] = new Array(num);
    for (var j:Number = 0; j<num; j++) {
      if (j != i) {
        if (connection[j] != undefined) {
          connection[i][j] = connection[j][i];
          trace("Duplicate");
        }
        else if (Math.random() > 0.5){
        connection[i][j] = new Shape();
        connection[i][j].graphics.lineStyle(1, 0x000000);
        connection[i][j].graphics.moveTo(item[i].x, item[i].y);
        connection[i][j].graphics.lineTo(item[j].x, item[j].y);
        addChild(connection[i][j]);
        }
        else {
          connection[i][j] = null;
        }
      }
      else {
        connection[i][j] = null;
      }
      trace("connecting " + i + " to " + j +" with " + connection[i][j]);
    }
  }
  trace(connection);
}

var num:Number = 5; var item:Array = new Array(num); var connection:Array = new Array(num); public function Test():void{ initItem(); initConnection(); } private function initItem():void { for (var i:Number = 0; i<num; i++) { item[i]= new Dot(); item[i].x = 500*Math.random(); item[i].y = 350*Math.random(); addChild(item[i]); initDragger(item[i]); } } private function initDragger(dot:Dot):void { dot.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler); dot.addEventListener(MouseEvent.MOUSE_UP, mouseUpHandler); } function mouseDownHandler(e:MouseEvent):void { e.currentTarget.startDrag(); e.target.addEventListener(MouseEvent.MOUSE_MOVE, onMouseMove); } function mouseUpHandler(e:MouseEvent):void { e.currentTarget.stopDrag(); e.target.removeEventListener(MouseEvent.MOUSE_MOVE, onMouseMove); redrawLines(e.target.id); } function redrawLines(i:Number):void { for (var j:Number = 0; j< num; j++) { if(connection[i][j] != null) { removeChild(connection[i][j]); connection[i][j] = new Shape(); connection[i][j].graphics.lineStyle(1, 0x000000); connection[i][j].graphics.moveTo(item[i].x, item[i].y); connection[i][j].graphics.lineTo(item[j].x, item[j].y); addChild(connection[i][j]); connection[j][i] = connection[i][j]; } } } function onMouseMove(e:MouseEvent):void { redrawLines(e.target.id); } private function initConnection():void { for (var i:Number = 0; i<num; i++) { connection[i] = new Array(num); for (var j:Number = 0; j<num; j++) { if (j != i) { if (connection[j] != undefined) { connection[i][j] = connection[j][i]; trace("Duplicate"); } else if (Math.random() > 0.5){ connection[i][j] = new Shape(); connection[i][j].graphics.lineStyle(1, 0x000000); connection[i][j].graphics.moveTo(item[i].x, item[i].y); connection[i][j].graphics.lineTo(item[j].x, item[j].y); addChild(connection[i][j]); } else { connection[i][j] = null; } } else { connection[i][j] = null; } trace("connecting " + i + " to " + j +" with " + connection[i][j]); } } trace(connection); }