Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6566831
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T14:15:47+00:00 2026-05-25T14:15:47+00:00

I’m developing a drag’n’clone feature for my flash app (AS3 and Flash CS5). The

  • 0

I’m developing a drag’n’clone feature for my flash app (AS3 and Flash CS5). The clones are created perfectly, but when I try to drag the clone recently created, the app creates a new clone (and allow me to drag it).

I want to remove this behaviour: clone only should be drag and dropped, not cloned. My code is:

public class Car extends MovieClip
    {

        // imports...

        public function Car()
        {
            addListeners();
        }

        private function addListeners():void
        {
            this.addEventListener(MouseEvent.MOUSE_DOWN,clone);
        }

        private function clone(e:MouseEvent):void
        {
            // Clone the object
            var newcar = new e.target.constructor;
            newcar.graphics.copyFrom(this.graphics);
            newcar.x = this.x;
            newcar.y = this.y;

            this.parent.addChild(newcar);

            newcar.addEventListener(MouseEvent.MOUSE_MOVE,dragCar);
            newcar.addEventListener(MouseEvent.MOUSE_UP,dropCar);
        }

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

        private function dropCar(e:MouseEvent):void
        {
            e.target.stopDrag();

                    // This line doesn't remove the event, and I don't know why
            e.currentTarget.removeEventListener(MouseEvent.MOUSE_DOWN,clone);

            e.target.removeEventListener(MouseEvent.MOUSE_MOVE, dragCar);
            e.target.removeEventListener(MouseEvent.MOUSE_UP,dropCar);

        }

    }

I hope someone can help me. Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T14:15:47+00:00Added an answer on May 25, 2026 at 2:15 pm

    Here as you are creating a new instance of Car in the clone function, the constructor of that new Car object is called which in turn calls addListeners() and the clone has a MOUSE_DOWN event listener that again clones the clone. That is why you have the problem you describe.

    To avoid this you need to add the following line in you clone function (this does not work see below edit)

    newcar.removeEventListener(MouseEvent.MOUSE_DOWN,clone);
    

    this removes the clone event listener from the cloned (new) Car instance and avoids cloning again.

    Also to start drag you should do it in MOUSE_DOWN instead of MOUSE_MOVE.

    Update
    OK I see, MOUSE_DOWN is not called Again for the clone, so you need to use MOUSE_MOVE. Thus in this case I would remove the MOUSE_MOVE listener in the listener’s body so that it is only called once.

    Update
    This seems to remove the event listener.

    newcar.removeEventListener(MouseEvent.MOUSE_DOWN, newcar.clone);
    

    You have to refer to the newcar instance’s clone method newcar.clone to remove the event listener.

    Working Code
    The following code is working just fine

    package{
        import flash.display.MovieClip;
        import flash.events.*;
        public class Car extends MovieClip
        {
            public function Car()
            {
                addListeners();
            }
    
            private function addListeners():void
            {
                this.addEventListener(MouseEvent.MOUSE_DOWN,clone);
            }
    
            private function clone(e:MouseEvent):void
            {
                // Clone the object
                var newcar = new e.target.constructor;
                newcar.graphics.copyFrom(this.graphics);
                newcar.x = this.x;
                newcar.y = this.y;
    
                this.parent.addChild(newcar);
    
                // remove the clone listener
                newcar.removeEventListener(MouseEvent.MOUSE_DOWN, newcar.clone);
    
                // add dragging listeners
                newcar.addEventListener(MouseEvent.MOUSE_MOVE, dragCar);
                newcar.addEventListener(MouseEvent.MOUSE_UP, dropCar);
    
                // this is used for dragging the clone
                newcar.addEventListener(MouseEvent.MOUSE_DOWN, dragCar);
            }
    
            private function dragCar(e:MouseEvent):void
            {
                // if a MOUSE_MOVE event listener is registered remove it
            if(e.target.hasEventListener(MouseEvent.MOUSE_MOVE))
                e.target.removeEventListener(MouseEvent.MOUSE_MOVE, dragCar);
                e.target.startDrag();
            }
    
            private function dropCar(e:MouseEvent):void
            {
                e.target.stopDrag();
    
                // do not remove the listener if you want the clone to be dragable
                // e.target.removeEventListener(MouseEvent.MOUSE_UP,dropCar);
            }
        }
    }
    

    Keep clone dragable
    For this in the above code I have added a MOUSE_DOWN event listener for dragging the clone

    newcar.addEventListener(MouseEvent.MOUSE_DOWN, dragCar);
    

    And commented out the e.target.removeEventListener(MouseEvent.MOUSE_UP,dropCar); in dropCar

    Also added a Check before removing MOUSE_MOVE listener as to whether it is there or not, as this function will be called later by MOUSE_DOWN also.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
Seemingly simple, but I cannot find anything relevant on the web. What is the
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I need to clean up various Word 'smart' characters in user input, including but
I want to construct a data frame in an Rcpp function, but when I
I am writing an app with both english and french support. The app requests
I am using Paperclip to handle profile photo uploads in my app. They upload

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.