我正在尝试制作一个动画片段跟随自定义鼠标指针(这是一个动画片段),但始终保持在定义的位置(就坐标而言),距鼠标指针一段距离放宽到鼠标指针的位置。以下是代码:
import flash.display.MovieClip;
import flash.events.Event;
Mouse.hide();
var mouseCounter:int = 0;
var mouseDelay:int = 5;// how many frames the mouse must stay still before the follow code is run.
var speed:Number = 5;
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)
{
orb_mc.x -= (orb_mc.x - mouseX) / speed;
orb_mc.y -= (orb_mc.y - mouseY) / speed;
orb_mc.x = mouseX + 46.5;
orb_mc.y = mouseY +50.95;
}
}
最后两行代码(26& 27)是我用来定义orb_mc相对于自定义鼠标指针的位置,它是" wand"然而它似乎导致了orb的易移动受到了阻碍,所以不知道我使用的定位代码是错误的
答案 0 :(得分:1)
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)
{
// Do this:
orb_mc.x -= (orb_mc.x - mouseX + 46.5) / speed;
orb_mc.y -= (orb_mc.y - mouseY + 50.95) / speed;
// OR this:
//orb_mc.x = orb_mc.x - (orb_mc.x - mouseX + 46.5) / speed;
//orb_mc.y = orb_mc.y - (orb_mc.y - mouseY + 50.95) / speed;
// but not both.
}
}
您可以看到,一旦您使用其中一个增量分配运算符(-=
,+=
,/=
,*=
),就会立即使用常规赋值运算符显然会覆盖它之前的任何值。你理解,动作脚本被计算机“读取”(这可能是错误的措辞,但你得到了我的漂移),它从上到下读取每个块(花括号集{ }
内的一个区域)。因此,第一次调用follow
方法时,它按顺序执行4项操作:
mouseCounter >= mouseDelay
orb_mc
x / y coords orb_mc
x / y coords分配到最终目的地 它是在第一次读取follow
块时执行此操作。这就是为什么我的答案中可接受的代码都做了两件事:
speed
)很高兴它正在工作!