A Flash Developer Resource Site

Results 1 to 5 of 5

Thread: [RESOLVED] Updating mouse coordinates inside loops

  1. #1
    Senior Member
    Join Date
    Dec 2010
    Posts
    121

    resolved [RESOLVED] Updating mouse coordinates inside loops

    I'm figuring out a way to update my mouse x and y inside a loop. In this small test block, I'm tracing the mouseX and mouseY twice. The first one is outside a loop. The second one is inside a loop.

    When I test the SWF, you can see that the first trace changes constantly and seamlessly. But when you hold your mouse down, the code starts tracing the 2nd trace. Even though the mouse is down and moving, the mouseX and mouseY traced is not changing. Why is that?

    Actionscript Code:
    import flash.events.MouseEvent;
    import flash.events.Event;

    var mouseIsUp:Boolean = true;

    stage.addEventListener(MouseEvent.MOUSE_DOWN, mouseDown);
    stage.addEventListener(MouseEvent.MOUSE_UP, mouseUp);
    stage.addEventListener(Event.ENTER_FRAME, onEnter);

    function mouseDown(e:MouseEvent):void
    {
        mouseIsUp = false;
    }

    function mouseUp(e:MouseEvent):void
    {
        mouseIsUp = true;
    }

    function onEnter(e:Event):void
    {
        trace("1. Mouse coor " + stage.mouseX + ", " + stage.mouseY);

        if (! mouseIsUp)
        {
            for (var i:uint = 0; i < 100; i++)
            {
                trace("2. Mouse coor: " + stage.mouseX + ", " + stage.mouseY);     
            }
        }
    }

    Closely related to the main question, when you click the stage, you would get 100 lines of the 2nd trace. Is there a way to break the loop as soon as the mouse is up? Thank you

  2. #2
    Ө_ө sleepy mod
    Join Date
    Mar 2003
    Location
    Oregon, USA
    Posts
    2,441
    There's a lot going on here. First of all - when you click down, you are getting both traces - every frame is one line of 1. and 100 lines of 2.

    The way events work in Flash is a bit unintuitive - basically the flash movie is playing at a given frame rate - so those frames are ticking along while the flash player is open. Each frame is sliced up into a couple of steps internally - which makes an odd situation where a mouse event that happens in frame 1, doesn't trigger the eventHandler until the beginning of frame 2.

    In practice - it means that your onEnter happens in its entirety once every frame. Clicking down doesn't interrupt your code - it just waits until the next frame and handles the new "clicked down" state then. So for every frame the mouse will only have one position (it can't be measured more often than that) and then you're iterating over that 100 times inside the frame before you move to the next one.

    For a detailed explanation check out this article: http://www.craftymind.com/2008/04/18...sh-9-and-avm2/

    If you post what you're trying to accomplish there is probably another way to set up the mouse handling.
    Please use [php] or [code] tags, and mark your threads resolved 8)

  3. #3
    Senior Member
    Join Date
    Dec 2010
    Posts
    121
    This is the video link to what I'm working on (http://www.youtube.com/watch?v=9vDMOn3qzJE). It's 319KB, 19KB bigger than the allowed size in Flashkit, which is why I could not upload it to here, but it will load really fast anyway.

    In the video, after the 3rd second, the mouse is kept down and is moved around. The particles do not follow the mouse but kept generated at the initial mouse coordinates. The particles are generated inside the for loop so basically, now I'm trying to get to update the mouse coordinate inside the loop.

    I've set a max number of particles allowed to generate so after 3rd second, it will generate 1000 particles but the particles will not follow the mouse. At 11th second, some particles get out of the screen and now when I mouse down and move, the particles follows the mouse coordinates but I have no idea why. Once all the particles got out of the screen, at second 26, I hold mouse down again but this time, the particles generate only at the initial mouse coordinate and not following the mouse.

    The results are not consistent so another method of mouse handling is probably worthwhile to look at.

    EDIT: or, how can I update mouse coordinate inside the loop or break loop when mouse is up?

  4. #4
    Ө_ө sleepy mod
    Join Date
    Mar 2003
    Location
    Oregon, USA
    Posts
    2,441
    I see. Without seeing how your code is set up I can recommend a couple things.

    First - make sure you have a display object that covers your whole stage. There is a weird glitch sometimes where clicking the stage doesn't work as expected because you aren't hitting anything on the display tree - just the empty back of the swf.

    Next - there are a lot of ways to set up mouse dragging - here's the way I like to set up mine:

    PHP Code:
    var currentlyDragging:Boolean;

    function 
    onMouseDown(e:MouseEvent):void{
        if(
    currentlyDragging == false){
            
    currentlyDragging true;
            
    stage.addEventListener(Event.ENTER_FRAMEonDraggingEFrame);
        }
    }

    function 
    onMouseUp(e:MouseEvent):void{
        if(
    currentlyDragging){
            
    currentlyDragging false;
            
    stage.removeEventListener(MouseEvent.MOUSE_MOVEonDraggingEFrame);
        }
    }

    function 
    onDraggingEFrame(e:Event):void{
        if(
    currentlyDragging){
            
    //  myCursor.x = stage.mouseX;
            //  myCursor.y = stage.mouseY;
        
    }

    Lastly - you keep mentioning a loop. I'm not sure exactly what you mean but you have to remember that the screen only updates when none of your code is running. Rather - it goes through all of your code that it needs to run (enter frames, etc), runs them to comletion (all 100 iterations of your for loop) and then releases the screen refresh. If you want repeated action over time (like spawning particles) you need to do those on an enterFrame rather than trying to make 100 particles when the mouse moves.

    Hope that helps!
    Please use [php] or [code] tags, and mark your threads resolved 8)

  5. #5
    Senior Member
    Join Date
    Dec 2010
    Posts
    121
    Oh, I finally understand what I'm doing wrong now. Thank you.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center