2012-02-22 77 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