Reputation: 41
I'm trying to get a movieclip "orb_mc" to move along the same ordinates of the mouse pointer after a delay of 2 seconds that is to say there should be a 2 seconds delay before the orb moves to the same position as the pointer. Below is my code- everything is working fine except the delay.
import flash.events.Event;
Mouse.hide();
stage.addEventListener(MouseEvent.MOUSE_MOVE, moveThatMouse);
function moveThatMouse(evt:MouseEvent):void {
wand.x = stage.mouseX;
wand.y = stage.mouseY;
evt.updateAfterEvent();
}
var myTimer:Timer = new Timer(2000);
var speed:Number=10;
orb_mc.addEventListener(Event.ENTER_FRAME,follow);
function follow(e:Event):void{
myTimer.start();
orb_mc.x -= (orb_mc.x - mouseX) / speed;
orb_mc.x = orb_mc.x +2;
orb_mc.y -= (orb_mc.y - mouseY) / speed;
orb_mc.y = orb_mc.y +3;
}
Upvotes: 1
Views: 692
Reputation: 2008
I wouldn't bother using a timer in this case. Per your response to my comments, I understand that you simply want the orb_mc
to travel smoothly to the point at which the mouse stops moving. Of course you could simply say something to the effect of "if mouse isn't moving, send orb to mouse" but the problem with that is you will get these jerky motions because there will be these little moments when the mouse movement doesn't register (especially when the mouse is slowing down to begin moving the other direction). This is why you want to add some sort of delay. So the way I would approach this is to basically have a counter go up each frame, and be reset to 0 whenever the mouse moves.
import flash.display.MovieClip;
import flash.events.Event;
Mouse.hide();
var mouseCounter: int = 0;
var mouseDelay: int = 20; // how many frames the mouse must stay still before the follow code is run.
// var myTimer:Timer = new Timer(2000); won't need this with my method.
var speed:Number=10;
stage.addEventListener(MouseEvent.MOUSE_MOVE, mouseMove);
stage.addEventListener(Event.ENTER_FRAME,follow);
// set counter back to zero whenever the mouse is moved.
function mouseMove(e:MouseEvent):void
{
wand.x = stage.mouseX;
wand.y = stage.mouseY;
e.updateAfterEvent();
mouseCounter = 0;
}
function follow(e:Event):void{
// increment the counter each frame
mouseCounter++;
// now run the follow block if the mouse has been still for enough frames.
if (mouseCounter >= mouseDelay)
{
//myTimer.start(); won't need this for my method.
orb_mc.x -= (orb_mc.x - mouseX) / speed;
orb_mc.x += 2;
orb_mc.y -= (orb_mc.y - mouseY) / speed;
orb_mc.y += 3;
}
}
Upvotes: 1