Line Following Mouse

Hi
Actually I already have a class that does this exact thing
you can enjoy it here - http://megaswf.com/serve/1154775
if you play around with it you will notice that the frame rate drops significantly when you move the mouse fast for a long time,
especially if you add more objects and logic to the game (press space and see what happens )
my question is how can I make this efficiant and not affect the frame rate.
heres the code:
package 
     import adobe.utils.ProductManager;
     import flash.display.DisplayObject;
     import flash.display.DisplayObjectContainer;
     import flash.display.MovieClip;
     import flash.display.Sprite;
     import flash.events.Event;
     import flash.events.MouseEvent;
     import flash.geom.Point;
      * @author Gadi G
     public class MouseHandler extends Sprite
          private  var _stage:DisplayObjectContainer;
          public var _bDraw:Boolean = false;
          private var _arrLine:Array = new Array();;
          private var _angle;
          private var _p1:Point =  new Point();
          private var _p2:Point = new Point();
          private var cos:Number;
          private var sin:Number;
          private var lineDrawing:Line;
          private var angle;
          private var distance:uint;
          private static var instance:MouseHandler;
          public var _mousePos:Point = new Point();
          public function MouseHandler()
          public static function getInstance():MouseHandler
               if (instance == null)
                    instance = new MouseHandler();
               return instance;
          public function startListening(prnt:DisplayObjectContainer)
               _stage = prnt;
               _stage.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
               _stage.addEventListener(MouseEvent.MOUSE_UP, mouseUpHandler);
          private function mouseDownHandler(e:MouseEvent):void
               _bDraw = true;
               _p2.x = mouseX;
               _p2.y = mouseY;
               _stage.addEventListener(Event.ENTER_FRAME, enterFrameHandler);
               _stage.addEventListener(MouseEvent.MOUSE_MOVE, drawMouseLine);
          private function mouseUpHandler(e:MouseEvent):void
               _bDraw = false;
               _stage.removeEventListener(MouseEvent.MOUSE_MOVE, drawMouseLine);
          private function enterFrameHandler(e:Event):void
               for each (var s:Line in _arrLine)
                    s.scaleX = s.scaleY *=  .79;
                    // the lowest possile value is one twip which is 0.05
                    if (s.width < .7)
                         _arrLine.splice(_arrLine.indexOf(s), 1);
                         _stage.removeChild(s);
                         s = null;
               if (!_bDraw)
                    //_mousePos = null;
                    if (_arrLine.length == 0)
                         _stage.removeEventListener(Event.ENTER_FRAME, enterFrameHandler);
               _mousePos.x = mouseX;
               _mousePos.y = mouseY;
          private function drawMouseLine(e:MouseEvent):void
               _p1.x = mouseX;
               _p1.y = mouseY;
               _angle = Math.atan2(_p1.y - _p2.y,_p1.x - _p2.x);
               var rot:Number = (180 / Math.PI ) * _angle + 180;
               cos = Math.cos(_angle);
               sin = Math.sin(_angle);
               distance = Math.round(Point.distance(_p1,_p2)) + 1;
               while (--distance)
                    lineDrawing = new Line();
                    lineDrawing.x = _p2.x;
                    lineDrawing.y = _p2.y;
                    lineDrawing.rotation = rot;
                    _arrLine.push(_stage.addChild(lineDrawing));
                    lineDrawing = null;
                    _p2.x +=  cos;
                    _p2.y +=  sin;
               _p2.x = _p1.x;
               _p2.y = _p1.y;
Thanks

