在as3中2秒后制作一个movieclip跟随鼠标
Make a movieclip follow mouse after 2 seconds in as3
我正在尝试让动画片段 "orb_mc" 在延迟 2 秒后沿着鼠标指针的相同坐标移动,也就是说在球体移动到之前应该有 2 秒的延迟与指针相同的位置。下面是我的代码——除了延迟之外一切正常。
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;
}
在这种情况下,我不会费心使用计时器。根据您对我评论的回复,我了解到您只是希望 orb_mc
平稳移动到鼠标停止移动的位置。当然,您可以简单地说出 "if mouse isn't moving, send orb to mouse" 的效果,但这样做的问题是您会得到这些不稳定的动作,因为会有这些小时刻鼠标移动没有记录(尤其是当鼠标速度变慢时向下开始向另一个方向移动)。这就是为什么要添加某种延迟。所以我处理这个的方法基本上是让一个计数器在每一帧都上升,并在鼠标移动时重置为 0。
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;
}
}
我正在尝试让动画片段 "orb_mc" 在延迟 2 秒后沿着鼠标指针的相同坐标移动,也就是说在球体移动到之前应该有 2 秒的延迟与指针相同的位置。下面是我的代码——除了延迟之外一切正常。
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;
}
在这种情况下,我不会费心使用计时器。根据您对我评论的回复,我了解到您只是希望 orb_mc
平稳移动到鼠标停止移动的位置。当然,您可以简单地说出 "if mouse isn't moving, send orb to mouse" 的效果,但这样做的问题是您会得到这些不稳定的动作,因为会有这些小时刻鼠标移动没有记录(尤其是当鼠标速度变慢时向下开始向另一个方向移动)。这就是为什么要添加某种延迟。所以我处理这个的方法基本上是让一个计数器在每一帧都上升,并在鼠标移动时重置为 0。
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;
}
}