第一个
如果您的意思是“跟随其路径”,即该对象跟随另一个对象,那么只需这样做
obj2.x = obj1.x;
obj2.y = obj1.y;
遵循确切的坐标。如果你想拉开它们之间的距离,那么
obj2.x = obj1.x + dx;
obj2.y = obj1.y + dy;
根据自己的意愿选择 dx 和 dy。
第二次
如果你想做一个应用程序,你可以“画一个箭头”或“画一个路径”,然后一个对象应该跟随它,那么你可以尝试存储鼠标的坐标,同时“画箭头” ",然后将您想要的对象捕捉到这些坐标。
var coordinates:Array = [];
stage.addEventListener("mouseDown", md);
function md(evt:*):void
{
//empty the coordinates
coordinates = [];
//add listener when mouse is released
stage.addEventListener("mouseUp", mu);
//add a listener for enterframe to record the mouse's motion
addEventListener("enterFrame", recordMouse);
}
function mu(evt:*):void
{
stage.removeEventListener("mouseUp", mu);
removeEventListener("enterFrame", recordMouse);
//snap the object to the drawn line and play it
addEventListener("enterFrame", playRecording);
}
function recordMouse(evt:*):void
{
coordinates.push(new Point(stage.mouseX, stage.mouseY));
}
function playRecording(evt:*):void
{
//snap object to the recorded coordinates
myObject.x = coordinates[0].x;
myObject.y = coordinates[0].y;
//delete first element of array
coordinates.splice(0, 1);
//stop playing if there are no more points
if(coordinates.length == 0) removeEventListener("enterFrame", playRecording);
}
在舞台上放置一个影片剪辑并将其命名为 myObject。然后添加代码并编译 swf。
此外,在“记录”坐标的同时,您还可以绘制一些线条。
把 md 函数改成这样:
function md(evt:*):void
{
//empty the coordinates
coordinates = [];
//add listener when mouse is released
stage.addEventListener("mouseUp", mu);
//add a listener for enterframe to record the mouse's motion
addEventListener("enterFrame", recordMouse);
//clear graphics, and initialize line
with(graphics) clear(), lineStyle(1, 0xff0000), moveTo(stage.mouseX, stage.mouseY);
}
并将鼠标记录到此。
function recordMouse(evt:*):void
{
coordinates.push(new Point(stage.mouseX, stage.mouseY));
//draw the line
with(graphics) lineTo(stage.mouseX, stage.mouseY);
}
第三
如果您想遵循预先绘制的路线,那么您有多种选择,具体取决于您的任务。但一切都取决于您究竟想如何“捕捉”您的对象。