我想在我的应用程序中实现拖放可能性。这是我的源代码:
我将图像添加到容器中,现在希望能够将它们从元素PieceContainer移动到元素板(在另一个类中定义)。我尝试定义鼠标移动处理程序,因为它显示在这里:http://livedocs.adobe.com/flex/3/html/help.html?content=dragdrop_7.html,但它没有做任何事情......实际上我不明白我应该如何定义drap启动器,并删除处理程序
public class PieceContainer extends Canvas
{
//private var image:Image = new Image();
private var piecesWhite:Dictionary;
private var piecesBlack:Dictionary;
private var images:ArrayCollection = new ArrayCollection();
private var placeX:Number;
public var items:Dictionary;
public function PieceContainer()
{
super();
this.addEventListener(Event.ADDED_TO_STAGE, displayPiece);
}
private function displayPiece(event:Event):void
{
placePiecesDict(new Point(0, 0), items);
}
private function placePiecesDict(start:Point, dict:Dictionary):void
{
placeX = 0;
for (var item:* in dict)
{
var image:Image = new Image();
image.source = dict[item];
image.x = placeX/1.4;
image.y = start.y;
image.addEventListener(MouseEvent.MOUSE_MOVE, doDrag);
images.addItem(image);
this.addChild(image);
placeX += height;
}
}
private function doDrag(evt:Event):void
{
trace(evt);
}
}
有关更多说明,请参阅附图: http://img.skitch.com/20091221-e6j8d62y9iin621hmcdbssrp6d.png
我用这种方式重新组织了代码:
public class PieceContainer extends Canvas
{
private var piecesWhite:Dictionary;
private var piecesBlack:Dictionary;
private var images:ArrayCollection = new ArrayCollection();
private var placeX:Number;
public var items:Dictionary;
public function PieceContainer()
{
super();
this.addEventListener(Event.ADDED_TO_STAGE, displayPiece);
}
private function displayPiece(event:Event):void
{
placePiecesDict(new Point(0, 0), items);
}
private function placePiecesDict(start:Point, dict:Dictionary):void
{
placeX = 0;
for (var item:* in dict)
{
var image:Image = new Image();
image.source = dict[item];
image.x = placeX/1.4;
image.y = start.y;
image.addEventListener(MouseEvent.MOUSE_MOVE, mouseMoveHandler);
images.addItem(image);
this.addChild(image);
placeX += height;
}
}
private function mouseMoveHandler(evt:MouseEvent):void
{
var dragInitiator:Image = Image(evt.currentTarget);
var ds:DragSource = new DragSource();
ds.addData(dragInitiator, "img");
DragManager.doDrag(dragInitiator, ds, evt);
}
并且还在棋盘上添加了拖放处理程序:
public class BoardCell extends Canvas
{
private var _color:Number;
public function BoardCell()
{
super();
this.addEventListener(DragEvent.DRAG_ENTER, dragEnterHanler);
this.addEventListener(DragEvent.DRAG_DROP, dragDropHandler);
}
public function placeMe(x:int, y:int, width:Number, color:Number):void
{
graphics.lineStyle(1, 0x000000);
graphics.beginFill(color);
graphics.drawRect(x, y, width, width);
graphics.endFill();
}
private function dragEnterHanler(event:DragEvent):void
{
if(event.dragSource.hasFormat("img"))
{
DragManager.acceptDragDrop(Canvas(event.currentTarget))
}
}
private function dragDropHandler(event:DragEvent):void
{
event.dragInitiator.x = 0;
event.dragInitiator.y = 0;
}
但实际上有两个问题:
首先:项目是可拖动的,但是当我将它们添加到板上时,它们会保留在PiecesContainer中。 第二:我希望复制项目,而不是移动(但是当他们在船上时,他们应该是可移动的,而不是复制的)
先谢谢
答案 0 :(得分:1)
第一个问题很容易解决。 dragMoveEnabled=true
是如何在列表控件中执行的,但由于您执行的操作非常自定义,因此您需要回顾拖动启动器并从中删除药品。详细信息:
http://livedocs.adobe.com/flex/3/html/help.html?content=dragdrop_8.html
这也可以帮助您设置拖放功能,以便您也可以解决问题2。