以前练习函数时做的一个效果,用代码画一个象棋盘。
var sp:Sprite=new Sprite();
addChild(sp);
sp.x=100;
sp.y=100;
drawSp(50);
function drawSp(r:Number=40,w:Number=2,color:uint=0×000000) {
?sp.graphics.lineStyle(w,color,0.6);
?sp.graphics.drawRect(0,0,9*r,8*r);
?for (var i:int=1; i<=8; i++) {
??sp.graphics.moveTo(i*r,0);
??sp.graphics.lineTo(i*r,8*r);//画竖线8条
?}
?for (i=1; i<=7; i++) {
??sp.graphics.moveTo(0,i*r);
??sp.graphics.lineTo(4*r,i*r);//横线左半
??sp.graphics.moveTo(5*r,i*r);
??sp.graphics.lineTo(9*r,i*r);//横线右半
?}
?sp.graphics.moveTo(0,3*r);
?sp.graphics.lineTo(2*r,5*r);
?sp.graphics.moveTo(0,5*r);
?sp.graphics.lineTo(2*r,3*r);
?sp.graphics.moveTo(7*r,3*r);
?sp.graphics.lineTo(9*r,5*r);
?sp.graphics.moveTo(7*r,5*r);
?sp.graphics.lineTo(9*r,3*r);//画斜线
?for (i=0; i<=9; i++) {
??for (var j:int=0; j<=8; j++) {
???if (((i==2||i==7)&&(j==1||j==7))||((i==3||i==6)&&j%2==0)) {
????j!=0?drawFlag(i*r,j*r,5):0;//上半部分
????j!=8?drawFlag(i*r,j*r,-5):0;
???}
??}
?}
}
function drawFlag(fx:Number=0,fy:Number=0,v:Number=5) {
?var flag:Shape=new Shape();
?sp.addChild(flag);
?flag.graphics.lineStyle(2,0xff0000,0.6);
?flag.graphics.moveTo(fx-v,fy-v);
?flag.graphics.lineTo(fx-2*v,fy-v);
?flag.graphics.moveTo(fx-v,fy-v);
?flag.graphics.lineTo(fx-v,fy-2*v);
?flag.graphics.moveTo(fx+v,fy-v);
?flag.graphics.lineTo(fx+2*v,fy-v);
?flag.graphics.moveTo(fx+v,fy-v);
?flag.graphics.lineTo(fx+v,fy-2*v);
}
addEventListener(Event.ENTER_FRAME,frame);
function frame(e){
?sp.rotationX+=0.5;
?sp.rotationY++;
?//sp.rotationZ+=2;
?}