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 9156831
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T12:51:53+00:00 2026-06-17T12:51:53+00:00

How do I make a class that for example, handles all startDrag() and stopDrag()

  • 0

How do I make a class that for example, handles all startDrag() and stopDrag() events? There is no MovieClip for this class. I want this class to function solely as a handler for events that are used by multiple classes.

I’m making a game and I have lots of items that can be dragged and dropped, and I need a more efficient way rather than putting this code in every single item class.

So is there any other way of doing this? Right now I have to copy and paste this into every class I have that can be dragged and dropped.

public function Weapon1() 
{
        originalPosition = new Point(x,y);
        addEventListener(Event.ENTER_FRAME, onEntrance);
}
public function onEntrance(evt:Event)
{
        addEventListener(MouseEvent.MOUSE_DOWN, mousedown);
        addEventListener(MouseEvent.MOUSE_UP, mouseup);
}
public function mousedown(evt:MouseEvent)
{
        startDrag(true);
}
public function mouseup(evt:MouseEvent)
{
        stopDrag();
        if(dropTarget)
        {
            if(dropTarget.parent.name == "slot")
            {
                this.x = dropTarget.parent.x;
                this.y = dropTarget.parent.y;
            }
        }
        else
        {
            returnToPosition();
        }
    }
    else
    {
        returnToNewPosition();
    }
}
public function returnToPosition()
{
    x = originalPosition.x;
    y = originalPosition.y;
}
public function returnToNewPosition()
{
    x = newPosition.x;
    y = newPosition.y;
}
  • 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-06-17T12:51:55+00:00Added an answer on June 17, 2026 at 12:51 pm

    What you need is Inheritance which is one of the four fundamental concepts of object-oriented programming. In OOP, inheritance enables classes to take on the properties and methods of an existing base class (also called a super class).

    So; You should create a base class with drag and drop logic and extend that class to create your other derived classes (also called sub-classes).

    For example, here is the base class (Draggable):

    package
    {
        public class Draggable extends MovieClip
        {
            //Constructor
    
            public function Draggable()
            {
                addEventListener(Event.ADDED_TO_STAGE, onStageReady);
            }
    
            //Event Handlers
    
            protected function onStageReady(event:Event):void
            {
                addEventListener(MouseEvent.MOUSE_DOWN, mousedown);
                addEventListener(MouseEvent.MOUSE_UP, mouseup);
            }
    
            protected function mousedown(event:MouseEvent):void
            {
                startDrag(true);
                //more stuff here...
            }
    
            protected function mouseup(event:MouseEvent):void
            {
                stopDrag();
                //more stuff here...
            }
        }
    }
    

    …and here is the Weapon1 class that extends the base class:

    package
    {
        public class Weapon1 extends Draggable
        {
            //Constructor
    
            public function Weapon1()
            {
                //initialize Weapon1
            }
    
            //Methods, handlers, etc...
        }
    }
    

    As you see; it’s the extend keyword that enables inheritance for the Weapon1 class.
    You can go on creating more weapon classes…

    public class Weapon2 extends Draggable { ... }
    public class Weapon3 extends Draggable { ... }
    public class Weapon4 extends Draggable { ... }
    

    Notice that the Draggable class also extends the MovieClip class to gain some other functionality. So, each class you extend from Draggable will have the functionality of both the MovieClip and Draggable classes.

    References:

    • Object Oriented Programming
    • Inheritance
    • Encapsulation
    • Abstraction
    • Polymorphism
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a member variable that in a class that I want to make
For example I have a class that builds the GUI, a class that handles
lets take this example, which I think covers all my questions: class SomeClass {
I'm designing a UI manager class that will manage all my UI elements (this
I am trying to make a class that will represent two different behaivours at
I would like to make a generic class that when tapped, makes the element
I am trying to extend the mysqli class to make a helper class that
I have method in a class that I need to make sure is only
I know from Algebra class that with ABC and 123 we can make 216
Given an instantiated JavaScript object/class can I make an Ajax call from that class

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.