運用for循環畫五角星(多角星):ui
/*** * 多角星 * @param vertex 頂點 角的個數 * @param radius 半徑 * @param color 填充顏色 ***/ //方法: star(12); function star(vertex:int=5,radius:int=100,color:uint=0xff0000) { if (vertex>=2) { //初始點 graphics.moveTo(radius,0); //填充顏色; graphics.beginFill(color); //for循環畫線條 vertex*2須要通過的頂點數; for (var i:int = 1; i < vertex*2; i++) { //半徑 var radius2:Number = radius; //求模,餘數不等於0,這裏其實就是奇、偶數的判斷 if (i % 2 !=0) { //i爲奇數的時候半徑減半 radius2 = radius / 2; } //當前角度 var angle:Number = Math.PI * 2 / (vertex * 2) * i; //點的座標(經過角度與半徑計算每個頂點的座標) graphics.lineTo(Math.cos(angle) * radius2,Math.sin(angle) * radius2); } //結束畫圖 this.graphics.endFill(); //移動圖形座標; x = y = radius; //旋轉圖形 //rotation = -90; } }