2012-02-22 73 views

回答

7

1.创建坐标数组 - 这是您的路径。有许多的你其实可以接近创建阵列的方式,但结果应该类似于此:

var path:Array = [ 
    Point(0, 0), 
    Point(20, 12), 
    Point(60, 72), 
    Point(67, 118) 
]; 


2.设置nextStep()功能或类似 - 这将收集有关信息路径的下一个步骤,例如它与当前步骤之间的角度。您还需要跟踪当前的步骤,可以通过简单地将路径数据的索引存储在路径数组中来表示。总之,它可能是这样的:

var currentStep:int = 0; 

function nextStep():Object 
{ 
    // Object to return. 
    var out:Object = { 
     hasDestination: false, 
     destination: null, 
     radians: 0 
    }; 


    var current:Point = path[currentStep]; 

    // Check that you're not on the last step first. 
    if(currentStep != path.length - 1) 
    { 
     currentStep ++; 

     var next:Point = path[currentStep + 1]; 
     var t:Point = next.subtract(current); 

     out.nextDestination = true; 
     out.destination = next; 
     out.radians = Math.atan2(t.y, t.x); 
    } 

    return out; 
} 



3.使用上述信息移动 - 返回的对象从nextStep()可以用来改变您所选择的DisplayObject的位置和旋转。

假设entity是你DisplayObject

var stepInfo:Object = nextStep(); 

if(stepInfo.hasDestination) 
{ 
    entity.rotation = stepInfo.radians * 180/Math.PI; 
    entity.x = stepInfo.destination.x; 
    entity.y = stepInfo.destination.y; 
} 
else trace("End of path reached."); 


4。整理(可选) - 考虑创建自己的类是nextStep()为tidyness,例如结果:

public class StepInfo 
{ 
    public var hasDestination:Boolean = false; 
    public var destination:Point; 
    public var radians:Number = 0; 
} 

我甚至建议把以上所有入Path类,所以你可以简单地做的东西像:

var path:Path = new Path(); 
path.generate(); // create this yourself, generates the path array. 

var step:StepInfo = path.nextStep(); 

trace(path.currentStep); 

希望这有助于。

+2

很好的答案,你可能永远不会得到一个绿色的检查,所以+1 – danii 2012-03-23 18:49:09

1

你必须把你的路径作为一个数学函数,如(x,y) = f(t)。在这种情况下,只需将新动画片段移动到(x,y)并使用例如Math.atan2将其旋转即可。

在你的情况下,不清楚along a path (other movieclip)的含义。例如,它是静态的还是动态的?

这样做如果你有一个静态路径的黑客方式是使用一个空的精灵,沿着这个路径补间100帧例如。这样的功能(x,y) = f(t)

mc.gotoAndStop(int((t-minTime)/(maxTime-minTime))); 
var xToAddFootsteps:Number = mc.dummy.x; 
var yToAddFootsteps:Number = mc.dummy.y; 
var rotationOfFootsteps:Number = Math.atan2(xToAddFootsteps, yToAddFootsteps); 

前提是路径动画片段称为mc和空精灵内部被称为dummy