When someone "click and holds" I want to fire off function over and over, say, once every 100ms whi开发者_StackOverflowle 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?
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;
}
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.
Listen for buttonDown event after setting autorepeat property of button to true.
<mx:Button label="Click" buttonDown="trace('continuous')" autoRepeat="true"/>
精彩评论