2014-01-20 36 views
0

我有多邊形說(六角形有6條線)這個六角形連接從中心與6點這使得6個三角形 我需要當移動任何點(導致移動三角形),其他點移動像這個點我的意思是如果左點動解除其他點向左移動等如何根據一點的移動移動多邊形點?

我想這樣ptcP1.x和ptcP1.y點我移動它的其他點移動代碼依賴於ptcP1運動需要注意的是,這個公式做工精細在方形,,放在五角和六..等這個方程有效,以便任何人可以幫助我

function button1_triggeredHandler(event:Event):void 
{ 

    mode="mode2"; 
    //trace(list.selectedIndex); 

    if(list.selectedIndex==1) 
    { 
    DrawSqure.ptcP1.x = Math.random() + 50; 
    DrawSqure.ptcP1.y = Math.random() + 50; 


    DrawSqure.ptcP2.y = 50-DrawSqure.ptcP1.x; 
    DrawSqure.ptcP2.x = DrawSqure.ptcP1.y; 

    DrawSqure.ptcP3.x = 50-DrawSqure.ptcP1.y; 
    DrawSqure.ptcP3.y = DrawSqure.ptcP1.x; 


    DrawSqure.ptcP4.x = 50-DrawSqure.ptcP1.x; 
    DrawSqure.ptcP4.y = 50-DrawSqure.ptcP1.y; 
} 
+0

爲什麼不保存該對象的所有點的數組,並且當某個點正在移動時 - 將新位置(平移)應用於所有點? –

+0

你是在時間軸上做這個嗎? @Creative Magic的解決方案也可以工作。 –

回答

0

正如評論指出,存儲VERT冰點/點放入容器(ArrayVector),然後在移動時調整這些位置是最好的方法。下面是如何可能的工作的例子:

//setup array or vector of vertices 
var polygonVertices:Array = [DrawPolygon.ptcP1, DrawPolygon.ptcP2, DrawPolygon.ptcP3, DrawPolygon.ptcP4]; 

這種方法將採取所有的頂點和應用翻譯:

//function for adjusting all the vertices based on the distance you pass 
function moveShape(vertices:Array, dx:Number, dy:Number) { 
    var i:int; 
    for (; i < vertices.length; i++) { 
     vertices[i].x += dx; 
     vertices[i].y += dy; 
    } 
} 

然後你只需要知道你的距離X & Y在您的形狀已移動,你可以撥打moveShape(polygonVertices, 100, 100);

我插入100,100作爲距離參數爲例,但這應該會給你你正在尋找的結果。