0

i am newbie to flash.i need to change the below actionscript code to actionscript 3.0 code. i am currently working on drag and drop. so i want to duplicate the movieclip while dragging i found the code on internet but it is actionscript 2.0 so please convert it to as3. the box is a instance name of a movieclip. the code blocks are:

var num:Number = 0

box.onPress = function(){

    num++


    duplicateMovieClip(box ,"box"+num, _root.getNextHighestDepth())

    _root["box"+num].startDrag();

}

box.onReleaseOutside = function(){

    trace(_root["box"+num])

    stopDrag();

}

2 Answers 2

1

If you dont want to use seperate .as file, follow this steps:

1- assign AS linkage to box movieClip (in library panel):

enter image description here

2- Select frame 1 on the timeline, and paste this code in the Actions panel:

var boxes:Array=[];
//var box:Box=new Box();
//addChild(box);
box.addEventListener(MouseEvent.MOUSE_DOWN,generateBox);
function generateBox(e:MouseEvent):void{
    var newBox:Box=new Box();
    newBox.x = e.target.x;
    newBox.y = e.target.y;
    newBox.startDrag();
    newBox.addEventListener(MouseEvent.MOUSE_UP,stopD);
    newBox.addEventListener(MouseEvent.MOUSE_DOWN,startD);
    boxes.push(newBox);
    addChild(newBox);
}
function startD(e:MouseEvent):void{
    e.target.startDrag();
}
function stopD(e:MouseEvent):void{
    e.target.stopDrag();
}
Sign up to request clarification or add additional context in comments.

1 Comment

thank you man i got what i want. but i want to drag the child boxes not to reproduce the child but just drag.
1

Unfortunately, there's no duplicateMovieClip analog in AS3, so you'll have to create a Class for your box movieClip template. Let's say it will be called BoxTemplate. (You can google how to create Classes for your library object). Add a Class with this name and add this code (event subscription in the constructor and a private event listener). You'll get something like this:

package
{
    public class BoxTemplate
    {
        public function BoxTemplate()
        {
            addEventListener(MouseEvent.MOUSE_UP, onMouseUp);
        }
    }

    private function onMouseUp(e:MouseEvent):void 
    {
        stopDrag();
    }
}

Leave your present instance of this symbol on the stage. This is your code in the frame:

import flash.event.MouseEvent

box.addEventListener(MouseEvent.CLICK, onClick);

function onClick(e:MouseEvent):void
{
    var newBox:BoxTemplate = new BoxTemplate();
    newBox.x = e.target.x;
    newBox.y = e.target.y;
    addChild(newBox);
    newBox.startDrag();
}

It will allow you to infinitely clone your boxes. Of course, you can add all of them in the array to keep the references.

5 Comments

thankyou kumokairo for your answer. but the code shows the error 1013: The private attribute may be used only on class property definitions. please help me to solve the error.
@user2622338 As I've said, you have to create a class BoxTemplate.as and put the top part of the code there. You're trying to paste it to the frame and that is why you get this error
i have created the BoxTemplate.as by class for library object myMC(movie clip name) and paste the first block of code there. i pasted the second block of code in frame. and still i get the error please help me to solve the error
@user2622338 please, upload your project files somewhere so I can take a closer look)
fileswap.com/dl/xgazggpWw i have uploaded my files please solve the error and let me know the correction

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.