Drag, Drop and Clone - Flash AS3

假装没事ソ 提交于 2019-12-23 22:28:48

问题


I am a teacher trying to make a simple Flash AS3 game where students can drag any letter from a-z(movieclips) out to the middle of the stage to create words.

I am a total amateur with Flash and so using code snippets, I have been successful in allowing users to drag and drop letters but what I would really like is for users to be able to drag and drop a letter leaving the original letter movieclip in place and clone a new one as many times as is necessary.

Is anyone able to help me with the AS3 I would need to achieve this? Many thanks.


回答1:


Here is a quick sample. FLA | SWF

CODE:

import flash.display.MovieClip;

for (var i=1; i<5; i++)
{
    this["object" + i].addEventListener(MouseEvent.MOUSE_DOWN, onStart);
    this["object" + i].addEventListener(MouseEvent.MOUSE_UP, onStop);
}    

var sx = 0,sy = 0;

function onStart(e)
{
    sx = e.currentTarget.x;
    sy = e.currentTarget.y;
    e.currentTarget.startDrag();
}

function onStop(e)
{
    if (e.target.dropTarget != null && 
    e.target.dropTarget.parent == dest)
    {
        var objectClass:Class = 
        getDefinitionByName(getQualifiedClassName(e.currentTarget)) as Class;
        var copy:MovieClip = new objectClass();
        this.addChild(copy);
        copy.x = e.currentTarget.x;
        copy.y = e.currentTarget.y;
    }

    e.currentTarget.x = sx;
    e.currentTarget.y = sy;
    e.currentTarget.stopDrag();
}

Hoping you could take it ahead & turn into something useful for your kids.




回答2:


first, you can use a property for your letters called like "type" and "letter" for assuming is it original or a copy and which letter is it.

then initialize an empty object variable to use when clicked

var draggingObj:Object;

then you can check if user clicking on orginal letter to make a copy and move or clicking a copy just to move

if (Letter(clickedLetter).type == "original")
{
draggingObj = new Letter();
draggingObj.type = "copy";
draggingObj.letter = Letter(clickedLetter).letter;
}
else draggingObj = Letter(clickedLetter);

draggingObj.x = mousex;
draggingObj.y = mousey;

if you cannot implement this codeflow to your codeflow, you can write your own code here so we can help you properly



来源:https://stackoverflow.com/questions/21674358/drag-drop-and-clone-flash-as3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!