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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T06:04:26+00:00 2026-05-30T06:04:26+00:00

I’m refreshing my AS3 basic knowledge. Now I’m playing with creating movieclips with dimensions

  • 0

I’m refreshing my AS3 basic knowledge. Now I’m playing with creating movieclips with dimensions specified by mouse distance.

This is my full code:

http://pastebin.com/4jpqFn7N

package  {

    import flash.display.MovieClip;
    import flash.display.Shape;
    import flash.display.Graphics;
    import flash.events.MouseEvent;
    import wck.World;

    public class Game extends World {
        private var isDragging:Boolean = false;
        private var line:Shape = new Shape();
        private var lineInfo:Array = [];
        public function Game() {
            addEventListener(MouseEvent.MOUSE_DOWN, startDragging);
            addEventListener(MouseEvent.MOUSE_MOVE, recalcDrag);
            addEventListener(MouseEvent.MOUSE_UP, stopDragging);
        }

        private function addIt(dist:Number):void {
            var i:Rect = new Rect();
            i.x = mouseX;
            i.y = mouseY;
            i.width = dist;
            i.height = dist;

            addChild(i);
        }

        private function startDragging(e:MouseEvent):void {
            if(!(e.target is Rect)) {
                isDragging = true;
                lineInfo['bX'] = mouseX;
                lineInfo['bY'] = mouseY;
                line.graphics.moveTo(mouseX, mouseY); 
                addChild(line);
            }
        }

        private function recalcDrag(e:MouseEvent):void {
            if(isDragging) {                
                line.graphics.clear();
                line.graphics.lineStyle(10, 0x000, .65);
                line.graphics.moveTo(lineInfo['bX'], lineInfo['bY']); 
                line.graphics.lineTo(mouseX, mouseY);
            }
        }

        private function stopDragging(e:MouseEvent):void {
            if(isDragging) {
                var distance = DistanceTwoPoints(mouseX, lineInfo['bX'], mouseY, lineInfo['bY']);
                line.graphics.clear();
                isDragging = false;
                if(!(e.target is Rect) && distance != 0 ) addIt(distance);
            }
        }

        public function DistanceTwoPoints(x1:Number, x2:Number,  y1:Number, y2:Number): Number {
            var dx:Number = x1-x2;
            var dy:Number = y1-y2;
            return Math.sqrt(dx * dx + dy * dy);
        }
    }

}

However I have feeling that redrawing whole line on mouse move might be resource expensive. How can I lighten it up a bit?

  • 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-30T06:04:28+00:00Added an answer on May 30, 2026 at 6:04 am

    you could employ an ENTER_FRAME event instead of a MOUSE_MOVE event. MOUSE_MOVE is quite an expensive call, especially on mobile devices, and it has no real benefits over ENTER_FRAME if you set it up correctly.

    also, if you’re not using the timeline in Flash Professional, you should use Sprite objects instead of MovieClip objects.

    note: the following code is untested:

    package
    {   
        //Imports
        import flash.display.Sprite;
        import flash.display.Shape;
        import flash.display.Graphics;
        import flash.events.MouseEvent;
        import flash.events.Event;
        import wck.World;
    
        //Class
        public class Game extends World
        {
            //Properties
            private var line:Shape;
            private var lineInfo:Array;
    
            //Constructor
            public function Game()
            {
                addEventListener(MouseEvent.MOUSE_DOWN, mouseDownEventListener);
            }
    
            //Mouse Down Event Listener
            private function mouseDownEventListener(e:MouseEvent):void
            {
                if (!(e.target is Rect))
                {
                    removeEventListener(MouseEvent.MOUSE_DOWN, mouseDownEventListener);
                    addEventListener(MouseEvent.MOUSE_MOVE, mouseMoveEventListener);
    
                    lineInfo = new Array();
                    lineInfo['bX'] = mouseX;
                    lineInfo['bY'] = mouseY;
    
                    line = new Shape();
                    line.graphics.moveTo(mouseX, mouseY);
    
                    addChild(line);
                }
            }
    
            //Mouse Move Event Listener
            private function mouseMoveEventListener(e:MouseEvent):void
            {
                removeEventListener(MouseEvent.MOUSE_MOVE, mouseMoveEventListener); 
                addEventListener(Event.ENTER_FRAME, enterFrameEventListener);
                addEventListener(MouseEvent.MOUSE_UP, mouseUpEventListener);
            }
    
            //Enter Frame Event Listener
            private function enterFrameEventListener(e:Event):void
            {
                line.graphics.clear();
                line.graphics.lineStyle(10, 0x000, .65);
                line.graphics.moveTo(lineInfo['bX'], lineInfo['bY']); 
                line.graphics.lineTo(mouseX, mouseY);
            }
    
            //Mouse Up Event Listener
            private function mouseUpEventListener(e:MouseEvent):void 
            {
                removeEventListener(Event.ENTER_FRAME, enterFrameEventListener);
                removeEventListener(MouseEvent.MOUSE_UP, mouseUpEventListener);
                addEventListener(MouseEvent.MOUSE_DOWN, mouseDownEventListener);
    
                line.graphics.clear();
    
                var distance:Number = distanceBetweenTwoPoints(mouseX, lineInfo['bX'], mouseY, lineInfo['bY']);
    
                if (!(e.target is Rect) && distance != 0)
                {
                    addRectangle(distance);
                }
            }
    
            //Distance Between Two Points
            public function distanceBetweenTwoPoints(x1:Number, x2:Number, y1:Number, y2:Number):Number
            {
                var dx:Number = x1 - x2;
                var dy:Number = y1 - y2;
    
                return Math.sqrt(dx * dx + dy * dy);
            }
    
            //Add Rectangle
            private function addRectangle(distance:Number):void
            {
                var rect:Rect = new Rect();
                rect.x = mouseX;
                rect.y = mouseY;
                rect.width = distance;
                rect.height = distance;
    
                addChild(rect);
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a jquery bug and I've been looking for hours now, I can't
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
Does anyone know how can I replace this 2 symbol below from the string
I have this code to decode numeric html entities to the UTF8 equivalent character.
That's pretty much it. I'm using Nokogiri to scrape a web page what has

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.