use:
package 
     import adobe.utils.ProductManager;
     import flash.display.DisplayObject;
     import flash.display.DisplayObjectContainer;
     import flash.display.MovieClip;
     import flash.display.Sprite;
     import flash.events.Event;
     import flash.events.MouseEvent;
     import flash.geom.Point;
      * @author Gadi G
     public class MouseHandler extends Sprite
          private  var _stage:DisplayObjectContainer;
          public var _bDraw:Boolean = false;
          private var _arrLine:Array = new Array();;
          private var _angle;
          private var _p1:Point =  new Point();
          private var _p2:Point = new Point();
          private var cos:Number;
          private var sin:Number;
          private var lineDrawing:Line;
          private var angle;
          private var distance:uint;
          private static var instance:MouseHandler;
          public var _mousePos:Point = new Point();
          public function MouseHandler()
          public static function getInstance():MouseHandler
               if (instance == null)
                    instance = new MouseHandler();
               return instance;
          public function startListening(prnt:DisplayObjectContainer)
               _stage = prnt;
               _stage.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
               _stage.addEventListener(MouseEvent.MOUSE_UP, mouseUpHandler);
          private function mouseDownHandler(e:MouseEvent):void
               _bDraw = true;
               _p2.x = mouseX;
               _p2.y = mouseY;
               _stage.addEventListener(Event.ENTER_FRAME, enterFrameHandler);
               _stage.addEventListener(MouseEvent.MOUSE_MOVE, drawMouseLine);
          private function mouseUpHandler(e:MouseEvent):void
               _bDraw = false;
               _stage.removeEventListener(MouseEvent.MOUSE_MOVE, drawMouseLine);
          private function enterFrameHandler(e:Event):void
               for each (var i:int= _arrLine.length-1;i>=0;i--)
var s:Line=_arrLine[i];
                     s.scaleX = s.scaleY *=  .79;
                    // the lowest possile value is one twip which is 0.05
                    if (s.width < .7)
                         _arrLine.splice(_arrLine.indexOf(s), 1);
                         _stage.removeChild(s);
                         s = null;
               if (!_bDraw)
                    //_mousePos = null;
                    if (_arrLine.length == 0)
                         _stage.removeEventListener(Event.ENTER_FRAME, enterFrameHandler);
               _mousePos.x = mouseX;
               _mousePos.y = mouseY;
          private function drawMouseLine(e:MouseEvent):void
               _p1.x = mouseX;
               _p1.y = mouseY;
               _angle = Math.atan2(_p1.y - _p2.y,_p1.x - _p2.x);
               var rot:Number = (180 / Math.PI ) * _angle + 180;
               cos = Math.cos(_angle);
               sin = Math.sin(_angle);
               distance = Math.round(Point.distance(_p1,_p2)) + 1;
               while (--distance)
                    lineDrawing = new Line();
                    lineDrawing.x = _p2.x;
                    lineDrawing.y = _p2.y;
                    lineDrawing.rotation = rot;
_stage.addChild(lineDrawing)
                    _arrLine.push(lineDrawing);
                    lineDrawing = null;
                    _p2.x +=  cos;
                    _p2.y +=  sin;
               }                _p2.x = _p1.x;                _p2.y = _p1.y;           }      }   }
Thanks

