如何定义一个动画片段相对于另一个动画片段的位置
How to define the position of a movieclip in relation to that of another of movieclip
我正在尝试使动画片段跟随自定义鼠标指针(这是一个动画片段),但始终停留在定义的位置 (根据坐标),距离较远从鼠标指针缓动到鼠标指针所在的位置。下面是代码:
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",但它似乎导致球体的轻松移动受到阻碍,所以不知道我使用的定位代码是否错误
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.
}
}
你看,一旦你使用增量赋值运算符之一(-=
、+=
、/=
、*=
),紧随其后的是常规 assignment 运算符显然会覆盖它之前的任何值。您了解 actionscript 由计算机获取 "read"(这可能是错误的措辞,但您明白我的意思)并且它从上到下读取每个块(花括号组 { }
内的一个区域)。所以第一次调用你的 follow
方法时,它按顺序做了 4 件事:
- 增加计数器
- 评估如果
mouseCounter >= mouseDelay
- 增量
orb_mc
x/y坐标
- 分配
orb_mc
x/y坐标到最终目的地
它在第一次读取 follow
块时执行此操作。这就是为什么我的答案中可接受的代码位都做了两件事:
- 它们会增加 x/y 坐标(不要将它们设置为最终值)
- 它们会根据可变因素 (
speed
)
很高兴它起作用了!
我正在尝试使动画片段跟随自定义鼠标指针(这是一个动画片段),但始终停留在定义的位置 (根据坐标),距离较远从鼠标指针缓动到鼠标指针所在的位置。下面是代码:
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",但它似乎导致球体的轻松移动受到阻碍,所以不知道我使用的定位代码是否错误
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.
}
}
你看,一旦你使用增量赋值运算符之一(-=
、+=
、/=
、*=
),紧随其后的是常规 assignment 运算符显然会覆盖它之前的任何值。您了解 actionscript 由计算机获取 "read"(这可能是错误的措辞,但您明白我的意思)并且它从上到下读取每个块(花括号组 { }
内的一个区域)。所以第一次调用你的 follow
方法时,它按顺序做了 4 件事:
- 增加计数器
- 评估如果
mouseCounter >= mouseDelay
- 增量
orb_mc
x/y坐标 - 分配
orb_mc
x/y坐标到最终目的地
它在第一次读取 follow
块时执行此操作。这就是为什么我的答案中可接受的代码位都做了两件事:
- 它们会增加 x/y 坐标(不要将它们设置为最终值)
- 它们会根据可变因素 (
speed
)
很高兴它起作用了!