package display { import flash.display.Graphics; import flash.display.Sprite; import flash.events.Event; import flash.geom.Rectangle; import game.Ball; import game.BallGame; public class GameView extends Sprite { protected var _game:BallGame; protected var _balls:Sprite; protected var _ballColour:uint = 0x0000AA; public function GameView( game:BallGame ) { super(); _game = game; _balls = new Sprite(); addChild( _balls ); drawBackground(); } public function get ballColour():uint { return _ballColour; } public function set ballColour( value:uint ):void { _ballColour = value; } /** * start up the view so it draws each frame */ public function init():void { addEventListener(Event.ENTER_FRAME, update, false, 0, true); } /** * draw the background of the view */ protected function drawBackground():void { var bounds:Rectangle = _game.container; var gr:Graphics = this.graphics; gr.clear(); gr.lineStyle( 5, 0x000000); gr.beginFill( 0x7F7F7F ); gr.drawRect( bounds.x, bounds.y, bounds.width, bounds.height ); gr.endFill(); } /** * update listener for redrawing the view each frame */ protected function update(e:Event):void { var arr:Array = _game.balls; var gr:Graphics = _balls.graphics; gr.clear(); gr.lineStyle( 1, 0x000000); for each(var ball:Ball in arr) { gr.beginFill( ballColour ); gr.drawCircle( ball.x, ball.y, ball.radius ); gr.endFill(); } } } }