Search code examples
performanceactionscript-3timermousedown

AS3 Hold mouseDown to increase speed/power


this is what i'm trying to accomplish;

With a click on a movieclip (cannon_mc) a shot is being fired (ball_mc)

The longer mouse is down, the speed of wich the ball is fired with should increase. My question to you is;

What is the most efficient way to accomplish this? With a timer or something like this;

    var isMouseDown:Boolean = false;
    var speed= 10;    

    myCannon.addEventListener(MouseEvent.MOUSE_DOWN,buttonPressed);

        function buttonPressed(event:MouseEvent){
            //trace("down");
            isMouseDown == true;

            if(isMouseDown == false)
            {
                speed == +1
            }

        }

Solution

  • The MOUSE_DOWN event is only fired once. To get the effect you want you need the combo of MOUSE_DOWN and MOUSE_UP Event Handlers.

    You can set a variable to true in the MOUSE_DOWN event alongwith the current timestamp from flash.utils.getTimer()

    Then on MOUSE_UP, if the variable you set in MOUSE_DOWN is true, you compute the time elapsed and set the power accordingly.

    Example:

    var isMouseDown:Boolean = false;
    var mouseDownBegin:int;
    var speed = 10; 
    var speed_inc = 2; // give it in per second
    var speed_max = 100; // max speed possible
    
    // add event handlers
    myCannon.addEventListener(MouseEvent.MOUSE_DOWN, buttonPressed);
    myCannon.addEventListener(MouseEvent.MOUSE_UP, buttonReleased);
    
    function buttonPressed(event:MouseEvent){
        isMouseDown = true;
        mouseDownBegin = flash.utils.getTimer();
    }
    
    function buttonReleased(event:MouseEvent){
        if(isMouseDown == true){
            // get time between press and release
            var timeElapsed = flash.utils.getTimer() - mouseDownBegin;
    
            // reset isMouseDown
            isMouseDown = false;
    
            // compute speed
            speed += int(Math.floor(speed_inc * (timeElapsed / 1000.0)));
            speed = Math.min(speed, speed_max);
    
            // code to fire ball with new speed
            // .......
        }
    }
    

    You can also add an ENTER_FRAME event and animate a power gauge or something for visual effect

    Update
    As pointed to by The_asMan, MOUSE_UP event will not fire if the mouse is dragged and released Outside the stage. To handle this case add and event listener for MOUSE_LEAVE event with the callback as the copy of buttonReleased function but which takes an Event object:

    function buttonReleasedOutsideStage(event:Event){
        if(isMouseDown == true){
            // get time between press and release
            var timeElapsed = flash.utils.getTimer() - mouseDownBegin;
    
            // reset isMouseDown
            isMouseDown = false;
    
            // compute speed
            speed += int(Math.floor(speed_inc * (timeElapsed / 1000.0)));
            speed = Math.min(speed, speed_max);
    
            // code to fire ball with new speed
            // .......
        }
    }
    stage.addEventListener(Event.MOUSE_LEAVE, buttonReleasedOutsideStage);