如何使用 ActionScript 3 创建鼠标单击时的连续射击效果?

发布于 2024-11-03 13:09:30 字数 216 浏览 3 评论 0原文

当有人“单击并按住”时,我想一遍又一遍地触发功能,例如,在单击鼠标时每 100 毫秒触发一次。我会查找 MouseEvent.MOUSE_DOWN,而不是 MouseEvent.CLICK,然后在该事件函数上启动一个计时器,每 100 毫秒调用另一个函数。然后,在 MouseEvent.MOUSE_UP 上我将停止计时器。我走在正确的轨道上吗?当 mousedown 事件触发时,我能否获得更新的 X 和 Y 坐标?

When someone "click and holds" I want to fire off function over and over, say, once every 100ms while the mouse is clicked. Instead of MouseEvent.CLICK I would look for MouseEvent.MOUSE_DOWN and then start a timer on that event function that calls another function every 100ms. Then, on MouseEvent.MOUSE_UP I would stop the timer. Am I on the right track? Will I be able to get updated X and Y coordinates while the mousedown event is firing?

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

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

发布评论

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

评论(3

微暖i 2024-11-10 13:09:30

尝试类似的事情:就我个人而言,我不会有计时器,而只是让帧速率来处理事情发生的频率。

var _mouseDown:Boolean = false;
var _msTimer:int = 3;
var _msMax:int = _msTimer;

stage.addEventListener(MouseEvent.MOUSE_DOWN, _mousedown);
stage.addEventListener(MouseEvent.MOUSE_UP, _mouseup);
stage.addEventListener(Event.ENTER_FRAME, _handle);

function _mousedown(e:MouseEvent):void
{
    _mouseDown = true;
}

function _mouseup(e:MouseEvent):void
{
    _mouseDown = false;
}


function _handle(e:Event):void
{
    if(_mouseDown) _msTimer --;
    else _msTimer = _msMax;
    if(_msTimer < 1)
    {
        _msTimer = _msMax;

        // DO FIRE EFFECT HERE
        var xm:Number = stage.mouseX;
        var ym:Number = stage.mouseY;

        trace("do fire effect here", xm, ym);
    }
}

这是一个快速开火效果,仅供参考:)

var _mouseDown:Boolean = false;
var fire:Array = [];
var colors:Array = [0xFF0000,0xFF9900,0xFFCC00,0xFF0000,0xFF9900,0xFFCC00,0x333333];

var rect:Rectangle = new Rectangle(0,0,stage.stageWidth, stage.stageHeight);
var point:Point = new Point(0, 0);
var bmd:BitmapData = new BitmapData(stage.stageWidth, stage.stageHeight);
var filter:BlurFilter = new BlurFilter(6,6,3);

var container:MovieClip = new MovieClip();
var bitmap:Bitmap = new Bitmap(bmd);

addChild(bitmap);

stage.addEventListener(MouseEvent.MOUSE_DOWN, _mousedown);
stage.addEventListener(MouseEvent.MOUSE_UP, _mouseup);
stage.addEventListener(Event.ENTER_FRAME, _handle);

function _mousedown(e:MouseEvent):void
{
    _mouseDown = true;
}

function _mouseup(e:MouseEvent):void
{
    _mouseDown = false;
}


function _handle(e:Event):void
{
    if(_mouseDown)
    {
        // DO FIRE EFFECT HERE
        var xm:Number = stage.mouseX;
        var ym:Number = stage.mouseY;

        var k:uint = 0;
        for(k; k<10; k++)
        {
            var s:Sprite = fireThing();
            s.x = -30 + xm + Math.random()*30;
            s.y = -30 + ym + Math.random()*30;
        }
    }

    // handle fire
    var i:Array;
    for each(i in fire)
    {
        i[0].x += Math.cos(i[1].ang) * i[1].speed;
        i[0].y += Math.sin(i[1].ang) * i[1].speed;

        if(i[0].y < 0 || i[0].x < 0 || i[0].x > stage.stageWidth || i[0].y > stage.stageHeight)
        {
            if(i[0].parent) i[0].parent.removeChild(i[0]);

            var ix:uint = fire.indexOf(i);
            fire.splice(ix, 1);
        }
    }

    bmd.lock();
    bmd.draw(container);
    bmd.applyFilter(bmd, rect, point, filter);
    bmd.unlock();
}

function fireThing():Sprite
{
    var s:Sprite = new Sprite();

    s.graphics.beginFill(colors[uint(Math.random()*colors.length)]);
    s.graphics.drawCircle(0,0,1+Math.random()*2);
    s.graphics.endFill();

    var o:Object =
    {
        ang: Math.random()*Math.PI*2,
        speed: Math.random()*4
    };

    fire.push([s,o]);
    container.addChild(s);

    return s;
}

Try something similar to this: Personally I wouldn't have a timer, and just let the framerate handle how often stuff happens.

var _mouseDown:Boolean = false;
var _msTimer:int = 3;
var _msMax:int = _msTimer;

