在as3中多次使用1个对象?

时间:2012-08-23 08:38:36

标签: actionscript-3 flash

我正在尝试制作类似书签的东西,我在舞台上有1个音符,当用户点击它时,它开始拖动,用户将其放在他们想要的位置。问题是我希望多次拖动这些笔记..这是我的代码:

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);
}

任何帮助将不胜感激。

3 个答案:

答案 0 :(得分:0)

您需要在放下时创建笔记类的新实例,从拖动的笔记中复制位置和其他变量,将新笔记添加到舞台,然后将拖动笔记返回到其原始位置。

类似的东西:

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);
}

...这实际上是伪代码,因为我不知道您是否需要将新笔记添加到列表中,或将其链接到其原始笔记。如果你Google ActionScript Drag Drop Duplicate,你会发现更多的例子。

答案 1 :(得分:0)

我认为你不是目标拖动功能中的拖动对象和对象实例化中的问题

for (var i:Number = 1; i<numberOfNodes; i++) {

        note = new note();
        addChild(note);
        ...
        ....
    }

 function drag(e:MouseEvent):void{
        (e.target).startDrag();
    }

答案 2 :(得分:0)

如果你拖动多种类型的对象(例如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);
}
相关问题