2011-11-02 67 views
-2

我試圖創建可移動的實例,這是可拖動的,並且有一條連接它們的線路,它們會自動更新。從一個實例到另一個實例不工作行爲腳本3.0

問題是:原始行隨機出現,並且在實例(mc1,mc2)被拖動時不會更新。 這是到目前爲止,我已經得到了代碼:

mc1.addEventListener(MouseEvent.MOUSE_DOWN, function (e:MouseEvent):void 
{ 
    e.currentTarget.startDrag(); 
}); 

mc1.addEventListener(MouseEvent.MOUSE_UP, function (e:MouseEvent):void 
{ 
    e.currentTarget.stopDrag(); 
}); 
mc2.addEventListener(MouseEvent.MOUSE_DOWN, function (e:MouseEvent):void 
{ 
    e.currentTarget.startDrag(); 
}); 

mc2.addEventListener(MouseEvent.MOUSE_UP, function (e:MouseEvent):void 
{ 
    e.currentTarget.stopDrag(); 
}); 

var mc:MovieClip = new MovieClip(); 
mc.graphics.beginFill(0x000000); 
mc.graphics.lineStyle(2,0x000000); 

//start drawing the line 
mc.graphics.moveTo(mc1.x,mc1.y); 
mc.graphics.lineTo(mc2.x,mc2.y); 
mc.graphics.endFill(); 

//Position your new movie clip 
addChild(mc); 

誰能告訴我去哪裏我聽錯了?

非常感謝您的幫助!

回答

0

你需要做的是捕捉用戶鼠標的移動,因爲他正在拖動MovieClip。

首先創建一個isDragging變量var isDragging:Boolean = false;,並在鼠標向下時將其設置爲true,並在鼠標向上時將其設置爲false。

接着一個的MouseEvent.MOUSE_MOVE事件監聽器添加到行時間軸

addEventListener(MouseEvent.MOUSE_MOVE, function (e:MouseEvent):void 
{ 
    if (isDragging) { 
     drawConnectiveLine(mc1, mc2); 
    } 
    return; 
}); 

方法drawConnectiveLine將更新線矢量形狀移動到的movieclip的{X,Y}。

function drawConnectiveLine(d:DisplayObject, d2:DisplayObject):void { 
    mc.graphics.clear(); 
    mc.graphics.lineStyle(2,0); 
    mc.graphics.moveTo(d.x,d.y); 
    mc.graphics.lineTo(d2.x,d2.y); 
} 

修改您的預先存在的腳本:

var isDragging:Boolean = false; 
var mc:MovieClip = new MovieClip(); 
mc.graphics.lineStyle(2,0); 
mc.graphics.moveTo(mc1.x,mc1.y); 
mc.graphics.lineTo(mc2.x,mc2.y); 
addChild(mc); 

mc1.addEventListener(MouseEvent.MOUSE_DOWN, function (e:MouseEvent):void 
{ 
    isDragging = true; 
    e.currentTarget.startDrag(); 
}); 

mc1.addEventListener(MouseEvent.MOUSE_UP, function (e:MouseEvent):void 
{ 
    isDragging = false; 
    e.currentTarget.stopDrag(); 
}); 

mc2.addEventListener(MouseEvent.MOUSE_DOWN, function (e:MouseEvent):void 
{ 
    isDragging = true; 
    e.currentTarget.startDrag(); 
}); 
mc2.addEventListener(MouseEvent.MOUSE_UP, function (e:MouseEvent):void 
{ 
    isDragging = false; 
    e.currentTarget.stopDrag(); 
}); 
addEventListener(MouseEvent.MOUSE_MOVE, function (e:MouseEvent):void 
{ 
    if (isDragging) { 
     mc.graphics.clear(); 
     mc.graphics.lineStyle(2,0); 
     mc.graphics.moveTo(mc1.x,mc1.y); 
     mc.graphics.lineTo(mc2.x,mc2.y); 
    } 
}); 
+0

我應該將所有代碼還是隻是「修改現有代碼」下的代碼?因爲每當我做他們任何一個時,我都會遇到很多問題。你能告訴我哪個代碼放在哪裏? –

+0

現在我所有的問題都是「訪問未定義的屬性mc」。我唯一擁有的代碼是庫對象「mc」和它的兩個實例stage(mc1,mc2)。你知道爲什麼會這樣嗎?你有沒有機會發布你製作的實際fla文件? –

+0

已更新,包含名爲'mc'的新MovieClip –

0

以下的區塊可能會對您有所幫助。

mc.graphics.clear(); 
//mc.graphics.beginFill(0x000000); //no need fills 
mc.graphics.lineStyle(2,0); 

//start drawing the line 
mc.graphics.moveTo(mc1.x,mc1.y); 
mc.graphics.lineTo(mc2.x,mc2.y); 
//mc.graphics.endFill(); //no need to end it. 
+0

怎麼了自動更新? –

+0

,它仍然不會從一個實例轉到另一個實例... –

相關問題