2012-08-23 5 views
0

북마크와 같은 것을 만들려고합니다. 무대에 1 개의 메모가 있으며 사용자가 클릭하면 끌기 시작하고 사용자가 원하는 곳에 놓습니다. 문제는이 노트를 여러 번 끌고 싶습니다. 여기 내 코드는 다음과 같습니다.as3에서 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); 
} 

어떤 도움을 받으실 수 있습니다.

+0

사용자가 원래의 음표를 삭제할 때 스테이지에 새 노트를 만들겠습니까 (드래그 된 사본과 중복)? – JcFx

+0

네가 정확히 원하는 걸 – Guram

+0

샘플 코드 중 일부에 오류가 있습니다. 'var note : notes = new notes; '-이 (가) 앞에 반드시 있어야합니다(). 나는 대답하려고 노력할 것이다. – JcFx

답변

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 인 경우 몇 가지 예가 더 있습니다.

+0

감사합니다. 귀하의 방법은 정상적으로 작동합니다. 지금 나는 새로운 사본을 지우는 데 어려움을 겪고있다. 그러나 그 자신을 고칠 것이다. – Guram

+0

도움이 된다니 기쁩니다. 배열의 각 새 노트에 대한 참조를 저장 한 다음 찾은 다음 삭제할 때 삭제하고 실제로 removeChild()를 사용하여 스테이지에서 제거하려고합니다. 각 음표에 고유 한 이름 ("copy_1", "copy_2"등)을 지정하면 나중에 쉽게 찾아서 제거 할 수 있습니다. – JcFx

0

난 당신이

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

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

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

당신이 객체의 주변에 여러 유형 (예. 참고 및 이미지) 드래그하는 경우, 당신이 뭔가를 할 수있는 객체 인스턴스화에서 드래그 드래그 기능의 객체와 문제를 대상으로하지 생각 이는 인스턴스화 할 객체의 유형을 하드 코딩하는 것이 아닙니다.

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); 
} 
관련 문제