ActionScript Example: Comcast color panels

I got the idea to try and re-create those vibrantly colored panels you see at the end of the Comcast commercials. It proved to be a pretty simple and fun task with pure ActionScript.

Hit any key to swap the panels and their placement. I’m going to see if I can use this code as a transition in some other projects.

package 
{
	import flash.display.*;
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.events.TimerEvent;
	import flash.text.TextField;
	import flash.text.TextFormat;	import flash.utils.Timer;
	
	public class ComcastColors extends Sprite 
	{
		
		private var _panel1:Sprite;
		private var _panel2:Sprite;
		private var _panel3:Sprite;
		private var _timer:Timer;
		
		public function ComcastColors():void
		{
			addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);	
			_panel1 = createPanel( "Oooh, look at the colors", 0xFF00FF, 1, 1, 300, 300 );
			addChild(_panel1);
			_panel2 = createPanel( "Save Moola", 0x99FF00, 5, 5, 300, 300 );
			addChild(_panel2);
			_panel3 = createPanel( "ActionScript is fun", 0x99CCFF, 10, 10, 300, 300 );
			addChild(_panel3);		
		}
		private function createPanel( text:String, color:uint, x:int, y:int, w:int, h:int ):Sprite
		{
			var panel:Sprite = new Sprite();
			panel.graphics.lineStyle();
			panel.graphics.beginFill(color);
			panel.graphics.drawRoundRect(  x, y, w, h, 20 );
			panel.graphics.endFill();
			var formatter:TextFormat = new TextFormat();
			formatter.color = 0xFFFFFF;
			formatter.font = 'Arial';
			formatter.size = 40;
			var textField:TextField = new TextField();
			textField.x = x + 5;
			textField.y = y + 5;
			textField.width = w - 10;
			textField.height = h - 10;
			textField.wordWrap = true;
			textField.text = text;
			textField.setTextFormat(formatter);
			panel.addChild(textField); 
			_timer = new Timer(100);
			_timer.addEventListener("timer", onTimer);
			_timer.start();
			return panel;
		}
		
		private function onTimer(event:TimerEvent):void 
		{
			_panel1.x += .1;
			_panel2.x += .2;
			_panel3.x += .3;
		}

		private function onKeyDown(event:KeyboardEvent):void 
		{
			swapPanels();
		}
		
		private function swapPanels():void 
		{
			var xSave:int = _panel1.x;
			swapChildren(_panel1, _panel2);
			_panel1.x = _panel2.x;
			swapChildren(_panel1, _panel3);		
			_panel3.x = _panel1.x;
			_panel2.x = xSave;
		}
	}
}

Feel free to play with/use/improve this code.