(AS3) MovieClip 的运动路径作为舞台上的 Shape

(AS3) MovieClip' s motion path as Shape on stage

我正在为学校项目制作游戏。这个概念很简单,你需要固定电路线(矩形形式的路径),以便电流可以运行通过电路并点亮灯泡。

我正在尝试找到一种方法来制作影片剪辑在电线中移动。我看过很多教程,其中说明了运动路径的坐标和角度,但我想做到这一点,以便影片剪辑将自动遵循舞台上存在的形状路径,因此即使路径发生变化(对于不同级别),影片剪辑仍将遵循该路径。目前,我所能做的就是在跟踪路径的影片剪辑中创建一个预定义的引导路径。

后续问题: 还有一种方法可以检测形状路径是否完整?系统将检查电线是否相互连接。

如果您想查看您的 'shape' 是否完整: 您可以创建一个布尔变量向量(每个变量都表示您的电线节点 - 连接时将变量设置为真),并且您需要一个函数来检查向量中的所有变量是否为真(带循环),即意味着你的形状是完整的。希望这个想法有所帮助!

你应该在移动你想要移动的东西之前进行元数据计算,这样你首先计算你的谜题是否已经解决,一旦解决,或者一旦你确定了你的可移动物体应该停止的点并显示错误,然后你从简单的部分中创建一条路径,然后让你的对象一个接一个地移动直到最后一点,然后显示结果。这是一般的想法。按部分移动对象的简单代码如下所示:

var sections:Vector.<Point>; // we need x&y sequence. Fill this prior to launching the routine
var position:int=0; // where are we now
var velocity:int=8; // pixels per frame, adjust as needed
movable.addEventListener(Event.ENTER_FRAME,moveABit);
function moveABit(e:Event):void {
    var nextPoint:Point=sections[position];
    var do:DisplayObject=e.target as DisplayObject; // what we are moving
    var here:Point=new Point(do.x,do.y);
    if (Point.distance(here,nextPoint)<velocity) {
        // this means we are too close to interpolate, just place
        do.x=nextPoint.x;
        do.y=nextPoint.y;
        position++;
        if (position>=sections.length) {
            // movement finished
            // TODO make your final animation
            do.removeEventListener(Event.ENTER_FRAME,moveABit); // stop moving
        }
    } else {
        // interpolate movement
        var angle:Number=Math.atan2(nextPoint.y-here.y,nextPoint.x-here.x);
        do.x+=Math.cos(angle)*velocity;
        do.y+=Math.sin(angle)*velocity; 
        // if the object will move to wrong direction, fix this code!
    }
}