我写了一个类ANDGatter.as,它有4个子对象“AndIONode0-3”。见代码。我有拖放功能,但我添加eventlisteners的顺序似乎是错误的。我应该在addChild方法之前或之后添加监听器吗?我尝试了两种方法,但是当我在我的应用程序中实现2 ANDGatter时,我在第一次拖放操作期间得到了一个空指针异常。我该如何解决这个问题?
package classes{
import classes.ConnectionLine;
import flash.display.DisplayObject;
import flash.events.MouseEvent;
import mx.containers.Canvas;
import mx.controls.Alert;
import mx.core.DragSource;
import mx.events.DragEvent;
import mx.managers.DragManager;
public class AndGatter extends Canvas
{
public var AndIONode0:Canvas;
public var AndIONode1:Canvas;
public var AndIONode2:Canvas;
public var AndIONode3:Canvas;
public var nodeWidth:int=10;
public var nodeHeight:int=10;
public var nodeColor:uint=0x919191;
public var gatterColor:uint=0x474747;
public var startconnector:DisplayObject;
public var endconnector:DisplayObject;
public function AndGatter()
{
super();
super.width=50;
super.height=50;
this.setStyle("backgroundColor",gatterColor);
/*EingangsNode0 für AndGatter*/
AndIONode0=new Canvas();
AndIONode0.name="And Output Node";
AndIONode0.width=nodeWidth;
AndIONode0.height=nodeHeight;
AndIONode0.x=40;
AndIONode0.y=20;
AndIONode0.setStyle("backgroundColor",nodeColor);
addChild(AndIONode0);
AndIONode1=new Canvas();
AndIONode1.name="And Input Node 1";
AndIONode1.width=nodeWidth;
AndIONode1.height=nodeHeight;
AndIONode1.x=0;
AndIONode1.y=5;
AndIONode1.setStyle("backgroundColor",nodeColor);
addChild(AndIONode1);
AndIONode2=new Canvas();
AndIONode2.name="And Input Node 2";
AndIONode2.width=nodeWidth;
AndIONode2.height=nodeHeight;
AndIONode2.x=0;
AndIONode2.y=20;
AndIONode2.setStyle("backgroundColor",nodeColor);
addChild(AndIONode2);
AndIONode3=new Canvas();
AndIONode3.name="And Input Node 3";
AndIONode3.width=nodeWidth;
AndIONode3.height=nodeHeight;
AndIONode3.x=0;
AndIONode3.y=35;
AndIONode3.setStyle("backgroundColor",nodeColor);
addChild(AndIONode3);
AndIONode0.addEventListener(MouseEvent.MOUSE_MOVE,mouseMoveHandler);
AndIONode1.addEventListener(DragEvent.DRAG_ENTER,dragEnterHandler);
AndIONode1.addEventListener(DragEvent.DRAG_DROP,dragDropHandler);
AndIONode2.addEventListener(DragEvent.DRAG_ENTER,dragEnterHandler);
AndIONode2.addEventListener(DragEvent.DRAG_DROP,dragDropHandler);
AndIONode3.addEventListener(DragEvent.DRAG_ENTER,dragEnterHandler);
AndIONode3.addEventListener(DragEvent.DRAG_DROP,dragDropHandler);
}
private function mouseMoveHandler(event:MouseEvent):void {
var dragInitiator:Canvas=Canvas(event.currentTarget);
startconnector=dragInitiator;
var ds:DragSource = new DragSource();
ds.addData(id, 'start');
DragManager.doDrag(dragInitiator, ds, event);
}
private function dragEnterHandler(event:DragEvent):void {
if (event.dragSource.hasFormat('start')) {
var dropTarget:Canvas=Canvas(event.currentTarget);
endconnector=dropTarget;
DragManager.acceptDragDrop(dropTarget);
}
}
private function dragDropHandler(event:DragEvent):void {
/*var connector:ConnectionLine = new ConnectionLine(startconnector,endconnector,true);
stage.rawChildren.addChild(connector);*/
Alert.show("Verbindung hergestellt zwischen \n"+startconnector.name+" und "+endconnector.name);
}
}
}
答案 0 :(得分:1)
在dragDropHandler()中,您引用了startconnector.name。在调用mouseMoveHandler()之前,不会分配startconnector,只有当您将鼠标移到Andionode0上时才会发生。您可能需要调整要添加移动侦听器的Andionodes,以确保startconnector始终具有值。
答案 1 :(得分:0)
以下声明失败
if (event.dragSource.hasFormat('start')) {
导致端连接器
endconnector=dropTarget;
永远不会是dropTarget,它基本上是一个空对象 空对象不具有属性 所以
Alert.show("Verbindung hergestellt zwischen \n"+startconnector.name+" und "+endconnector.name);
由于空对象上的属性请求而错误输出
endconnector.name