AS3: Structuring custom classes
I wrote a class ANDGatter.as which has 4 child objects "AndIONode0-3". See code. I got drag-drop functionality working but the order in which I add the eventlisteners seems to be wrong. Should I add listeners before or after the addChild methods ? I tried both ways, but when instanciating 2 ANDGatter in my Application I get a Null-pointer Exception during the first drag-drop action. How can I fix this ?
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 zw开发者_运维问答ischen \n"+startconnector.name+" und "+endconnector.name);
}
}
}
In dragDropHandler(), you're referencing startconnector.name. startconnector isn't assigned until mouseMoveHandler() is called, which only happens when you move your mouse over AndIONode0. You may need to adjust which AndIONodes you are adding your move listeners to in order to make sure that startconnector always has a value.
The following statement is failing
if (event.dragSource.hasFormat('start')) {
causing endconnector
endconnector=dropTarget;
to never be dropTarget which will basically be a null object.
Null objects will not have attributes.
So
Alert.show("Verbindung hergestellt zwischen \n"+startconnector.name+" und "+endconnector.name);
Will error out because of the request of an attribute on a null object
endconnector.name
精彩评论