大体的方法和前面画线是一致的,不同的是,画曲线需要用到控制点,那么为了将曲线平滑连接,将运动的位置点当做控制点,各个点的中点当成是曲线的起点和终点,这样问题就解决了。代码如下:
var ballArr = new Array();
for (var i=0; i< 6; i++) {
var ball = new Ball();
addChild( ball );
ball.x = 100;
ball.y = 100;
ball.vx = Math.random() * 10 + 5;
ball.vy = Math.random() * 10 + 5;
//ball.alpha = 0;
ball.addEventListener(Event.ENTER_FRAME,onMove );
ballArr.push( ball );
}
function onMove( evt:Event ) {
var obj = evt.target;
if (obj.x <= 0 || obj.x >= stage.stageWidth) {
obj.vx = - obj.vx;
}
if (obj.y <= 0 || obj.y >= stage.stageHeight) {
obj.vy = - obj.vy;
}
obj.x += obj.vx;
obj.y+=obj.vy;
}
var paper:Sprite = new Sprite();
addChild(paper);
var pen:Graphics=paper.graphics;
paper.addEventListener(Event.ENTER_FRAME,onDrawLine);
function onDrawLine(evt:Event) {
pen.clear();
pen.lineStyle(2,0xff0000);
pen.moveTo( (ballArr[0].x + ballArr[ballArr.length -1].x)/2 ,(ballArr[0].y+ballArr[ballArr.length -1].y)/2);
for (var i=1; i< ballArr.length-1; i++) {
pen.curveTo( ballArr[i].x,ballArr[i].y,
(ballArr[i].x+ballArr[i+1].x)/2,
(ballArr[i].y+ballArr[i+1].y)/2);
}
pen.curveTo( ballArr[ballArr.length -1].x,ballArr[ballArr.length -1].y,
(ballArr[ballArr.length -1].x + ballArr[0].x)/2 ,
(ballArr[ballArr.length -1].y+ballArr[0].y)/2);
}
短时间内不会去聊天室了,今年的暑期课就此结束,祝大家学习快乐~~