AS3:构建自定义类

发布于 2024-12-04 16:22:56 字数 3687 浏览 0 评论 0原文

我编写了一个 ANDGatter.as 类,它有 4 个子对象“AndIONode0-3”。参见代码。我可以使用拖放功能,但添加事件侦听器的顺序似乎是错误的。我应该在 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);
    }    
}

}

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 zwischen \n"+startconnector.name+" und "+endconnector.name);
    }    
}

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

清浅ˋ旧时光 2024-12-11 16:22:56

在dragDropHandler()中,您引用startconnector.name。在调用 mouseMoveHandler() 之前不会分配 startconnector,只有当您将鼠标移到 AndIONode0 上时才会发生这种情况。您可能需要调整要添加移动侦听器的 AndIONode,以确保 startconnector 始终具有值。

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.

水水月牙 2024-12-11 16:22:56

以下语句失败,

if (event.dragSource.hasFormat('start')) {

导致 endconnector

endconnector=dropTarget;

永远不会成为 dropTarget,它基本上是一个 null 对象。
空对象不会有属性。
所以

Alert.show("Verbindung hergestellt zwischen \n"+startconnector.name+" und "+endconnector.name);

会因为请求空对象上的属性而出错

endconnector.name

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文