基本上,我想要的是一个精灵填充一个不断更新的区域。我已经创建了两条线,它们总是指向与光标相反的方向,我想要的是在这两条线的起点和终点处创建一个带有角的精灵。下面是我最初创建精灵的步骤:
function addWalls():void {
var w:Sprite = new Sprite();
w.graphics.beginFill(0x00FF00);
//sArray is an array containing the two lines, and the lines' length == 200
w.graphics.moveTo(sArray[0].x, sArray[0].y);
w.graphics.lineTo(sArray[1].x, sArray[1].y);
w.graphics.lineTo(sArray[1].x + Math.cos(sArray[1].rotation / 180 * Math.PI) * 200, sArray[1].y + Math.sin(sArray[1].rotation / 180 * Math.PI) * 200);
w.graphics.lineTo(sArray[0].x + Math.cos(sArray[0].rotation / 180 * Math.PI) * 200, sArray[0].y + Math.sin(sArray[0].rotation / 180 * Math.PI) * 200);
w.graphics.lineTo(sArray[0].x, sArray[0].y);
w.graphics.endFill();
addChild(w);
wArray[0] = w;
}
我应该如何在enter_frame函数中更新此代码以使其根据行更改?
一个好的解决方案是将您的绘图逻辑移动到它自己的方法。然后在enterFrame处理程序中调用graphics.clear()
方法:
function addWalls():void {
var w:Sprite = new Sprite();
drawWalls();
addChild(w);
wArray[0] = w;
}
function drawWalls(w:Sprite):void {
w.graphics.beginFill(0x00FF00);
//sArray is an array containing the two lines, and the lines' length == 200
w.graphics.moveTo(sArray[0].x, sArray[0].y);
w.graphics.lineTo(sArray[1].x, sArray[1].y);
w.graphics.lineTo(sArray[1].x + Math.cos(sArray[1].rotation / 180 * Math.PI) * 200, sArray[1].y + Math.sin(sArray[1].rotation / 180 * Math.PI) * 200);
w.graphics.lineTo(sArray[0].x + Math.cos(sArray[0].rotation / 180 * Math.PI) * 200, sArray[0].y + Math.sin(sArray[0].rotation / 180 * Math.PI) * 200);
w.graphics.lineTo(sArray[0].x, sArray[0].y);
w.graphics.endFill();
}
function enterFrameHandler(e:Event):void {
for(var i:int=0;i<wArray.length;i++){
wArray[i].graphics.clear(); //clear the current graphics;
drawWalls(wArray[i]);
}
}
this.addEventListener(Event.ENTER_FRAME, enterFrameHandler);