Similar Messages

  • Weird lines follow mouse cursor... (PICS)

    I have a iMac 2.4Ghz and it has weird lines that follow the mouse cursor at all times. Does anybody have suggestions as of what it could be? Computer is out of warranty.
    Here's a pic.
    http://tinypic.com/r/5bryap/3

    Before concluding it is hardware related (which is possible), I would start up from a different system (such as your Mac OS X installation disc). If that makes the problem go away, you can try to further isolate the cause by booting normally and creating a new admin user account using System Preferences Accounts pane.
    http://docs.info.apple.com/article.html?path=Mac/10.5/en/8235.html
    Log out and log in to the new account. In the new account, do you see this problem. Since the new account uses default user preference settings (and does not have any third-party background processes), if the problem goes away, there must be something in your normal user account that is the cause.
    Actually, before you do anything else, if you have not done so already, you should do the general trouble-shooting +first steps+, reset PRAM
    http://support.apple.com/kb/HT1379
    and run +Repair Disk Permissions+ in Disk Utility.

  • Small Black Vertical Lines follow my mouse pointer AND surround all ICONS

    Small Black Vertical Lines follow my mouse pointer AND surround all ICONS on my desktop. I just turned on my computer and it showed like this. I already tested my monitor and it's not the monitor.
    See a picture of my problem here on my me gallery:
    http://gallery.me.com/egarfias/100024
    Looking at websites, it's fine the display but any icons and my pointer and things that move have these lines following! Looking at VIDEO, it's the picture is full of white and black lines though you can see the video just fine.
    Any ideas? Thank you!!

    I am in Safe Mode now: This is the weirdest thing: A bit from column A AND B:
    The display is perfect EXCEPT for two parallel lines of 17, 3 millimeter-long black lines that follow the pointer everywhere it goes.
    Get this: my current display is 1024x768, but when I change it to 640x480, the pointer and everything is perfect! (of course, I can only see a fraction of my desktop)
    But then again... when I zoom in and out holding down control (in 640x480) the pointer lines appear again, but less lines. They disappear when I re-reset it to 640.
    Strange.
    But yeah, safe mode is NEAR perfect with the except of the cursor lines. I can screen grab my cursor though to show...

  • Red lines surrounding mouse cursor arrow, shutdown

    noticed that four small dotted red lines have appeared around the cursor arrow..lines only follow mouse movement and do not appear anywhere else on the screen...my imac just now froze as i was scanning in a pic. i've done a manual restart and everything looks fine, starts to go through the "your computer did not shut down properly" screen without a hitch and then the dancing red lines appear. mac can make it through this screen but then freezes in main startup. this is the third time ive had to do a hard shut down. ??
    has anyone run into the dancing red lines before? any suggestions?
    any help is greatly appreciated
    (btw: accessing internet from a pc at work)
    imac   Mac OS 9.2.x  

    Hi, monica -
    Just a thought - do you have the CloseView control panel installed? If so, try turning CloseView off within that control panel, or use the keyboard shortcut to toggle it off, which is Command-Option-K.
    If you can't get the machine booted successfully to get to that control panel, then try booting with Extensions Off - hold down the Shift key from the start of booting until the desktop has fully loaded.
    Then drag that control panel out of the Control Panels folder (in System Folder) and restart. I'm not sure if that control panel uses an associated preferences file, but you might look for one (System Folder >> Preferences) - if there, remove it to the desktop also.

  • Focus follows mouse, etc

    So many years ago I was a mac user. And many years ago this forum looked different...
    But I have since switched to linux. Which I like because I can program it however I want.
    BUT now I have a mac again. My macbook with intel ran linux, but it died. So now I have a powerbook. Since PPC linux ***** I'm running 10.5 on it.
    I actually really like this system. It's pretty straight forward and easy to use, just a few things I want for it that I can't find.
    One big major thing that's really holding me back with the system is there's no focus follows mouse. Especially with the small screen it's something I really need to use the computer. And no auto-raise please. That completely ruins the point of focus follows mouse.
    I do have it working in terminal via playing with some plist files. And that's mostly where I use it. But I use it enough on the rest of the system that it's something I really want.
    Also, I'd really like to be able to hold down a modifier key, and click anywhere in a window to be able to move it. Something else I've become really accgstomed to in my GUI that I'd like to have on this little machine.
    Thanks for any help.

    Cool! Thanks for Window Wrangler! That's a pretty neat program. Not what I was looking for exactly, but it'll do just fine.
    I now see what you were trying to show me with the zoom. That's not what I'm looking for.
    What I'm looking for is focus follows mouse. What it does is as you put a mouse over a window, that window gets focus. But does not raise to the top.
    So say I have a web browser open, and my IM client open. I'll leave the client at the bottom of the screen and have the browser sized up just enough to see the text field and the first two lines of conversation of the chat.
    Now, on my linux system to reply all I have to do is put the curser over the IM window and I can type into it without having to click on it and bring it to the top.
    For that example, it isn't a huge deal. But when you have 5 or 6 windows open all organized with each on the right level, bringing one to the top will screw it up.
    I'm liable to have a few terminal windows, IM, email, IM, some programming ide, etc open at once. It's especially useful for web development. I can have the source file open and edit it, but not bring it over the browser window so I can have an idea of what I'm doing with the source.

  • Disable window focus follows mouse without click

    I've noticed that I can scroll a window without clicking on it by moving the mouse cursor over that window.  For example, this Safari window has focus, and I'm typing in it, but if I move the cursor to another application (Terminal or another application), I can scroll that other window without having to click on it.  This gets me into trouble, because I can scroll around the other window then start typing, thinking that I'm in that other window because I'm actively doing something to it (scrolling), but the text goes to the window which really has the focus.  For example, Mail has focus, then I scroll around a Terminal looking for something, and I type the delete key forgetting that I'm in Mail.  This has an advantage in that one can scroll a window that is not on top, but there are drawbacks.
    Is there a way to require the click in a window in order to scroll in that window?
    Thanks,
    ==Leonard

    Cool! Thanks for Window Wrangler! That's a pretty neat program. Not what I was looking for exactly, but it'll do just fine.
    I now see what you were trying to show me with the zoom. That's not what I'm looking for.
    What I'm looking for is focus follows mouse. What it does is as you put a mouse over a window, that window gets focus. But does not raise to the top.
    So say I have a web browser open, and my IM client open. I'll leave the client at the bottom of the screen and have the browser sized up just enough to see the text field and the first two lines of conversation of the chat.
    Now, on my linux system to reply all I have to do is put the curser over the IM window and I can type into it without having to click on it and bring it to the top.
    For that example, it isn't a huge deal. But when you have 5 or 6 windows open all organized with each on the right level, bringing one to the top will screw it up.
    I'm liable to have a few terminal windows, IM, email, IM, some programming ide, etc open at once. It's especially useful for web development. I can have the source file open and edit it, but not bring it over the browser window so I can have an idea of what I'm doing with the source.

  • Macbook pro 15" retina, display flickers, weird lines follow cursor

    Just got this MacBook Pro on Christmas... Installed Microsoft Office Suite, Adobe Photoshop, and Adobe Illustrator today. Later in the day the display became very discolored; vertical and horizontal "shaded" lines follow the cursor, areas above open windows are discolored (lighter), and the screen flickers when typing. I did the software update and have restarted the system, but it is still having issues. Does anyone know of a fix, short of getting into an Apple Store or scheduling a call with Apple?
    Thanks!!
    Maggi and Joe

    Nope - it sounds as if you've GPU issues and the discrete GPU is soldered onto the logic board. take your new machine into your local Apple Store for a replacement. Don't wait too long!
    Clinton

  • HT204088 I have purchased stickers LINE Order ID: MJ647N61DH but I can not download sticker LINE, Mickey Mouse.

    Billed To:
    [email protected]
    Surin Nawgrod
    224 moo 6 T.Nicom A.Mung
    Meuang, Lopburi 15000
    THA
    Order ID: MJ647N61DH
    Receipt Date: 14/01/13
    Order Total: $3.98
    Billed To: Visa .... 9677
    Item
    Developer
    Type
    Unit Price
    LINE, Mickey Mouse
    Report a Problem
    NAVER JAPAN
    In App Purchase
    $1.99
    LINE, Minnie Mouse
    Report a Problem
    NAVER JAPAN
    In App Purchase
    $1.99
    Order Total:
    $3.98
    I can use LINE, Minnie Mouse but i can't use LINE, Mickey Mouse , please chack for me. Thank you

    You should not leave personal info on a public forum
    You are not addressing Apple here at all.
    We are all itunes suers just like you

  • Ugh!  Please restore old Terminal.app focus-follows-mouse behavior!

    The 10.4 and prior Terminal.app's focus-follows-mouse feature allowed one to type into the terminal by mousing over it even if the Terminal wasn't the currently focussed application. It didn't focus Terminal when you did this, but did direct keyboard input to it.
    10.5's Terminal.app only allows focus follows mouse to work when it's the focussed application, which destroys about 80% of this feature's usability for me. I like to keep things like finder and browser windows overlapping most of a terminal window and then just slide the mouse over to the terminal to type there, and I can no longer do that in 10.5. Now I have to click on the Terminal window, bringing all my terminal windows forward and obscuring the windows I wanted on top. Very annoying.
    Is there any way to restore the old behaviour? If not, can we please have it back?
    Luckily, 10.4's Terminal.app still behaves properly in 10.5, so I'm using it, but I'd like to have some of the new features of 10.5's Terminal.app. Unfortunately, "broken" focus-follows-mouse is a deal-breaker for me.

    The command I know,
    defaults write com.apple.Terminal FocusFollowsMouse -string Yes
    seems to work about the same in Leopard as in Tiger for me. I don't remember ever being able to get focus-follows-mouse behaviour to work in a non-active application from an active non-focus-follows-mouse application. In other words one can focus follows mouse from one Terminal to another, but not from Finder to Terminal.
    The X11 equivalent (why the inconsistent key and value?) is
    defaults write com.apple.X11 wm_ffm -bool true
    I remember being able to focus-follows-mouse between Terminal and X11, but cannot in Leopard. However, I also cannot in Tiger 10.4.11 either, so maybe it is my memory that is faulty.
    What I don't seem to be able to do, but could do in Tiger, is
    defaults write com.apple.X11 swapaltmeta -int 1
    On Tiger, that would make the option key the meta key, so that e.g. alt-d would delete forward one word, etc. On Leopard, this seems to be a NOP which means that cmd is the only possible meta key, which I personally find rather confusing.

  • Why do dark gray lines follow my cursor wherever I move it?

    Why do dark gray lines follow my cursor wherever I move it? Usually happen after computer is awakened from sleep mode.

    The display is not sleeping, though the account is logged out. See the support article below for a resolution:
    http://support.apple.com/kb/TS4135?viewlocale=en_US

  • Draw a line using mouse

    Hello there:
    I'm trying to draw a line using mouse pointer: My code is:
    public class DrawLine extends JFrame implements MouseListener, MouseMotionListener
        int x0, y0, x1, y1;  
        public DrawLine()
             addMouseListener(this);
             addMouseMotionListener(this);
        public void mouseDragged(MouseEvent e)
             x1 = e.getX();
             y1 = e.getY();
             repaint();
        public void mouseMoved(MouseEvent e) { }
        public void mouseClicked(MouseEvent e){ }
        public void mouseEntered(MouseEvent e) { }
        public void mouseExited (MouseEvent e) { }
        public void mousePressed(MouseEvent e)
              x0 = e.getX();
              y0 = e.getY();           
        public void mouseReleased(MouseEvent e)
              x1 = e.getX();
              y1 = e.getY();
       public void paint(Graphics g)
                 g.setColor(Color.BLACK);
              g.drawLine(x0, y0, x1, y1);
        public static void main(String[] argv)
             DrawLine dr=new DrawLine("Test");
             dr.setVisible(true);
             dr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }when mouse is dragged, multiple lines are being drawn....
    could you else please tell me what should I've to do???
    thanks n regards...
    Dev

    You can implement the listeners on any class, even one that (implicitly) extends Object. What matters is that the listener is added to the component that needs to use it.
    That said, why do you want to extend JFrame? Are you adding functionality to the JFrame to justify extending the JFC class? Note that extending JFrame allows the users of your class to access the functionality of a JFrame, is that really indicated here?
    one class that extends JFrame, and one can draw a line on JLabel, embedded within JFrame!So you still have to override paintComponent of the JLabel, which implies using an anonymous inner class.
    Starting with the example already posted, that would be:
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.Graphics;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseListener;
    import java.awt.event.MouseMotionListener;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.SwingUtilities;
    public class DrawLineTest
        implements MouseListener, MouseMotionListener {
      JLabel label;
      int x0, y0, x1, y1;
      private void makeUI() {
        JFrame frame = new JFrame("DrawLineTest");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        label = new JLabel("FFFF") {
          public void paintComponent(Graphics g) {
            super.paintComponent(g);
            g.setColor(Color.BLACK);
            g.drawLine(x0, y0, x1, y1);
        label.setPreferredSize(new Dimension(500, 500));
        label.addMouseListener(this);
        label.addMouseMotionListener(this);
        frame.add(label);
        frame.pack();
        frame.setVisible(true);
      public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
          public void run() {
            new DrawLineTest().makeUI();
      public void mousePressed(MouseEvent e) {
        x0 = e.getX();
        y0 = e.getY();      
      public void mouseReleased(MouseEvent e) {
        x1 = e.getX();
        y1 = e.getY();
      public void mouseDragged(MouseEvent e) {
        x1 = e.getX();
        y1 = e.getY();
        label.repaint();
      public void mouseMoved(MouseEvent e) { }
      public void mouseClicked(MouseEvent e){ }
      public void mouseEntered(MouseEvent e) { }
      public void mouseExited (MouseEvent e) { }
    }Better spend more time with the tutorials, there's a separate section on writing event listeners.
    db

  • White and black line follower

    need help to develop VI in labview for line follower which will track black path only and avoid white line , can any one please help me with VI ? 
    Attachments:
    line follower.vi ‏8 KB

    Middle School and High School students involved in FIRST Robotic have been doing this and have posted examples
    http://www.usfirst.org/
    Middle School Lego League Robotic
    https://www.google.com/webhp?sourceid=chrome-instant&ion=1&espv=2&ie=UTF-8#q=lego%20line%20follower
    High School FRIST Robotic
    https://decibel.ni.com/content/docs/DOC-14730
    http://www.chiefdelphi.com/forums/showthread.php?t=93826&highlight=Line+follow
    http://www.chiefdelphi.com/forums/showthread.php?t=91391&highlight=Line+follow
    http://www.chiefdelphi.com/forums/showthread.php?t=90386&highlight=Line+follow
    Omar

  • How to make a line follow a motion path

    alright, i'm gonna try and explain this as best as possible. i want to do something like what apple did for the motion video, where lines follow a motion path. but my problem is that when the line hits a keyframe, the line stays straight and turns its axis (looking ********), unlike apple's vid where the line itself turns and curves around each keyframe.....alright does that make any sense? look at the picture below, see how the lines are curved to follow a path....my lines stay straight and look like the second pic.
    http://img279.imageshack.us/img279/1269/picture59rh.png RIGHT WAY
    [img=http://img279.imageshack.us/img279/1387/picture68sb.th.png] WRONG WAY
    iMac G5   Mac OS X (10.4.5)  

    Don't do it like that. Create a curved line and use a replicator on it...
    Patrick

  • Xmonad and focus follows mouse issue

    I've been fiddling around with xmonad, and I notice that with focus follows mouse turned on, when I create a new window the window under the mouse gets selected instead of the window that I have just created. Pretty irritating, Anyone know how to solve that?
    Thanks for any help provided,
    Kruppe

    Yes, disabling the focus follows mouse behaviour makes it so the correct window is always chosen.
    I kind of like having focus follows mouse for when I need to copy and paste in applications that don't provide good methods for doing that purely with the keyboard (firefox and most terminals when not using screen). I would therefore like to have focus follows mouse.
    Sidenote: What is this updatePointer thing you use and what does it do?

  • Focus follows mouse

    Hi,
    Is there a way to keep a window open while another window is selected? What I would like to is manually type in a window while another window is visible. Other than clicking on alternate windows or resizing the windows I know of no way to do this.
    It seems to me that I had a utility that had focus follows mouse as one of the options. This stopped working a couple of releases ago and I have forgotten its name.
    Thanks.

    I can't appreciate the small space since I don't have a laptop, but you should be able to squeeze in two, either side by side or one on top of the other. Increase the display resolution, maybe? Other than that, I feel thy pain.

Maybe you are looking for

  • Re-executing Activities in a Scope Activity with the Replay Activity

    Hello All, Our destination DB can go down and may take 2 -3 hrs to come back. We would want to catch a fault and then would want to replay the Invoke activity every 15 = 30 minutes hoping the DB has come UP and so records would get inserted. Understa

  • Bleed Areas and PDF Export

    I set my book up in indesign and have all the bleed areas marked out.  When i export to a pdf it comes out fine, but when i export to a pdf  using the documents bleed settings i get an issue. On the two pages facing each other the inside edge of each

  • BURN PROBLEM WITH IPHOTO:

    When I manually sort one of my albums and then burn a disc, the results never come out right. In other words, the sort never took place and the disc shows the original album.

  • When I sign out of my bank account, Firefox says its blocked and I have to close the browser, why?

    I clicked sign out from my bank account, and a message came up that my account has been blocked, and I am requested to close my browser. Once I received an error message. ID Code:c967ecee-285b-4a1c-85fe-3f5cfdaaafdd This has happened several time now

  • HR OM  LOCK and UNLOCK object!

    hi! some one know HR OM object's LOCK and UNLOCK object ? thank you very much!