建筑物类:
需要传入两个参数:一个建筑物地基的所有顶点组成的数组(按顺时针或逆时针顺序),和建筑物的相对高度
package { import flash.display.Shape; import flash.display.Sprite; import flash.geom.Point; /** * ... * @author taotao5453 */ public class BuildingShape extends Sprite { /** * 构造 * @param _pointArr 建筑地基点数组(按顺时针或逆时针顺序), * @param _height 建筑物高度 */ private var pointArr:Array; private var buildingHeight:int; private var wallNum:int; //墙面数,和建筑地基点数相等 private var wallColor:uint = 0xcccccc;//墙色 private var topColor:uint = 0xffffff;//定色 private var s:Shape = new Shape ; public function BuildingShape(_pointArr:Array, _height:int=20) { if (_pointArr == null || _pointArr.length == 0) { return; } this.pointArr = _pointArr; this.buildingHeight = _height; wallNum = pointArr.length; this.addChild(s); drawBuildingWall(); drawBuildingTop(); } /** * 绘制建筑每面墙 * */ private function drawBuildingWall():void { var pp1:Point; var pp2:Point; var pp3:Point; var pp4:Point; for (var m:int=0; m<wallNum; m++) { //画每面墙 s.graphics.beginFill(this.wallColor); pp1 = pointArr[m]; pp2 = pointArr[m+1] || pointArr[0]; pp3 = new Point(pp2.x, pp2.y - buildingHeight); pp4 = new Point(pp1.x, pp1.y - buildingHeight); s.graphics.moveTo(pp1.x, pp1.y); s.graphics.lineTo(pp2.x, pp2.y); s.graphics.lineTo(pp3.x, pp3.y); s.graphics.lineTo(pp4.x, pp4.y); } s.graphics.endFill(); } /** * 绘制建筑顶部 * */ private function drawBuildingTop():void { s.graphics.lineStyle(0,this.wallColor); s.graphics.beginFill(this.topColor, 0.9); var pn:Point; for (var n:int=0; n<wallNum; n++) { pn = pointArr[n]; if (n==0) { s.graphics.moveTo(pn.x, pn.y - buildingHeight); } else { s.graphics.lineTo(pn.x, pn.y - buildingHeight); } } s.graphics.endFill(); } } }
package { import flash.display.Sprite; import flash.events.Event; import flash.geom.Point; /** * ... * @author taotao5453 */ public class Main extends Sprite { public function Main():void { if (stage) init(); else addEventListener(Event.ADDED_TO_STAGE, init); } private function init(e:Event = null):void { removeEventListener(Event.ADDED_TO_STAGE, init); // entry point var p1:Point = new Point(0,0); var p2:Point = new Point(70,50); var p3:Point = new Point(100,30); var p4:Point = new Point(65, -5); var p5:Point = new Point(120, -60); var p6:Point = new Point(100, -80); var pArr:Array = new Array(p1,p2,p3,p4,p5,p6); var b:BuildingShape = new BuildingShape(pArr, 30); b.x = 100; b.y = 200; this.addChild(b); } } }