拖放形状

时间:2015-12-15 21:47:25

标签: actionscript-3

我正在构建一个包含几何几何对象的Flash应用。 那部分正在发挥作用。

用鼠标绘制矩形后,我想拖放它。 我怎样才能做到这一点?

var diktemporaryDrawing:Shape = new Shape();
addChild(diktemporaryDrawing);
diktemporaryDrawing.graphics.lineStyle(2, 0x666666, 1);

var dikmyDrawing:Shape = new Shape();
addChild(dikmyDrawing);
dikmyDrawing.graphics.lineStyle(2, 0x000000, 1);

var mouseHolding:Boolean = false;
var clickedX:Number;
var clickedY:Number;
stage.addEventListener(MouseEvent.MOUSE_DOWN, dikDown);
stage.addEventListener(MouseEvent.MOUSE_UP, dikUp);

function dikDown(MouseEvent):void {
    mouseHolding = true;
    clickedX = mouseX;
    clickedY = mouseY
}

function dikUp(MouseEvent):void {
    mouseHolding = false;
    //myDrawing.graphics.beginFill(0x222222, 0.5);
    dikmyDrawing.graphics.drawRect(clickedX, clickedY, mouseX-clickedX, mouseY-clickedY);
    //myDrawing.graphics.endFill();
    dikclearTemp()
}

stage.addEventListener(MouseEvent.MOUSE_MOVE, dikMove);

function dikMove(MouseEvent):void {
    if (mouseHolding) {
        dikclearTemp();
        diktemporaryDrawing.graphics.drawRect(clickedX, clickedY, mouseX-clickedX, mouseY-clickedY)
    }
}

function dikclearTemp():void {
    diktemporaryDrawing.graphics.clear();
    diktemporaryDrawing.graphics.lineStyle(2, 0x666666, 1)
}

2 个答案:

答案 0 :(得分:1)

首先,如果您需要将对象移动到某个位置,可以使用startDrag提到的shape.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown); shape.addEventListener(MouseEvent.MOUSE_UP, onMouseUp); 功能。

您的示例不移动对象,只是在指定位置绘制一条线。

在绘制一个像您在示例中所做的对象之后,可以将MouseEvent.MOUSE_DOWN和MouseEvent.MOUSE_UP附加到您绘制的形状上。

function onMouseDown(event:MouseEvent):void {
     shape.startDrag();
}

在onMouseDown处理程序中,您可以像

一样使用startDrag
onMouseDown

如果您不想使用startDrag函数,您还可以在onMouseMove函数中存储初始形状位置,并通过在for (var i:int = 0; i < 20; i++) { var sprite:Sprite = new Sprite(); sprite.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown); addChild(sprite); } function onMouseDown(event:MouseEvent):void { var clickedSprite:Sprite = event.target as Sprite; // or whatever movieclip you are adding trace("Selected " + clickedSprite); } 函数中取当前位置和起始位置之间的差异来移动。

编辑@TancotCossin:

假设您正在创建矩形(精灵或影片剪辑)并将它们添加到舞台中,如下所示。

Text Stuff = new Text("Do something here");
Stuff.setFill(Color.RED); //I am red now!
TextFlow.getChildren().add(Stuff);

答案 1 :(得分:0)

首先,&#34; Shape对象比包含相同图形的Sprite对象消耗更少的内存。但是, Sprite对象支持用户输入事件,而 Shape对象不支持。&#34;

其次,任何能够接收用户输入(即鼠标交互)的类必须是InteractiveObject类的后代,而Shape类不是。{3}}类。 (继承:Shape→DisplayObject→EventDispatcher→继承→对象)

最基本的互动课程是Sprite。它能够接收用户输入(为您的目的点击和鼠标移动)。因此,您必须为您绘制的每个矩形创建一个精灵,以便能够单独拖动它们。

基本上,有两种拖动方式:使用startDrag()stopDrag()方法(在Sprite类上定义),或者仅移动对象以匹配MOUSE_MOVE或ENTER_FRAME处理程序中的鼠标位置。我演示了后者,因为它没有多少改变你的代码。

import flash.geom.Point;
import flash.display.Sprite;

var diktemporaryDrawing:Shape = new Shape();
diktemporaryDrawing.graphics.lineStyle(2, 0x666666, 1);

var dikmyDrawing:Sprite = new Sprite();

addChild(dikmyDrawing);
addChild(diktemporaryDrawing); // drawing layer should be above 


var mouseHolding:Boolean = false;
var clickedX:Number;
var clickedY:Number;
var draggingRect:Sprite;

function dikDown(MouseEvent):void {  
  mouseHolding = true;

  var children:Array = dikmyDrawing.getObjectsUnderPoint(new Point(mouseX, mouseY));
  if (children.length > 0) {
    // looks like we clicked some rectangle,
    // starting to drag topmost child.
    draggingRect = children[children.length-1];
    // remember offset between rect center and mouse coordinates,
    // so we don't displace rectangle when dragging
    clickedX = mouseX-draggingRect.x;
    clickedY = mouseY-draggingRect.y;    
  } else {
    // we clicked empty space, starting to draw
    clickedX = mouseX;
    clickedY = mouseY
  }
}

function dikUp(MouseEvent):void {
  mouseHolding = false;

  if (draggingRect == null) {
    // end of drawing new rect
    var dx:Number = mouseX-clickedX;
    var dy:Number = mouseY-clickedY;
    if (Math.abs(dx) > 0 && Math.abs(dy) > 0) {
      // ***
      // new sprite for each rectangle
      var newRect:Sprite = new Sprite();
      newRect.x = clickedX;
      newRect.y = clickedY;
      newRect.graphics.lineStyle(2, 0x000000, 1);
      newRect.graphics.beginFill(0, 0);
      newRect.graphics.drawRect(0, 0, dx, dy);
      newRect.graphics.endFill();
      dikmyDrawing.addChild(newRect);  
      // ***    
    }
    dikclearTemp()
  } else {
    // end of dragging a rectangle
    draggingRect = null;
  }
}

function dikMove(MouseEvent):void {
  if (mouseHolding) {
    if (draggingRect == null) {
      // drawing
      dikclearTemp();
      diktemporaryDrawing.graphics.drawRect(clickedX, clickedY, mouseX-clickedX, mouseY-clickedY)
    } else {
      // dragging
      draggingRect.x = mouseX - clickedX;
      draggingRect.y = mouseY - clickedY;
    }
  }
}

function dikclearTemp():void {
  diktemporaryDrawing.graphics.clear();
  diktemporaryDrawing.graphics.lineStyle(2, 0x666666, 1)
}

stage.addEventListener(MouseEvent.MOUSE_DOWN, dikDown);
stage.addEventListener(MouseEvent.MOUSE_UP, dikUp);
stage.addEventListener(MouseEvent.MOUSE_MOVE, dikMove);