我正在創建數組以在單擊時在舞臺上添加多個孩子。當小孩彼此接近時,我會添加一條線將兩者粘合在一起。AS3定位在陣列中糾結
問題是,X和Y的位置在我的數組中亂了起來,那裏的線條沒有移動到我孩子的正確協調位置。
編輯: 原始問題已修復;但需要限制時的點可以被添加到陣列(CARRAY,hArray,xArray,oArray)中的一個,基於化學規則:
更新代碼:
package
{
import flash.display.*;
import flash.events.*;
public class main extends MovieClip
{
public static var cArray:Array = new Array();
public static var hArray:Array = new Array();
public static var xArray:Array = new Array();
public static var oArray:Array = new Array();
var bondArray:Shape=new Shape();
public function main()
{
addEventListener(MouseEvent.MOUSE_UP,stopAllDrag);
add_c_mol.addEventListener(MouseEvent.MOUSE_DOWN, clickCmol);
add_h_mol.addEventListener(MouseEvent.MOUSE_DOWN, clickHmol);
add_x_mol.addEventListener(MouseEvent.MOUSE_DOWN, clickXmol);
add_o_mol.addEventListener(MouseEvent.MOUSE_DOWN, clickOmol);
addEventListener(Event.ENTER_FRAME,checkBond);
}
private function stopAllDrag(e:MouseEvent):void
{
stopDrag();
}
private function clickCmol(e:MouseEvent)
{
cArray.push(new C_mol());
cArray[cArray.length - 1].x=112;
cArray[cArray.length - 1].y=197;
addChild(cArray[cArray.length - 1]);
cArray[cArray.length - 1].addEventListener(MouseEvent.MOUSE_DOWN,dragCmol);
cArray[cArray.length - 1].addEventListener(Event.ENTER_FRAME,trashC);
}
private function dragCmol(e:MouseEvent):void
{
e.target.startDrag();
}
private function trashC (e:Event):void
{
if(e.target.hitTestObject(trash))
{
e.target.visible=false;
}
if(e.target.y<180)
{
e.target.y=180;
stopDrag();
}
}
private function clickHmol(e:MouseEvent)
{
hArray.push(new H_mol());
hArray[hArray.length - 1].x=280;
hArray[hArray.length - 1].y=174;
addChild(hArray[hArray.length - 1]);
hArray[hArray.length - 1].addEventListener(MouseEvent.MOUSE_DOWN,dragHmol);
hArray[hArray.length - 1].addEventListener(Event.ENTER_FRAME,trashH);
}
private function dragHmol(e:MouseEvent):void
{
e.target.startDrag();
}
private function trashH (e:Event):void
{
if(e.target.hitTestObject(trash))
{
e.target.visible=false;
}
if(e.target.y<152)
{
e.target.y=152;
stopDrag();
}
}
private function clickXmol(e:MouseEvent)
{
xArray.push(new X_mol());
xArray[xArray.length - 1].x=474;
xArray[xArray.length - 1].y=201;
addChild(xArray[xArray.length - 1]);
xArray[xArray.length - 1].addEventListener(MouseEvent.MOUSE_DOWN,dragXmol);
xArray[xArray.length - 1].addEventListener(Event.ENTER_FRAME,trashX);
}
private function dragXmol(e:MouseEvent):void
{
e.target.startDrag();
}
private function trashX (e:Event):void
{
if(e.target.hitTestObject(trash))
{
e.target.visible=false;
}
if(e.target.y<186)
{
e.target.y=186;
stopDrag();
}
}
private function clickOmol(e:MouseEvent)
{
oArray.push(new O_mol());
oArray[oArray.length - 1].x=648;
oArray[oArray.length - 1].y=189;
addChild(oArray[oArray.length - 1]);
oArray[oArray.length - 1].addEventListener(MouseEvent.MOUSE_DOWN,dragOmol);
oArray[oArray.length - 1].addEventListener(Event.ENTER_FRAME,trashO);
}
private function dragOmol(e:MouseEvent):void
{
e.target.startDrag();
}
private function trashO (e:Event):void
{
if(e.target.hitTestObject(trash))
{
e.target.visible=false;
}
if(e.target.y<187)
{
e.target.y=187;
stopDrag();
}
}
private function checkBond(e:Event):void
{
bondArray.graphics.clear();
for (var i:uint = 0; i < main.cArray.length; i++)
{
for (var j:uint = 0; j < main.hArray.length; j++)
{
if (Math.pow(Math.abs(main.cArray[i].x - main.hArray[j].x),2)+Math.pow(Math.abs(main.cArray[i].y - main.hArray[j].y),2) < Math.pow(100,2))
{
//trace("yes");
bondArray.graphics.lineStyle(8,00000, 1);
bondArray.graphics.moveTo(main.cArray[i].x,main.cArray[i].y);
bondArray.graphics.lineTo(main.hArray[j].x,main.hArray[j].y);
addChildAt(bondArray,0);
}
else
{
//bondArray.graphics.clear();
}
}
}
}
}
}
感謝.. 1更多的問題,如果我只希望有隻1連接到hArray線什麼。我應該如何使用布爾值? – user3251836
@ user3251836,我不太明白。你能在油漆上畫一個簡單的圖像來展示你想要做的事嗎? (確保標記哪些點/線將與代碼中的變量相關聯) – Jono
image url:http://oi61.tinypic.com/wgvz80.jpg – user3251836