开发者

Global MouseMove problem in AS3

开发者 https://www.devze.com 2023-03-02 10:56 出处:网络
I need to register mouse movement for a screensaver like feature. The code below works fine but the mou开发者_StackOverflow中文版se move event is not triggered when the mouse is over a movieclip that

I need to register mouse movement for a screensaver like feature. The code below works fine but the mou开发者_StackOverflow中文版se move event is not triggered when the mouse is over a movieclip that has had an external swf loaded into it with URLRequest. Is there any way around this without modifying the code in the external swf?

stage.addEventListener(MouseEvent.MOUSE_MOVE,function(e){
    lastMoveTime=getTimer()
    trace(stage.mouseX)
})

UPDATE:

I need interactivity to be retained in both the parent and child swfs.

The child swf is AS2.

Here is the code for loading the AS2 swf:

var sendPane = new Loader(); 
var url:URLRequest = new URLRequest("info.swf"); 
sendPane.load(url); 
addChild(sendPane);


set the loading container to loader.mouseChildren = false; or loader.mouseEnabled = false;

Or you can add a Sprite or Movieclip on top of the stage with sprite.alpha = 0 and draw it as large as the stage. then add the mouse listener to this Sprite.

var s:Sprite = new Sprite();
s.graphics.beginFill(0, 0);
s.graphics.drawRectangle(0, 0, stage.stageWidth, stage.stageHeight);
s.endFill();
s.addEventListener(MouseEvent.MOUSE_MOVE, listener);

addChild(s);

Booth versions this stops the loaded swf from reacting to the mouse. But you said screensaver, so I thought this is okay.


Instead of listening to MOUSE_MOVE events, you could rewrite your code to use a Timer with a delay of 10msecs and listen to the TIMER event. In the handler check if the mousepos has changed since last call. This is not a very nice solution, but the combination of AS3 and AS2 movies makes it necessary.

var lastPos:Point = new Point(stage.mouseX, stage.mouseY);
var lastMoveTime:int;
var timer:Timer = new Timer(10, 0);
timer.addEventListener(TimerEvent.TIMER, handleTimer);
timer.start();

private function handleTimer(e:TimerEvent):void
{
    if (lastPos.x!=stage.mouseX || lastPos.y!=stage.mouseY) lastMoveTime = getTimer();
    lastPos = new Point(stage.mouseX, stage.mouseY);
}
0

精彩评论

暂无评论...
验证码 换一张
取 消