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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T09:38:29+00:00 2026-06-11T09:38:29+00:00

I was thinking this is a simple task, but I’m wrong. I used a

  • 0

I was thinking this is a simple task, but I’m wrong.

I used a sprite to display an image, and when user drag it(MOUSE_DOWN and MOUSE_MOVE), I got the position in MOUSE_MOVE and calculated the offsets:

 var current: Point = new Point(event.localX, event.localY);
 sprite.x = current.x - start.x;
 sprite.y = current.y - start.y;

It works but not smooth. Is there a better solution?


UPDATE

After a day of debugging, I finally found the reason.

Bigger fps can make it smoother, but it’s not the key of this question.

The key is I should use stage to listen MOUSE_MOVE, not the image itself. And when getting the mouse position, I should use event.stageX/Y(or stage.mouseX/Y), not event.localX/Y. The event.localX/Y from the moving image, is neither stable nor smooth, that causes my problem.

Following is my working code, enjoy it 🙂

package {
    import flash.display.Bitmap;
    import flash.display.Loader;
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.events.MouseEvent;
    import flash.geom.Matrix;
    import flash.geom.Point;
    import flash.net.URLRequest;
    import flash.text.TextField;

    public class DragIssue extends Sprite {

        private static const URL: String = "assets/m1.jpg";

        private var self: DragIssue;

        private var sprite: Sprite;

        private var startPoint: Point;

        private var offsetX: Number = 0;
        private var offsetY: Number = 0;

        public function DragIssue() {
            self = this;
            init();
            loadImage();
        }

        protected function init(): void {
            if (stage == null) {
                this.addEventListener(Event.ADDED_TO_STAGE, on_addedToStage);
            } else {
                stage.addEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);
            }

            function on_addedToStage(event: Event): void {
                self.removeEventListener(Event.ADDED_TO_STAGE, on_addedToStage);
                stage.addEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);
            }
        }

        public function loadImage(): void {
            var loader: Loader = new Loader();
            loader.contentLoaderInfo.addEventListener(Event.COMPLETE, imageLoaded);
            loader.load(new URLRequest(URL));

            function imageLoaded(event: Event): void {
                var bitmap: Bitmap = event.target.content as Bitmap;

                self.sprite = new Sprite();
                sprite.addChild(bitmap);

                self.addChild(sprite);

                sprite.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
                sprite.addEventListener(MouseEvent.MOUSE_UP, onMouseUp);
            }
        }

        private function onMouseMove(event: MouseEvent): void {
            if (startPoint) {
                sprite.x = offsetX + event.stageX - startPoint.x;
                sprite.y = offsetY + event.stageY - startPoint.y;
            }
        }

        private function onMouseUp(event: MouseEvent): void {
            startPoint = null;
            offsetX = sprite.x;
            offsetY = sprite.y;
        }

        private function onMouseDown(event: MouseEvent): void {
            startPoint = new Point(event.stageX, event.stageY);
        }

    }
}
  • 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-11T09:38:30+00:00Added an answer on June 11, 2026 at 9:38 am

    Here, try this. It’s just a simple black square, but it looks fine until you really start dragging it around. As was mentioned, setting the framerate to something higher is ideal. In this case, I decided to up the framerate to 60fps in the MOUSE_DOWN and drop it back to 24 in MOUSE_UP for memory reasons. You can obviously change that how you please.

    import flash.display.*;
    import flash.events.*;
    
    
    var startX:Number;
    var startY:Number;
    var shape:Sprite = new Sprite();
    shape.graphics.beginFill(0x000000);
    shape.graphics.drawRect(0,0,50,50);
    shape.graphics.endFill();
    this.addChild(shape);
    
    shape.addEventListener(MouseEvent.MOUSE_DOWN,this.mouseDown);
    
    function mouseDown(e:MouseEvent = null):void{
        stage.frameRate = 60;
        startX = stage.mouseX - shape.x;
        startY = stage.mouseY - shape.y;
        stage.addEventListener(MouseEvent.MOUSE_MOVE,this.mouseMove);
        shape.addEventListener(MouseEvent.MOUSE_UP,this.mouseUp);
    }
    
    function mouseMove(e:MouseEvent = null):void{
        shape.x = stage.mouseX - startX;
        shape.y = stage.mouseY - startY;
    }
    
    function mouseUp(e:MouseEvent = null):void{
        shape.removeEventListener(MouseEvent.MOUSE_UP,this.mouseUp);
        stage.removeEventListener(MouseEvent.MOUSE_MOVE,this.mouseMove);
        stage.frameRate = 24;
    }
    

    Make sure you are removing the MOUSE_MOVE event on MOUSE_UP. That is key. Otherwise, you re-add the event on every MOUSE_DOWN and end up running the same code repeatedly, simultaneously. Sorry my syntax isn’t 100% proper; I threw this together really quick in CS5.5 rather than doing it in Flash Builder.

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

Sidebar

Related Questions

Maybe I'm thinking about this the wrong way but here's the idea: Class A
Been thinking about this for hours now. Im building a simple slideshow application, where
This is not as simple as it seems. Most of you are likely thinking
Im thinking this is correct, but probably WAY off. I have a string formatted
I was thinking this question for a while, and ever tried to google but
I know this is a task that can't be unique, but I can't seem
Im working on a simple task manager that would allow user to kill running
I know this may seem rather simple, but I am stumped on how to
I'm probably over-thinking this/wasting time trying to avoid a bit of conditional code -
Maybe I am just over thinking this and need to write some more prototype

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.