AS3 按住鼠标可提高速度/功率

发布于 2024-12-03 23:36:29 字数 512 浏览 2 评论 0原文

这就是我想要实现的目标;

单击影片剪辑 (cannon_mc) 正在射击 (ball_mc)

鼠标按下的时间越长,发射球的速度就会增加。 我向你提出的问题是;

实现这一目标最有效的方法是什么? 用计时器或类似的东西;

    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
            }

        }

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
            }

        }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

誰認得朕 2024-12-10 23:36:29

MOUSE_DOWN 事件仅触发一次。要获得您想要的效果,您需要 MOUSE_DOWN 和 MOUSE_UP 事件处理程序的组合。

您可以在 MOUSE_DOWN 事件中将变量设置为 true 以及来自 flash.utils.getTimer() 的当前时间戳

然后在 MOUSE_UP 上,如果您在 MOUSE_DOWN 中设置的变量为 true,则计算经过的时间并相应地设置功率。

示例:

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
        // .......
    }
}

您还可以添加 ENTER_FRAME 事件并为电量计或其他视觉效果添加动画

更新
正如 The_asMan 所指出的,如果在舞台外拖动并释放鼠标,MOUSE_UP 事件将不会触发。要处理这种情况,请为 MOUSE_LEAVE 事件添加事件侦听器,并将回调作为 buttonReleased 函数的副本,但它采用 Event 对象:

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);

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);
温柔戏命师 2024-12-10 23:36:29

(非常短的伪代码)
编写一些事件处理程序:

  • onMouseDown:设置标志 _mouseDown,将 power 设置为零
  • onFrame:if (_mouseDown) power++;
  • onMouseUp:清除标志 _mouseDown 并以累积的能量发射球

帧速率独立版本:

  • onMouseDown: _loadStart = getTimer(); _mouseDown = true; _power = 0;
  • onFrame: if (_mouseDown) delta = getTimer() - _loadStart; _power += delta;
  • onMouseUp:用_power射球,_mouseDown = false;

(in very short pseudocode)
Write some event handlers:

  • onMouseDown: sets flag _mouseDown, set power to zero
  • onFrame: if (_mouseDown) power++;
  • onMouseUp: clears flag _mouseDown and fires ball with accumulated power

Framerate independent version:

  • onMouseDown: _loadStart = getTimer(); _mouseDown = true; _power = 0;
  • onFrame: if (_mouseDown) delta = getTimer() - _loadStart; _power += delta;
  • onMouseUp: shot the ball with _power, _mouseDown = false;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文