在as3中多次使用一个对象



我正在尝试制作类似书签的东西,我在舞台上有一个笔记,当用户点击它时,它开始拖动,用户将它放在他们想要的地方。问题是我想把这些笔记拖多次。。这是我的代码:

import flash.events.MouseEvent;
//notess is the instance name of the movie clip on the stage
notess.inputText.visible = false;
//delet is a delete button inside the movie clip,
notess.delet.visible = false;
//the class of the object i want to drag
var note:notes = new notes  ;
notess.addEventListener(MouseEvent.CLICK , newNote);
function newNote(e:MouseEvent):void
{
    for (var i:Number = 1; i<10; i++)
    {

        addChild(note);
                //inpuText is a text field in notess movie clip
        note.inputText.visible = false;
        note.x = mouseX;
        note.y = mouseY;        
        note.addEventListener( MouseEvent.MOUSE_DOWN , drag);
        note.addEventListener( MouseEvent.MOUSE_UP , drop);
        note.delet.addEventListener( MouseEvent.CLICK , delet);
    }
}
function drag(e:MouseEvent):void
{
note.startDrag();
}

function drop(e:MouseEvent):void
{
    e.currentTarget.stopDrag();
    note.inputText.visible = true;
    note.delet.visible = true;
}
function delet(e:MouseEvent):void
{
    removeChild(note);
}

任何帮助都将不胜感激。

您需要在拖放时创建note类的新实例,从拖动的note中复制位置和其他变量,将新note添加到stage中,并将拖动的notes返回到其原始位置。

类似于:

function drop($e:MouseEvent):void
{
    $e.currentTarget.stopDrag();
    dropNote($e.currentTarget as Note);
}
var newNote:Note;
function dropNote($note:Note):void
{
    newNote = new Note();
    // Copy vars:
    newNote.x = $note.x;
    newNote.y = $note.y;
    // etc.
    // restore original note. 
    // You will need to store its original position before you begin dragging:
    $note.x = $note.originalX;
    $note.y = $note.orgiinalY;
    // etc.
    // Finally, add your new note to the stage:
    addChild(newNote);
}

这真的是伪代码,因为我不知道你是否需要将新注释添加到列表中,或者将其链接到原始注释。如果你谷歌ActionScript拖放复制,你会发现更多的例子。

我认为您不是针对拖动函数中的拖动对象和对象实例化中的问题

for (var i:Number = 1; i<numberOfNodes; i++) {
        note = new note();
        addChild(note);
        ...
        ....
    }
 function drag(e:MouseEvent):void{
        (e.target).startDrag();
    }

如果您正在拖动多种类型的对象(例如Notes和Images),您可以执行类似的操作,而不是对要实例化的对象类型进行硬编码。

function drop(e:MouseEvent):void{
   // Get a reference to the class of the dragged object
   var className:String = flash.utils.getQualifiedClassName(e.currentTarget);
   var TheClass:Class = flash.utils.getDefinitionByName(className) as Class;
   var scope:DisplayObjectContainer = this; // The Drop Target
   // Convert the position of the dragged clip to local coordinates
   var position:Point = scope.globalToLocal( DisplayObject(e.currentTarget).localToGlobal() );
   // Create a new instance of the dragged object
   var instance:DisplayObject = new TheClass();
   instance.x = position.x;
   instance.y = position.y;
   scope.addChild(instance);
}

最新更新