stage.addEventListener(MouseEvent.MOUSE_DOWN, _mousedown);
stage.addEventListener(MouseEvent.MOUSE_UP, _mouseup);
stage.addEventListener(Event.ENTER_FRAME, _handle);

function _mousedown(e:MouseEvent):void
{
    _mouseDown = true;
}

function _mouseup(e:MouseEvent):void
{
    _mouseDown = false;
}


function _handle(e:Event):void
{
    if(_mouseDown) _msTimer --;
    else _msTimer = _msMax;
    if(_msTimer < 1)
    {
        _msTimer = _msMax;

        // DO FIRE EFFECT HERE
        var xm:Number = stage.mouseX;
        var ym:Number = stage.mouseY;

        trace("do fire effect here", xm, ym);
    }
}

Here's a quick fire effect just for kicks :)

var _mouseDown:Boolean = false;
var fire:Array = [];
var colors:Array = [0xFF0000,0xFF9900,0xFFCC00,0xFF0000,0xFF9900,0xFFCC00,0x333333];

var rect:Rectangle = new Rectangle(0,0,stage.stageWidth, stage.stageHeight);
var point:Point = new Point(0, 0);
var bmd:BitmapData = new BitmapData(stage.stageWidth, stage.stageHeight);
var filter:BlurFilter = new BlurFilter(6,6,3);

var container:MovieClip = new MovieClip();
var bitmap:Bitmap = new Bitmap(bmd);

addChild(bitmap);

stage.addEventListener(MouseEvent.MOUSE_DOWN, _mousedown);
stage.addEventListener(MouseEvent.MOUSE_UP, _mouseup);
stage.addEventListener(Event.ENTER_FRAME, _handle);

function _mousedown(e:MouseEvent):void
{
    _mouseDown = true;
}

function _mouseup(e:MouseEvent):void
{
    _mouseDown = false;
}


function _handle(e:Event):void
{
    if(_mouseDown)
    {
        // DO FIRE EFFECT HERE
        var xm:Number = stage.mouseX;
        var ym:Number = stage.mouseY;

        var k:uint = 0;
        for(k; k<10; k++)
        {
            var s:Sprite = fireThing();
            s.x = -30 + xm + Math.random()*30;
            s.y = -30 + ym + Math.random()*30;
        }
    }

    // handle fire
    var i:Array;
    for each(i in fire)
    {
        i[0].x += Math.cos(i[1].ang) * i[1].speed;
        i[0].y += Math.sin(i[1].ang) * i[1].speed;

        if(i[0].y < 0 || i[0].x < 0 || i[0].x > stage.stageWidth || i[0].y > stage.stageHeight)
        {
            if(i[0].parent) i[0].parent.removeChild(i[0]);

            var ix:uint = fire.indexOf(i);
            fire.splice(ix, 1);
        }
    }

    bmd.lock();
    bmd.draw(container);
    bmd.applyFilter(bmd, rect, point, filter);
    bmd.unlock();
}

function fireThing():Sprite
{
    var s:Sprite = new Sprite();

    s.graphics.beginFill(colors[uint(Math.random()*colors.length)]);
    s.graphics.drawCircle(0,0,1+Math.random()*2);
    s.graphics.endFill();

    var o:Object =
    {
        ang: Math.random()*Math.PI*2,
        speed: Math.random()*4
    };

    fire.push([s,o]);
    container.addChild(s);

    return s;
}
蒲公英的约定 2024-11-10 13:09:30

你走在正确的轨道上。要在 MouseEvent.MOUSE_DOWN 上获取更新的鼠标坐标,请添加 MouseEvent.MOUSE_MOVE 侦听器,并将 X 和 Y 坐标保存到此侦听器中的某个变量。因此,当调用“伪”点击侦听器时,您将始终拥有更新的坐标。并在 MouseEvent.MOUSE_UP 上删除 MouseEvent.MOUSE_MOVE 侦听器。

您可以只使用 mouseXmouseYcontentMouseXcontentMouseY (对于 Flex 中的 UIComponent) 组件的属性。

You're on the right track. To get updated mouse coordinates also on MouseEvent.MOUSE_DOWN add MouseEvent.MOUSE_MOVE listener and save X and Y coordinates to some variable in this listener. So when calling your "pseudo" click listener, you'll always have updated coordinates. And on MouseEvent.MOUSE_UP remove MouseEvent.MOUSE_MOVE listener.
OR
You can just use mouseX, mouseY, and contentMouseX, contentMouseY (for UIComponent in Flex) properties of your component.

洒一地阳光 2024-11-10 13:09:30

将按钮的autorepeat 属性设置为true 后监听buttonDown 事件。

<mx:Button label="Click" buttonDown="trace('continuous')" autoRepeat="true"/>

Listen for buttonDown event after setting autorepeat property of button to true.

<mx:Button label="Click" buttonDown="trace('continuous')" autoRepeat="true"/>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文