我正在使用 AS3。但我使用什么语言并不重要。我只是在寻找如何解决这个问题的逻辑。
我已经创建了一条线,用图形类绘制了一条线的起点和终点。现在我再画一个圆圈。我希望那个圆圈穿过这条线。
我可以通过知道坐标平面中线的起点和终点之间的点来做到这一点。
例如.. (0,0) 和 (20,20)
我可以通过这个来分配对象的值来遍历这条线......
object.x = 5;
object.y = 5;
object.x = 10;
object.y = 10;
and so on and so forth..
我希望我的对象圆从起点到终点来回移动,然后在遍历线时再次回到起点。
有什么公式可以帮助我吗?我已经尝试扣除和添加斜率来穿越这条线,但它只在斜率为正时才有效。
注意:在 Flash 中,坐标系与通常的笛卡尔平面不同。第四象限都是积极的。
这是我目前的逻辑:
var speed:int = 10;
function makeItMove(packetNumber:int):void{
var xcenter:int = xCenterPt[packetNumber];
var ycenter:int = yCenterPt[packetNumber];
var xfirst:int = xFirstPt[packetNumber];
var yfirst:int = yFirstPt[packetNumber];
var xsecond:int = xSecondPt[packetNumber];
var ysecond:int = ySecondPt[packetNumber];
var switchPath:Boolean = switchPathArr[packetNumber];
var slope:int = 0;
if((xsecond-xfirst) != 0){
slope = ((ysecond - yfirst)/(xsecond - xfirst)) * speed;
}
if(slope<0){
//slope = ((ysecond - yfirst)/(xsecond - xfirst)) * speed;
slope = slope * -1;
}
//else if(slope)
//trace("PacketNumber: "+packetNumber+", xcenter: "+xcenter+", ycenter: "+ycenter+", xfirst: "+xfirst+", yfirst: "+yfirst+", xsecond: "+xsecond+", ysecond: "+ysecond)
if(xcenter <= xfirst && ycenter <= yfirst){
switchPath = true;
switchPathArr[packetNumber] = switchPath;
}
if(xcenter >= xsecond && ycenter >= ysecond){
switchPath = false;
switchPathArr[packetNumber] = switchPath;
}
if(switchPath){
xcenter = xcenter+slope;
ycenter = ycenter+slope;
xCenterPt[packetNumber] = xcenter;
yCenterPt[packetNumber] = ycenter;
//trace("UP: xcenter = " + xcenter + ", ycenter = " + ycenter);
this[mc_Packets[packetNumber]].x = xcenter;
this[mc_Packets[packetNumber]].y = ycenter;
}
else{
xcenter = xcenter-slope;
ycenter = ycenter-slope;
xCenterPt[packetNumber] = xcenter;
yCenterPt[packetNumber] = ycenter;
//trace("DOWN: xcenter = " + xcenter + ", ycenter = " + ycenter);
this[mc_Packets[packetNumber]].x = xcenter;
this[mc_Packets[packetNumber]].y = ycenter;
}
}